Add:Audiobooks only library settings, supplementary ebooks #1664

This commit is contained in:
advplyr 2023-06-10 12:46:57 -05:00
parent 4b4fb33d8f
commit 014fc45c15
39 changed files with 624 additions and 122 deletions

View file

@ -33,6 +33,9 @@ class Library {
get isMusic() {
return this.mediaType === 'music'
}
get isBook() {
return this.mediaType === 'book'
}
construct(library) {
this.id = library.id

View file

@ -80,6 +80,16 @@ class LibraryItem {
this.media.libraryItemId = this.id
this.libraryFiles = libraryItem.libraryFiles.map(f => new LibraryFile(f))
// Migration for v2.2.23 to set ebook library files as supplementary
if (this.isBook && this.media.ebookFile) {
for (const libraryFile of this.libraryFiles) {
if (libraryFile.isEBookFile && libraryFile.isSupplementary === null) {
libraryFile.isSupplementary = this.media.ebookFile.ino !== libraryFile.ino
}
}
}
}
toJSON() {
@ -432,21 +442,30 @@ class LibraryItem {
}
// Set metadata from files
async syncFiles(preferOpfMetadata) {
async syncFiles(preferOpfMetadata, librarySettings) {
let hasUpdated = false
if (this.mediaType === 'book') {
// Add/update ebook file (ebooks that were removed are removed in checkScanData)
if (this.media.ebookFile) {
if (this.isBook) {
// Add/update ebook files (ebooks that were removed are removed in checkScanData)
if (librarySettings.audiobooksOnly) {
hasUpdated = this.media.ebookFile
if (hasUpdated) {
// If library was set to audiobooks only then set primary ebook as supplementary
Logger.info(`[LibraryItem] Library is audiobooks only so setting ebook "${this.media.ebookFile.metadata.filename}" as supplementary`)
}
this.setPrimaryEbook(null)
} else if (this.media.ebookFile) {
const matchingLibraryFile = this.libraryFiles.find(lf => lf.ino === this.media.ebookFile.ino)
if (matchingLibraryFile && this.media.ebookFile.updateFromLibraryFile(matchingLibraryFile)) {
hasUpdated = true
}
} else {
const ebookLibraryFiles = this.libraryFiles.filter(lf => lf.isEBookFile && !lf.isSupplementary)
// Prefer epub ebook then fallback to first other ebook file
const ebookLibraryFile = this.libraryFiles.find(lf => lf.isEBookFile && lf.metadata.format === 'epub') || this.libraryFiles.find(lf => lf.isEBookFile)
const ebookLibraryFile = ebookLibraryFiles.find(lf => lf.metadata.format === 'epub') || ebookLibraryFiles[0]
if (ebookLibraryFile) {
this.media.setEbookFile(ebookLibraryFile)
this.setPrimaryEbook(ebookLibraryFile)
hasUpdated = true
}
}
@ -565,5 +584,20 @@ class LibraryItem {
}
return false
}
/**
* Set the EBookFile from a LibraryFile
* If null then ebookFile will be removed from the book
* all ebook library files that are not primary are marked as supplementary
*
* @param {LibraryFile} [libraryFile]
*/
setPrimaryEbook(ebookLibraryFile = null) {
const ebookLibraryFiles = this.libraryFiles.filter(lf => lf.isEBookFile)
for (const libraryFile of ebookLibraryFiles) {
libraryFile.isSupplementary = ebookLibraryFile?.ino !== libraryFile.ino
}
this.media.setEbookFile(ebookLibraryFile)
}
}
module.exports = LibraryItem

View file

@ -7,6 +7,7 @@ class LibraryFile {
constructor(file) {
this.ino = null
this.metadata = null
this.isSupplementary = null
this.addedAt = null
this.updatedAt = null
@ -18,6 +19,7 @@ class LibraryFile {
construct(file) {
this.ino = file.ino
this.metadata = new FileMetadata(file.metadata)
this.isSupplementary = file.isSupplementary === undefined ? null : file.isSupplementary
this.addedAt = file.addedAt
this.updatedAt = file.updatedAt
}
@ -26,6 +28,7 @@ class LibraryFile {
return {
ino: this.ino,
metadata: this.metadata.toJSON(),
isSupplementary: this.isSupplementary,
addedAt: this.addedAt,
updatedAt: this.updatedAt,
fileType: this.fileType

View file

@ -370,10 +370,20 @@ class Book {
return payload
}
setEbookFile(libraryFile) {
var ebookFile = new EBookFile()
ebookFile.setData(libraryFile)
this.ebookFile = ebookFile
/**
* Set the EBookFile from a LibraryFile
* If null then ebookFile will be removed from the book
*
* @param {LibraryFile} [libraryFile]
*/
setEbookFile(libraryFile = null) {
if (!libraryFile) {
this.ebookFile = null
} else {
const ebookFile = new EBookFile()
ebookFile.setData(libraryFile)
this.ebookFile = ebookFile
}
}
addAudioFile(audioFile) {

View file

@ -7,6 +7,7 @@ class LibrarySettings {
this.skipMatchingMediaWithAsin = false
this.skipMatchingMediaWithIsbn = false
this.autoScanCronExpression = null
this.audiobooksOnly = false
if (settings) {
this.construct(settings)
@ -19,6 +20,7 @@ class LibrarySettings {
this.skipMatchingMediaWithAsin = !!settings.skipMatchingMediaWithAsin
this.skipMatchingMediaWithIsbn = !!settings.skipMatchingMediaWithIsbn
this.autoScanCronExpression = settings.autoScanCronExpression || null
this.audiobooksOnly = !!settings.audiobooksOnly
}
toJSON() {
@ -27,12 +29,13 @@ class LibrarySettings {
disableWatcher: this.disableWatcher,
skipMatchingMediaWithAsin: this.skipMatchingMediaWithAsin,
skipMatchingMediaWithIsbn: this.skipMatchingMediaWithIsbn,
autoScanCronExpression: this.autoScanCronExpression
autoScanCronExpression: this.autoScanCronExpression,
audiobooksOnly: this.audiobooksOnly
}
}
update(payload) {
var hasUpdates = false
let hasUpdates = false
for (const key in payload) {
if (this[key] !== payload[key]) {
this[key] = payload[key]