Update Book.js to return array of AudioTrack objects on json expand

This commit is contained in:
advplyr 2022-04-03 16:01:59 -05:00
parent 609bf4309f
commit 6e5e638076
7 changed files with 33 additions and 20 deletions

View File

@ -6,6 +6,7 @@ export default class AudioTrack {
this.title = track.title || '' this.title = track.title || ''
this.contentUrl = track.contentUrl || null this.contentUrl = track.contentUrl || null
this.mimeType = track.mimeType this.mimeType = track.mimeType
this.metadata = track.metadata || {}
this.userToken = userToken this.userToken = userToken
} }

View File

@ -55,7 +55,7 @@ class PlaybackSessionManager {
var audioTracks = [] var audioTracks = []
if (shouldDirectPlay) { if (shouldDirectPlay) {
Logger.debug(`[PlaybackSessionManager] "${user.username}" starting direct play session for item "${libraryItem.id}"`) Logger.debug(`[PlaybackSessionManager] "${user.username}" starting direct play session for item "${libraryItem.id}"`)
audioTracks = libraryItem.getDirectPlayTracklist(libraryItem.id, episodeId) audioTracks = libraryItem.getDirectPlayTracklist(episodeId)
newPlaybackSession.playMethod = PlayMethod.DIRECTPLAY newPlaybackSession.playMethod = PlayMethod.DIRECTPLAY
} else { } else {
Logger.debug(`[PlaybackSessionManager] "${user.username}" starting stream session for item "${libraryItem.id}"`) Logger.debug(`[PlaybackSessionManager] "${user.username}" starting stream session for item "${libraryItem.id}"`)

View File

@ -59,8 +59,10 @@ class LibraryItem {
this.mediaType = libraryItem.mediaType this.mediaType = libraryItem.mediaType
if (this.mediaType === 'book') { if (this.mediaType === 'book') {
this.media = new Book(libraryItem.media) this.media = new Book(libraryItem.media)
this.media.libraryItemId = this.id
} else if (this.mediaType === 'podcast') { } else if (this.mediaType === 'podcast') {
this.media = new Podcast(libraryItem.media) this.media = new Podcast(libraryItem.media)
this.media.libraryItemId = this.id
} }
this.libraryFiles = libraryItem.libraryFiles.map(f => new LibraryFile(f)) this.libraryFiles = libraryItem.libraryFiles.map(f => new LibraryFile(f))
@ -163,9 +165,11 @@ class LibraryItem {
if (libraryMediaType === 'podcast') { if (libraryMediaType === 'podcast') {
this.mediaType = 'podcast' this.mediaType = 'podcast'
this.media = new Podcast() this.media = new Podcast()
this.media.libraryItemId = this.id
} else { } else {
this.mediaType = 'book' this.mediaType = 'book'
this.media = new Book() this.media = new Book()
this.media.libraryItemId = this.id
} }
@ -440,8 +444,8 @@ class LibraryItem {
return this.media.searchQuery(query) return this.media.searchQuery(query)
} }
getDirectPlayTracklist(libraryItemId, episodeId) { getDirectPlayTracklist(episodeId) {
return this.media.getDirectPlayTracklist(libraryItemId, episodeId) return this.media.getDirectPlayTracklist(episodeId)
} }
} }
module.exports = LibraryItem module.exports = LibraryItem

View File

@ -1,5 +1,6 @@
const Path = require('path') const Path = require('path')
const { encodeUriPath } = require('../../utils/index') const { encodeUriPath } = require('../../utils/index')
class AudioTrack { class AudioTrack {
constructor() { constructor() {
this.index = null this.index = null
@ -8,6 +9,7 @@ class AudioTrack {
this.title = null this.title = null
this.contentUrl = null this.contentUrl = null
this.mimeType = null this.mimeType = null
this.metadata = null
} }
toJSON() { toJSON() {
@ -17,7 +19,8 @@ class AudioTrack {
duration: this.duration, duration: this.duration,
title: this.title, title: this.title,
contentUrl: this.contentUrl, contentUrl: this.contentUrl,
mimeType: this.mimeType mimeType: this.mimeType,
metadata: this.metadata ? this.metadata.toJSON() : null
} }
} }
@ -28,6 +31,7 @@ class AudioTrack {
this.title = audioFile.metadata.filename || '' this.title = audioFile.metadata.filename || ''
this.contentUrl = Path.join(`/s/item/${itemId}`, encodeUriPath(audioFile.metadata.relPath)) this.contentUrl = Path.join(`/s/item/${itemId}`, encodeUriPath(audioFile.metadata.relPath))
this.mimeType = audioFile.mimeType this.mimeType = audioFile.mimeType
this.metadata = audioFile.metadata.clone()
} }
setFromStream(title, duration, contentUrl) { setFromStream(title, duration, contentUrl) {

View File

@ -11,6 +11,7 @@ const EBookFile = require('../files/EBookFile')
class Book { class Book {
constructor(book) { constructor(book) {
this.libraryItemId = null
this.metadata = null this.metadata = null
this.coverPath = null this.coverPath = null
@ -30,6 +31,7 @@ class Book {
} }
construct(book) { construct(book) {
this.libraryItemId = book.libraryItemId
this.metadata = new BookMetadata(book.metadata) this.metadata = new BookMetadata(book.metadata)
this.coverPath = book.coverPath this.coverPath = book.coverPath
this.tags = [...book.tags] this.tags = [...book.tags]
@ -43,6 +45,7 @@ class Book {
toJSON() { toJSON() {
return { return {
libraryItemId: this.libraryItemId,
metadata: this.metadata.toJSON(), metadata: this.metadata.toJSON(),
coverPath: this.coverPath, coverPath: this.coverPath,
tags: [...this.tags], tags: [...this.tags],
@ -70,6 +73,7 @@ class Book {
toJSONExpanded() { toJSONExpanded() {
return { return {
libraryItemId: this.libraryItemId,
metadata: this.metadata.toJSONExpanded(), metadata: this.metadata.toJSONExpanded(),
coverPath: this.coverPath, coverPath: this.coverPath,
tags: [...this.tags], tags: [...this.tags],
@ -106,8 +110,13 @@ class Book {
return this.missingParts.length || this.audioFiles.some(af => af.invalid) return this.missingParts.length || this.audioFiles.some(af => af.invalid)
} }
get tracks() { get tracks() {
var startOffset = 0
return this.audioFiles.filter(af => !af.exclude && !af.invalid) return this.audioFiles.filter(af => !af.exclude && !af.invalid).map((af) => {
var audioTrack = new AudioTrack()
audioTrack.setData(this.libraryItemId, af, startOffset)
startOffset += audioTrack.duration
return audioTrack
})
} }
get duration() { get duration() {
var total = 0 var total = 0
@ -395,18 +404,8 @@ class Book {
return !this.tracks.some((t) => !supportedMimeTypes.includes(t.mimeType)) return !this.tracks.some((t) => !supportedMimeTypes.includes(t.mimeType))
} }
getDirectPlayTracklist(libraryItemId) { getDirectPlayTracklist() {
var tracklist = [] return this.tracks
var startOffset = 0
this.tracks.forEach((audioFile) => {
var audioTrack = new AudioTrack()
audioTrack.setData(libraryItemId, audioFile, startOffset)
startOffset += audioTrack.duration
tracklist.push(audioTrack)
})
return tracklist
} }
getPlaybackTitle() { getPlaybackTitle() {

View File

@ -9,6 +9,7 @@ const naturalSort = createNewSortInstance({
class Podcast { class Podcast {
constructor(podcast) { constructor(podcast) {
this.libraryItemId = null
this.metadata = null this.metadata = null
this.coverPath = null this.coverPath = null
this.tags = [] this.tags = []
@ -26,6 +27,7 @@ class Podcast {
} }
construct(podcast) { construct(podcast) {
this.libraryItemId = podcast.libraryItemId
this.metadata = new PodcastMetadata(podcast.metadata) this.metadata = new PodcastMetadata(podcast.metadata)
this.coverPath = podcast.coverPath this.coverPath = podcast.coverPath
this.tags = [...podcast.tags] this.tags = [...podcast.tags]
@ -36,6 +38,7 @@ class Podcast {
toJSON() { toJSON() {
return { return {
libraryItemId: this.libraryItemId,
metadata: this.metadata.toJSON(), metadata: this.metadata.toJSON(),
coverPath: this.coverPath, coverPath: this.coverPath,
tags: [...this.tags], tags: [...this.tags],
@ -59,6 +62,7 @@ class Podcast {
toJSONExpanded() { toJSONExpanded() {
return { return {
libraryItemId: this.libraryItemId,
metadata: this.metadata.toJSONExpanded(), metadata: this.metadata.toJSONExpanded(),
coverPath: this.coverPath, coverPath: this.coverPath,
tags: [...this.tags], tags: [...this.tags],
@ -172,10 +176,10 @@ class Podcast {
return episode.checkCanDirectPlay(payload) return episode.checkCanDirectPlay(payload)
} }
getDirectPlayTracklist(libraryItemId, episodeId) { getDirectPlayTracklist(episodeId) {
var episode = this.episodes.find(ep => ep.id === episodeId) var episode = this.episodes.find(ep => ep.id === episodeId)
if (!episode) return false if (!episode) return false
return episode.getDirectPlayTracklist(libraryItemId) return episode.getDirectPlayTracklist(this.libraryItemId)
} }
addPodcastEpisode(podcastEpisode) { addPodcastEpisode(podcastEpisode) {

View File

@ -192,6 +192,7 @@ function makeLibraryItemFromOldAb(audiobook) {
bookMetadata.series = makeSeriesFromOldAb(audiobook.book) bookMetadata.series = makeSeriesFromOldAb(audiobook.book)
} }
bookEntity.libraryItemId = libraryItem.id
bookEntity.metadata = bookMetadata bookEntity.metadata = bookMetadata
bookEntity.coverPath = cleanOldCoverPath(audiobook.book.coverFullPath) bookEntity.coverPath = cleanOldCoverPath(audiobook.book.coverFullPath)
bookEntity.tags = [...audiobook.tags] bookEntity.tags = [...audiobook.tags]