mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Update Book.js to return array of AudioTrack objects on json expand
This commit is contained in:
		
							parent
							
								
									609bf4309f
								
							
						
					
					
						commit
						6e5e638076
					
				@ -6,6 +6,7 @@ export default class AudioTrack {
 | 
			
		||||
    this.title = track.title || ''
 | 
			
		||||
    this.contentUrl = track.contentUrl || null
 | 
			
		||||
    this.mimeType = track.mimeType
 | 
			
		||||
    this.metadata = track.metadata || {}
 | 
			
		||||
 | 
			
		||||
    this.userToken = userToken
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -55,7 +55,7 @@ class PlaybackSessionManager {
 | 
			
		||||
    var audioTracks = []
 | 
			
		||||
    if (shouldDirectPlay) {
 | 
			
		||||
      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
 | 
			
		||||
    } else {
 | 
			
		||||
      Logger.debug(`[PlaybackSessionManager] "${user.username}" starting stream session for item "${libraryItem.id}"`)
 | 
			
		||||
 | 
			
		||||
@ -59,8 +59,10 @@ class LibraryItem {
 | 
			
		||||
    this.mediaType = libraryItem.mediaType
 | 
			
		||||
    if (this.mediaType === 'book') {
 | 
			
		||||
      this.media = new Book(libraryItem.media)
 | 
			
		||||
      this.media.libraryItemId = this.id
 | 
			
		||||
    } else if (this.mediaType === 'podcast') {
 | 
			
		||||
      this.media = new Podcast(libraryItem.media)
 | 
			
		||||
      this.media.libraryItemId = this.id
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.libraryFiles = libraryItem.libraryFiles.map(f => new LibraryFile(f))
 | 
			
		||||
@ -163,9 +165,11 @@ class LibraryItem {
 | 
			
		||||
    if (libraryMediaType === 'podcast') {
 | 
			
		||||
      this.mediaType = 'podcast'
 | 
			
		||||
      this.media = new Podcast()
 | 
			
		||||
      this.media.libraryItemId = this.id
 | 
			
		||||
    } else {
 | 
			
		||||
      this.mediaType = 'book'
 | 
			
		||||
      this.media = new Book()
 | 
			
		||||
      this.media.libraryItemId = this.id
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -440,8 +444,8 @@ class LibraryItem {
 | 
			
		||||
    return this.media.searchQuery(query)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getDirectPlayTracklist(libraryItemId, episodeId) {
 | 
			
		||||
    return this.media.getDirectPlayTracklist(libraryItemId, episodeId)
 | 
			
		||||
  getDirectPlayTracklist(episodeId) {
 | 
			
		||||
    return this.media.getDirectPlayTracklist(episodeId)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
module.exports = LibraryItem
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
const Path = require('path')
 | 
			
		||||
const { encodeUriPath } = require('../../utils/index')
 | 
			
		||||
 | 
			
		||||
class AudioTrack {
 | 
			
		||||
  constructor() {
 | 
			
		||||
    this.index = null
 | 
			
		||||
@ -8,6 +9,7 @@ class AudioTrack {
 | 
			
		||||
    this.title = null
 | 
			
		||||
    this.contentUrl = null
 | 
			
		||||
    this.mimeType = null
 | 
			
		||||
    this.metadata = null
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  toJSON() {
 | 
			
		||||
@ -17,7 +19,8 @@ class AudioTrack {
 | 
			
		||||
      duration: this.duration,
 | 
			
		||||
      title: this.title,
 | 
			
		||||
      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.contentUrl = Path.join(`/s/item/${itemId}`, encodeUriPath(audioFile.metadata.relPath))
 | 
			
		||||
    this.mimeType = audioFile.mimeType
 | 
			
		||||
    this.metadata = audioFile.metadata.clone()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  setFromStream(title, duration, contentUrl) {
 | 
			
		||||
 | 
			
		||||
@ -11,6 +11,7 @@ const EBookFile = require('../files/EBookFile')
 | 
			
		||||
 | 
			
		||||
class Book {
 | 
			
		||||
  constructor(book) {
 | 
			
		||||
    this.libraryItemId = null
 | 
			
		||||
    this.metadata = null
 | 
			
		||||
 | 
			
		||||
    this.coverPath = null
 | 
			
		||||
@ -30,6 +31,7 @@ class Book {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  construct(book) {
 | 
			
		||||
    this.libraryItemId = book.libraryItemId
 | 
			
		||||
    this.metadata = new BookMetadata(book.metadata)
 | 
			
		||||
    this.coverPath = book.coverPath
 | 
			
		||||
    this.tags = [...book.tags]
 | 
			
		||||
@ -43,6 +45,7 @@ class Book {
 | 
			
		||||
 | 
			
		||||
  toJSON() {
 | 
			
		||||
    return {
 | 
			
		||||
      libraryItemId: this.libraryItemId,
 | 
			
		||||
      metadata: this.metadata.toJSON(),
 | 
			
		||||
      coverPath: this.coverPath,
 | 
			
		||||
      tags: [...this.tags],
 | 
			
		||||
@ -70,6 +73,7 @@ class Book {
 | 
			
		||||
 | 
			
		||||
  toJSONExpanded() {
 | 
			
		||||
    return {
 | 
			
		||||
      libraryItemId: this.libraryItemId,
 | 
			
		||||
      metadata: this.metadata.toJSONExpanded(),
 | 
			
		||||
      coverPath: this.coverPath,
 | 
			
		||||
      tags: [...this.tags],
 | 
			
		||||
@ -106,8 +110,13 @@ class Book {
 | 
			
		||||
    return this.missingParts.length || this.audioFiles.some(af => af.invalid)
 | 
			
		||||
  }
 | 
			
		||||
  get tracks() {
 | 
			
		||||
 | 
			
		||||
    return this.audioFiles.filter(af => !af.exclude && !af.invalid)
 | 
			
		||||
    var startOffset = 0
 | 
			
		||||
    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() {
 | 
			
		||||
    var total = 0
 | 
			
		||||
@ -395,18 +404,8 @@ class Book {
 | 
			
		||||
    return !this.tracks.some((t) => !supportedMimeTypes.includes(t.mimeType))
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getDirectPlayTracklist(libraryItemId) {
 | 
			
		||||
    var tracklist = []
 | 
			
		||||
 | 
			
		||||
    var startOffset = 0
 | 
			
		||||
    this.tracks.forEach((audioFile) => {
 | 
			
		||||
      var audioTrack = new AudioTrack()
 | 
			
		||||
      audioTrack.setData(libraryItemId, audioFile, startOffset)
 | 
			
		||||
      startOffset += audioTrack.duration
 | 
			
		||||
      tracklist.push(audioTrack)
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    return tracklist
 | 
			
		||||
  getDirectPlayTracklist() {
 | 
			
		||||
    return this.tracks
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getPlaybackTitle() {
 | 
			
		||||
 | 
			
		||||
@ -9,6 +9,7 @@ const naturalSort = createNewSortInstance({
 | 
			
		||||
 | 
			
		||||
class Podcast {
 | 
			
		||||
  constructor(podcast) {
 | 
			
		||||
    this.libraryItemId = null
 | 
			
		||||
    this.metadata = null
 | 
			
		||||
    this.coverPath = null
 | 
			
		||||
    this.tags = []
 | 
			
		||||
@ -26,6 +27,7 @@ class Podcast {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  construct(podcast) {
 | 
			
		||||
    this.libraryItemId = podcast.libraryItemId
 | 
			
		||||
    this.metadata = new PodcastMetadata(podcast.metadata)
 | 
			
		||||
    this.coverPath = podcast.coverPath
 | 
			
		||||
    this.tags = [...podcast.tags]
 | 
			
		||||
@ -36,6 +38,7 @@ class Podcast {
 | 
			
		||||
 | 
			
		||||
  toJSON() {
 | 
			
		||||
    return {
 | 
			
		||||
      libraryItemId: this.libraryItemId,
 | 
			
		||||
      metadata: this.metadata.toJSON(),
 | 
			
		||||
      coverPath: this.coverPath,
 | 
			
		||||
      tags: [...this.tags],
 | 
			
		||||
@ -59,6 +62,7 @@ class Podcast {
 | 
			
		||||
 | 
			
		||||
  toJSONExpanded() {
 | 
			
		||||
    return {
 | 
			
		||||
      libraryItemId: this.libraryItemId,
 | 
			
		||||
      metadata: this.metadata.toJSONExpanded(),
 | 
			
		||||
      coverPath: this.coverPath,
 | 
			
		||||
      tags: [...this.tags],
 | 
			
		||||
@ -172,10 +176,10 @@ class Podcast {
 | 
			
		||||
    return episode.checkCanDirectPlay(payload)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  getDirectPlayTracklist(libraryItemId, episodeId) {
 | 
			
		||||
  getDirectPlayTracklist(episodeId) {
 | 
			
		||||
    var episode = this.episodes.find(ep => ep.id === episodeId)
 | 
			
		||||
    if (!episode) return false
 | 
			
		||||
    return episode.getDirectPlayTracklist(libraryItemId)
 | 
			
		||||
    return episode.getDirectPlayTracklist(this.libraryItemId)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  addPodcastEpisode(podcastEpisode) {
 | 
			
		||||
 | 
			
		||||
@ -192,6 +192,7 @@ function makeLibraryItemFromOldAb(audiobook) {
 | 
			
		||||
    bookMetadata.series = makeSeriesFromOldAb(audiobook.book)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bookEntity.libraryItemId = libraryItem.id
 | 
			
		||||
  bookEntity.metadata = bookMetadata
 | 
			
		||||
  bookEntity.coverPath = cleanOldCoverPath(audiobook.book.coverFullPath)
 | 
			
		||||
  bookEntity.tags = [...audiobook.tags]
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user