From bac09de23dd00bdcc4c554fba22622c7192cf66b Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 12 May 2023 18:22:09 -0500 Subject: [PATCH] Fix:getNarrators API endpoint check narrators are strings #1770 --- server/controllers/LibraryController.js | 6 ++++-- server/objects/metadata/BookMetadata.js | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 0dfd1b6f..82ccd09e 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -709,9 +709,11 @@ class LibraryController { async getNarrators(req, res) { const narrators = {} req.libraryItems.forEach((li) => { - if (li.media.metadata.narrators && li.media.metadata.narrators.length) { + if (li.media.metadata.narrators?.length) { li.media.metadata.narrators.forEach((n) => { - if (!narrators[n]) { + if (typeof n !== 'string') { + Logger.error(`[LibraryController] getNarrators: Invalid narrator "${n}" on book "${li.media.metadata.title}"`) + } else if (!narrators[n]) { narrators[n] = { id: encodeURIComponent(Buffer.from(n).toString('base64')), name: n, diff --git a/server/objects/metadata/BookMetadata.js b/server/objects/metadata/BookMetadata.js index a924c3c0..586d699a 100644 --- a/server/objects/metadata/BookMetadata.js +++ b/server/objects/metadata/BookMetadata.js @@ -27,9 +27,9 @@ class BookMetadata { construct(metadata) { this.title = metadata.title this.subtitle = metadata.subtitle - this.authors = (metadata.authors && metadata.authors.map) ? metadata.authors.map(a => ({ ...a })) : [] - this.narrators = metadata.narrators ? [...metadata.narrators] : [] - this.series = (metadata.series && metadata.series.map) ? metadata.series.map(s => ({ ...s })) : [] + this.authors = (metadata.authors?.map) ? metadata.authors.map(a => ({ ...a })) : [] + this.narrators = metadata.narrators ? [...metadata.narrators].filter(n => n) : [] + this.series = (metadata.series?.map) ? metadata.series.map(s => ({ ...s })) : [] this.genres = metadata.genres ? [...metadata.genres] : [] this.publishedYear = metadata.publishedYear || null this.publishedDate = metadata.publishedDate || null @@ -230,7 +230,7 @@ class BookMetadata { updateNarrator(oldNarratorName, newNarratorName) { if (!this.hasNarrator(oldNarratorName)) return false this.narrators = this.narrators.filter(n => n !== oldNarratorName) - if (!this.hasNarrator(newNarratorName)) { + if (newNarratorName && !this.hasNarrator(newNarratorName)) { this.narrators.push(newNarratorName) } return true