From 8b5d05739f8d36a50c87d2736ff429cbcabe7386 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 11 Feb 2023 15:25:54 -0600 Subject: [PATCH] Fix:Adding new podcast when folder already exists #1462 --- server/controllers/PodcastController.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/server/controllers/PodcastController.js b/server/controllers/PodcastController.js index 0eafd4e6..23cec882 100644 --- a/server/controllers/PodcastController.js +++ b/server/controllers/PodcastController.js @@ -31,21 +31,24 @@ class PodcastController { } const podcastPath = filePathToPOSIX(payload.path) - if (await fs.pathExists(podcastPath)) { - Logger.error(`[PodcastController] Podcast folder already exists "${podcastPath}"`) + + // Check if a library item with this podcast folder exists already + const existingLibraryItem = this.db.libraryItems.find(li => li.path === podcastPath && li.libraryId === library.id) + if (existingLibraryItem) { + Logger.error(`[PodcastController] Podcast already exists with name "${existingLibraryItem.media.metadata.title}" at path "${podcastPath}"`) return res.status(400).send('Podcast already exists') } - var success = await fs.ensureDir(podcastPath).then(() => true).catch((error) => { + const success = await fs.ensureDir(podcastPath).then(() => true).catch((error) => { Logger.error(`[PodcastController] Failed to ensure podcast dir "${podcastPath}"`, error) return false }) if (!success) return res.status(400).send('Invalid podcast path') await filePerms.setDefault(podcastPath) - var libraryItemFolderStats = await getFileTimestampsWithIno(podcastPath) + const libraryItemFolderStats = await getFileTimestampsWithIno(podcastPath) - var relPath = payload.path.replace(folder.fullPath, '') + let relPath = payload.path.replace(folder.fullPath, '') if (relPath.startsWith('/')) relPath = relPath.slice(1) const libraryItemPayload = { @@ -60,14 +63,14 @@ class PodcastController { media: payload.media } - var libraryItem = new LibraryItem() + const libraryItem = new LibraryItem() libraryItem.setData('podcast', libraryItemPayload) // Download and save cover image if (payload.media.metadata.imageUrl) { // TODO: Scan cover image to library files // Podcast cover will always go into library item folder - var coverResponse = await this.coverManager.downloadCoverFromUrl(libraryItem, payload.media.metadata.imageUrl, true) + const coverResponse = await this.coverManager.downloadCoverFromUrl(libraryItem, payload.media.metadata.imageUrl, true) if (coverResponse) { if (coverResponse.error) { Logger.error(`[PodcastController] Download cover error from "${payload.media.metadata.imageUrl}": ${coverResponse.error}`)