Fix book duration to exclude disabled audio tracks

Add Book.updateDuration() which sums the included (non-excluded) audio
files, and call it from updateTracks before saving so disabling a track
updates the stored duration.

Fixes #5311

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
wakamex 2026-06-29 16:57:44 -04:00
parent 2892e99384
commit 9cfe9964c2
2 changed files with 15 additions and 0 deletions

View file

@ -507,6 +507,8 @@ class LibraryItemController {
req.libraryItem.media.audioFiles = updatedAudioFiles req.libraryItem.media.audioFiles = updatedAudioFiles
req.libraryItem.media.changed('audioFiles', true) 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() await req.libraryItem.media.save()
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)

View file

@ -261,6 +261,19 @@ class Book extends Model {
return this.audioFiles.filter((af) => !af.exclude) 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() { get hasMediaFiles() {
return !!this.hasAudioTracks || !!this.ebookFile return !!this.hasAudioTracks || !!this.ebookFile
} }