From fde07d26e5a994c3b9fc8dcdc3d3429f7151510f Mon Sep 17 00:00:00 2001 From: advplyr Date: Tue, 6 Jun 2023 16:53:11 -0500 Subject: [PATCH] Update:Prefer epub ebook file when setting ebook #1825, validate ebookLocation --- client/components/readers/EpubReader.vue | 10 ++++++++-- client/components/readers/PdfReader.vue | 4 +++- server/objects/LibraryItem.js | 21 ++++++++++++--------- server/objects/files/LibraryFile.js | 4 ++++ 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/client/components/readers/EpubReader.vue b/client/components/readers/EpubReader.vue index a36c4e43..f43f8a89 100644 --- a/client/components/readers/EpubReader.vue +++ b/client/components/readers/EpubReader.vue @@ -67,6 +67,12 @@ export default { if (!this.libraryItemId) return return this.$store.getters['user/getUserMediaProgress'](this.libraryItemId) }, + savedEbookLocation() { + if (!this.userMediaProgress?.ebookLocation) return null + // Validate ebookLocation is an epubcfi + if (!String(this.userMediaProgress.ebookLocation).startsWith('epubcfi')) return null + return this.userMediaProgress.ebookLocation + }, localStorageLocationsKey() { return `ebookLocations-${this.libraryItemId}` }, @@ -197,7 +203,7 @@ export default { }, /** @param {string} location - CFI of the new location */ relocated(location) { - if (this.userMediaProgress?.ebookLocation === location.start.cfi) { + if (this.savedEbookLocation === location.start.cfi) { return } @@ -233,7 +239,7 @@ export default { }) // load saved progress - reader.rendition.display(this.userMediaProgress?.ebookLocation || reader.book.locations.start) + reader.rendition.display(this.savedEbookLocation || reader.book.locations.start) // load style reader.rendition.themes.default({ '*': { color: '#fff!important', 'background-color': 'rgb(35 35 35)!important' } }) diff --git a/client/components/readers/PdfReader.vue b/client/components/readers/PdfReader.vue index d67a2229..e9a417ac 100644 --- a/client/components/readers/PdfReader.vue +++ b/client/components/readers/PdfReader.vue @@ -95,7 +95,9 @@ export default { return this.$store.getters['user/getUserMediaProgress'](this.libraryItemId) }, savedPage() { - return Number(this.userMediaProgress?.ebookLocation || 0) + // Validate ebookLocation is a number + if (!this.userMediaProgress?.ebookLocation || isNaN(this.userMediaProgress.ebookLocation)) return 0 + return Number(this.userMediaProgress.ebookLocation) }, pdfDocInitParams() { return { diff --git a/server/objects/LibraryItem.js b/server/objects/LibraryItem.js index 7dfc2312..2d1271ee 100644 --- a/server/objects/LibraryItem.js +++ b/server/objects/LibraryItem.js @@ -437,16 +437,19 @@ class LibraryItem { if (this.mediaType === 'book') { // Add/update ebook file (ebooks that were removed are removed in checkScanData) - this.libraryFiles.forEach((lf) => { - if (lf.fileType === 'ebook') { - if (!this.media.ebookFile) { - this.media.setEbookFile(lf) - hasUpdated = true - } else if (this.media.ebookFile.ino == lf.ino && this.media.ebookFile.updateFromLibraryFile(lf)) { // Update existing ebookFile - hasUpdated = true - } + 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 { + // 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) + if (ebookLibraryFile) { + this.media.setEbookFile(ebookLibraryFile) + hasUpdated = true + } + } } // Set cover image if not set diff --git a/server/objects/files/LibraryFile.js b/server/objects/files/LibraryFile.js index d832a135..d78bbaee 100644 --- a/server/objects/files/LibraryFile.js +++ b/server/objects/files/LibraryFile.js @@ -50,6 +50,10 @@ class LibraryFile { return this.fileType === 'audio' || this.fileType === 'ebook' || this.fileType === 'video' } + get isEBookFile() { + return this.fileType === 'ebook' + } + get isOPFFile() { return this.metadata.ext === '.opf' }