mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-05 02:34:56 +02:00
Update PlaybackSession to use new library item model
This commit is contained in:
parent
d205c6f734
commit
c251f1899d
16 changed files with 284 additions and 193 deletions
|
@ -62,6 +62,13 @@ const parseNameString = require('../utils/parsers/parseNameString')
|
|||
* @property {ChapterObject[]} chapters
|
||||
* @property {Object} metaTags
|
||||
* @property {string} mimeType
|
||||
*
|
||||
* @typedef AudioTrackProperties
|
||||
* @property {string} title
|
||||
* @property {string} contentUrl
|
||||
* @property {number} startOffset
|
||||
*
|
||||
* @typedef {AudioFileObject & AudioTrackProperties} AudioTrack
|
||||
*/
|
||||
|
||||
class Book extends Model {
|
||||
|
@ -367,16 +374,6 @@ class Book extends Model {
|
|||
return this.audioFiles.filter((af) => !af.exclude)
|
||||
}
|
||||
|
||||
get trackList() {
|
||||
let startOffset = 0
|
||||
return this.includedAudioFiles.map((af) => {
|
||||
const track = structuredClone(af)
|
||||
track.startOffset = startOffset
|
||||
startOffset += track.duration
|
||||
return track
|
||||
})
|
||||
}
|
||||
|
||||
get hasMediaFiles() {
|
||||
return !!this.hasAudioTracks || !!this.ebookFile
|
||||
}
|
||||
|
@ -385,6 +382,59 @@ class Book extends Model {
|
|||
return !!this.includedAudioFiles.length
|
||||
}
|
||||
|
||||
/**
|
||||
* Supported mime types are sent from the web client and are retrieved using the browser Audio player "canPlayType" function.
|
||||
*
|
||||
* @param {string[]} supportedMimeTypes
|
||||
* @returns {boolean}
|
||||
*/
|
||||
checkCanDirectPlay(supportedMimeTypes) {
|
||||
if (!Array.isArray(supportedMimeTypes)) {
|
||||
Logger.error(`[Book] checkCanDirectPlay: supportedMimeTypes is not an array`, supportedMimeTypes)
|
||||
return false
|
||||
}
|
||||
return this.includedAudioFiles.every((af) => supportedMimeTypes.includes(af.mimeType))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the track list to be used in client audio players
|
||||
* AudioTrack is the AudioFile with startOffset, contentUrl and title
|
||||
*
|
||||
* @param {string} libraryItemId
|
||||
* @returns {AudioTrack[]}
|
||||
*/
|
||||
getTracklist(libraryItemId) {
|
||||
let startOffset = 0
|
||||
return this.includedAudioFiles.map((af) => {
|
||||
const track = structuredClone(af)
|
||||
track.title = af.metadata.filename
|
||||
track.startOffset = startOffset
|
||||
track.contentUrl = `${global.RouterBasePath}/api/items/${libraryItemId}/file/${track.ino}`
|
||||
startOffset += track.duration
|
||||
return track
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {ChapterObject[]}
|
||||
*/
|
||||
getChapters() {
|
||||
return structuredClone(this.chapters) || []
|
||||
}
|
||||
|
||||
getPlaybackTitle() {
|
||||
return this.title
|
||||
}
|
||||
|
||||
getPlaybackAuthor() {
|
||||
return this.authorName
|
||||
}
|
||||
|
||||
getPlaybackDuration() {
|
||||
return this.duration
|
||||
}
|
||||
|
||||
/**
|
||||
* Total file size of all audio files and ebook file
|
||||
*
|
||||
|
@ -635,7 +685,7 @@ class Book extends Model {
|
|||
metadata: this.oldMetadataToJSONMinified(),
|
||||
coverPath: this.coverPath,
|
||||
tags: [...(this.tags || [])],
|
||||
numTracks: this.trackList.length,
|
||||
numTracks: this.includedAudioFiles.length,
|
||||
numAudioFiles: this.audioFiles?.length || 0,
|
||||
numChapters: this.chapters?.length || 0,
|
||||
duration: this.duration,
|
||||
|
@ -666,7 +716,7 @@ class Book extends Model {
|
|||
ebookFile: structuredClone(this.ebookFile),
|
||||
duration: this.duration,
|
||||
size: this.size,
|
||||
tracks: structuredClone(this.trackList)
|
||||
tracks: this.getTracklist(libraryItemId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -112,15 +112,15 @@ class FeedEpisode extends Model {
|
|||
/**
|
||||
* If chapters for an audiobook match the audio tracks then use chapter titles instead of audio file names
|
||||
*
|
||||
* @param {import('./Book').AudioTrack[]} trackList
|
||||
* @param {import('./Book')} book
|
||||
* @returns {boolean}
|
||||
*/
|
||||
static checkUseChapterTitlesForEpisodes(book) {
|
||||
const tracks = book.trackList || []
|
||||
static checkUseChapterTitlesForEpisodes(trackList, book) {
|
||||
const chapters = book.chapters || []
|
||||
if (tracks.length !== chapters.length) return false
|
||||
for (let i = 0; i < tracks.length; i++) {
|
||||
if (Math.abs(chapters[i].start - tracks[i].startOffset) >= 1) {
|
||||
if (trackList.length !== chapters.length) return false
|
||||
for (let i = 0; i < trackList.length; i++) {
|
||||
if (Math.abs(chapters[i].start - trackList[i].startOffset) >= 1) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ class FeedEpisode extends Model {
|
|||
const contentUrl = `/feed/${slug}/item/${episodeId}/media${Path.extname(audioTrack.metadata.filename)}`
|
||||
|
||||
let title = Path.basename(audioTrack.metadata.filename, Path.extname(audioTrack.metadata.filename))
|
||||
if (book.trackList.length == 1) {
|
||||
if (book.includedAudioFiles.length == 1) {
|
||||
// If audiobook is a single file, use book title instead of chapter/file title
|
||||
title = book.title
|
||||
} else {
|
||||
|
@ -185,11 +185,12 @@ class FeedEpisode extends Model {
|
|||
* @returns {Promise<FeedEpisode[]>}
|
||||
*/
|
||||
static async createFromAudiobookTracks(libraryItemExpanded, feed, slug, transaction) {
|
||||
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(libraryItemExpanded.media)
|
||||
const trackList = libraryItemExpanded.getTrackList()
|
||||
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(trackList, libraryItemExpanded.media)
|
||||
|
||||
const feedEpisodeObjs = []
|
||||
let numExisting = 0
|
||||
for (const track of libraryItemExpanded.media.trackList) {
|
||||
for (const track of trackList) {
|
||||
// Check for existing episode by filepath
|
||||
const existingEpisode = feed.feedEpisodes?.find((episode) => {
|
||||
return episode.filePath === track.metadata.path
|
||||
|
@ -204,7 +205,7 @@ class FeedEpisode extends Model {
|
|||
|
||||
/**
|
||||
*
|
||||
* @param {import('./Book')[]} books
|
||||
* @param {import('./Book').BookExpandedWithLibraryItem[]} books
|
||||
* @param {import('./Feed')} feed
|
||||
* @param {string} slug
|
||||
* @param {import('sequelize').Transaction} transaction
|
||||
|
@ -218,8 +219,9 @@ class FeedEpisode extends Model {
|
|||
const feedEpisodeObjs = []
|
||||
let numExisting = 0
|
||||
for (const book of books) {
|
||||
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(book)
|
||||
for (const track of book.trackList) {
|
||||
const trackList = book.libraryItem.getTrackList()
|
||||
const useChapterTitles = this.checkUseChapterTitlesForEpisodes(trackList, book)
|
||||
for (const track of trackList) {
|
||||
// Check for existing episode by filepath
|
||||
const existingEpisode = feed.feedEpisodes?.find((episode) => {
|
||||
return episode.filePath === track.metadata.path
|
||||
|
|
|
@ -497,6 +497,57 @@ class LibraryItem extends Model {
|
|||
return libraryItem
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('sequelize').WhereOptions} where
|
||||
* @param {import('sequelize').IncludeOptions} [include]
|
||||
* @returns {Promise<LibraryItemExpanded>}
|
||||
*/
|
||||
static async findOneExpanded(where, include = null) {
|
||||
const libraryItem = await this.findOne({
|
||||
where,
|
||||
include
|
||||
})
|
||||
if (!libraryItem) {
|
||||
Logger.error(`[LibraryItem] Library item not found`)
|
||||
return null
|
||||
}
|
||||
|
||||
if (libraryItem.mediaType === 'podcast') {
|
||||
libraryItem.media = await libraryItem.getMedia({
|
||||
include: [
|
||||
{
|
||||
model: this.sequelize.models.podcastEpisode
|
||||
}
|
||||
]
|
||||
})
|
||||
} else {
|
||||
libraryItem.media = await libraryItem.getMedia({
|
||||
include: [
|
||||
{
|
||||
model: this.sequelize.models.author,
|
||||
through: {
|
||||
attributes: []
|
||||
}
|
||||
},
|
||||
{
|
||||
model: this.sequelize.models.series,
|
||||
through: {
|
||||
attributes: ['id', 'sequence']
|
||||
}
|
||||
}
|
||||
],
|
||||
order: [
|
||||
[this.sequelize.models.author, this.sequelize.models.bookAuthor, 'createdAt', 'ASC'],
|
||||
[this.sequelize.models.series, 'bookSeries', 'createdAt', 'ASC']
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
if (!libraryItem.media) return null
|
||||
return libraryItem
|
||||
}
|
||||
|
||||
/**
|
||||
* Get old library item by id
|
||||
* @param {string} libraryItemId
|
||||
|
@ -1176,6 +1227,22 @@ class LibraryItem extends Model {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the track list to be used in client audio players
|
||||
* AudioTrack is the AudioFile with startOffset and contentUrl
|
||||
* Podcasts must have an episodeId to get the track list
|
||||
*
|
||||
* @param {string} [episodeId]
|
||||
* @returns {import('./Book').AudioTrack[]}
|
||||
*/
|
||||
getTrackList(episodeId) {
|
||||
if (!this.media) {
|
||||
Logger.error(`[LibraryItem] getTrackList: Library item "${this.id}" does not have media`)
|
||||
return []
|
||||
}
|
||||
return this.media.getTracklist(this.id, episodeId)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} ino
|
||||
|
|
|
@ -76,42 +76,26 @@ class MediaItemShare extends Model {
|
|||
}
|
||||
|
||||
/**
|
||||
* Expanded book that includes library settings
|
||||
*
|
||||
* @param {string} mediaItemId
|
||||
* @param {string} mediaItemType
|
||||
* @returns {Promise<import('../objects/LibraryItem')>}
|
||||
* @returns {Promise<import('./LibraryItem').LibraryItemExpanded>}
|
||||
*/
|
||||
static async getMediaItemsOldLibraryItem(mediaItemId, mediaItemType) {
|
||||
static async getMediaItemsLibraryItem(mediaItemId, mediaItemType) {
|
||||
/** @type {typeof import('./LibraryItem')} */
|
||||
const libraryItemModel = this.sequelize.models.libraryItem
|
||||
|
||||
if (mediaItemType === 'book') {
|
||||
const book = await this.sequelize.models.book.findByPk(mediaItemId, {
|
||||
include: [
|
||||
{
|
||||
model: this.sequelize.models.author,
|
||||
through: {
|
||||
attributes: []
|
||||
}
|
||||
},
|
||||
{
|
||||
model: this.sequelize.models.series,
|
||||
through: {
|
||||
attributes: ['sequence']
|
||||
}
|
||||
},
|
||||
{
|
||||
model: this.sequelize.models.libraryItem,
|
||||
include: {
|
||||
model: this.sequelize.models.library,
|
||||
attributes: ['settings']
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
const libraryItem = book.libraryItem
|
||||
libraryItem.media = book
|
||||
delete book.libraryItem
|
||||
const oldLibraryItem = this.sequelize.models.libraryItem.getOldLibraryItem(libraryItem)
|
||||
oldLibraryItem.librarySettings = libraryItem.library.settings
|
||||
return oldLibraryItem
|
||||
const libraryItem = await libraryItemModel.findOneExpanded(
|
||||
{ mediaId: mediaItemId },
|
||||
{
|
||||
model: this.sequelize.models.library,
|
||||
attributes: ['settings']
|
||||
}
|
||||
)
|
||||
|
||||
return libraryItem
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
|
|
@ -276,6 +276,78 @@ class Podcast extends Model {
|
|||
return hasUpdates
|
||||
}
|
||||
|
||||
checkCanDirectPlay(supportedMimeTypes, episodeId) {
|
||||
if (!Array.isArray(supportedMimeTypes)) {
|
||||
Logger.error(`[Podcast] checkCanDirectPlay: supportedMimeTypes is not an array`, supportedMimeTypes)
|
||||
return false
|
||||
}
|
||||
const episode = this.podcastEpisodes.find((ep) => ep.id === episodeId)
|
||||
if (!episode) {
|
||||
Logger.error(`[Podcast] checkCanDirectPlay: episode not found`, episodeId)
|
||||
return false
|
||||
}
|
||||
return supportedMimeTypes.includes(episode.audioFile.mimeType)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the track list to be used in client audio players
|
||||
* AudioTrack is the AudioFile with startOffset and contentUrl
|
||||
* Podcast episodes only have one track
|
||||
*
|
||||
* @param {string} libraryItemId
|
||||
* @param {string} episodeId
|
||||
* @returns {import('./Book').AudioTrack[]}
|
||||
*/
|
||||
getTracklist(libraryItemId, episodeId) {
|
||||
const episode = this.podcastEpisodes.find((ep) => ep.id === episodeId)
|
||||
if (!episode) {
|
||||
Logger.error(`[Podcast] getTracklist: episode not found`, episodeId)
|
||||
return []
|
||||
}
|
||||
|
||||
const audioTrack = episode.getAudioTrack(libraryItemId)
|
||||
return [audioTrack]
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} episodeId
|
||||
* @returns {import('./PodcastEpisode').ChapterObject[]}
|
||||
*/
|
||||
getChapters(episodeId) {
|
||||
const episode = this.podcastEpisodes.find((ep) => ep.id === episodeId)
|
||||
if (!episode) {
|
||||
Logger.error(`[Podcast] getChapters: episode not found`, episodeId)
|
||||
return []
|
||||
}
|
||||
|
||||
return structuredClone(episode.chapters) || []
|
||||
}
|
||||
|
||||
getPlaybackTitle(episodeId) {
|
||||
const episode = this.podcastEpisodes.find((ep) => ep.id === episodeId)
|
||||
if (!episode) {
|
||||
Logger.error(`[Podcast] getPlaybackTitle: episode not found`, episodeId)
|
||||
return ''
|
||||
}
|
||||
|
||||
return episode.title
|
||||
}
|
||||
|
||||
getPlaybackAuthor() {
|
||||
return this.author
|
||||
}
|
||||
|
||||
getPlaybackDuration(episodeId) {
|
||||
const episode = this.podcastEpisodes.find((ep) => ep.id === episodeId)
|
||||
if (!episode) {
|
||||
Logger.error(`[Podcast] getPlaybackDuration: episode not found`, episodeId)
|
||||
return 0
|
||||
}
|
||||
|
||||
return episode.duration
|
||||
}
|
||||
|
||||
/**
|
||||
* Old model kept metadata in a separate object
|
||||
*/
|
||||
|
|
|
@ -135,23 +135,28 @@ class PodcastEpisode extends Model {
|
|||
PodcastEpisode.belongsTo(podcast)
|
||||
}
|
||||
|
||||
get size() {
|
||||
return this.audioFile?.metadata.size || 0
|
||||
}
|
||||
|
||||
get duration() {
|
||||
return this.audioFile?.duration || 0
|
||||
}
|
||||
|
||||
/**
|
||||
* AudioTrack object used in old model
|
||||
* Used in client players
|
||||
*
|
||||
* @returns {import('./Book').AudioFileObject|null}
|
||||
* @param {string} libraryItemId
|
||||
* @returns {import('./Book').AudioTrack}
|
||||
*/
|
||||
get track() {
|
||||
if (!this.audioFile) return null
|
||||
getAudioTrack(libraryItemId) {
|
||||
const track = structuredClone(this.audioFile)
|
||||
track.startOffset = 0
|
||||
track.title = this.audioFile.metadata.title
|
||||
track.contentUrl = `${global.RouterBasePath}/api/items/${libraryItemId}/file/${track.ino}`
|
||||
return track
|
||||
}
|
||||
|
||||
get size() {
|
||||
return this.audioFile?.metadata.size || 0
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} libraryItemId
|
||||
* @returns {oldPodcastEpisode}
|
||||
|
@ -228,9 +233,9 @@ class PodcastEpisode extends Model {
|
|||
toOldJSONExpanded(libraryItemId) {
|
||||
const json = this.toOldJSON(libraryItemId)
|
||||
|
||||
json.audioTrack = this.track
|
||||
json.audioTrack = this.getAudioTrack(libraryItemId)
|
||||
json.size = this.size
|
||||
json.duration = this.audioFile?.duration || 0
|
||||
json.duration = this.duration
|
||||
|
||||
return json
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue