diff --git a/server/controllers/LibraryItemController.js b/server/controllers/LibraryItemController.js index 07b4ee67f..ddfe2fa34 100644 --- a/server/controllers/LibraryItemController.js +++ b/server/controllers/LibraryItemController.js @@ -507,6 +507,8 @@ class LibraryItemController { req.libraryItem.media.audioFiles = updatedAudioFiles req.libraryItem.media.changed('audioFiles', true) + // Recompute duration so disabled (excluded) tracks no longer count toward the total + req.libraryItem.media.updateDuration() await req.libraryItem.media.save() SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) diff --git a/server/models/Book.js b/server/models/Book.js index d9f2ff132..2e4d59bee 100644 --- a/server/models/Book.js +++ b/server/models/Book.js @@ -261,6 +261,19 @@ class Book extends Model { return this.audioFiles.filter((af) => !af.exclude) } + /** + * Recalculate the book duration from the included (non-excluded) audio files. + * Sets `this.duration` and returns whether it changed. + * + * @returns {boolean} + */ + updateDuration() { + const newDuration = this.includedAudioFiles.reduce((total, af) => total + (isNaN(af.duration) ? 0 : Number(af.duration)), 0) + if (this.duration === newDuration) return false + this.duration = newDuration + return true + } + get hasMediaFiles() { return !!this.hasAudioTracks || !!this.ebookFile }