From 9916a1e8f68fa08d2717746b95c85aa8fba65652 Mon Sep 17 00:00:00 2001 From: advplyr Date: Tue, 19 Jul 2022 08:33:32 -0500 Subject: [PATCH] Fix:Watcher scanner to ignore non-media files that are not inside library item folders #834 --- server/scanner/Scanner.js | 1 + server/utils/scandir.js | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/server/scanner/Scanner.js b/server/scanner/Scanner.js index 2f1d2456..a67e0589 100644 --- a/server/scanner/Scanner.js +++ b/server/scanner/Scanner.js @@ -512,6 +512,7 @@ class Scanner { } var relFilePaths = folderGroups[folderId].fileUpdates.map(fileUpdate => fileUpdate.relPath) var fileUpdateGroup = groupFilesIntoLibraryItemPaths(library.mediaType, relFilePaths) + if (!Object.keys(fileUpdateGroup).length) { Logger.info(`[Scanner] No important changes to scan for in folder "${folderId}"`) continue; diff --git a/server/utils/scandir.js b/server/utils/scandir.js index 5f846944..5618ba3c 100644 --- a/server/utils/scandir.js +++ b/server/utils/scandir.js @@ -20,11 +20,22 @@ function isMediaFile(mediaType, ext) { // Output: map of files grouped into potential item dirs function groupFilesIntoLibraryItemPaths(mediaType, paths) { // Step 1: Clean path, Remove leading "/", Filter out non-media files in root dir + var nonMediaFilePaths = [] var pathsFiltered = paths.map(path => { return path.startsWith('/') ? path.slice(1) : path }).filter(path => { let parsedPath = Path.parse(path) - return parsedPath.dir || (mediaType === 'book' && isMediaFile(mediaType, parsedPath.ext)) + // Is not in root dir OR is a book media file + if (parsedPath.dir) { + if (!isMediaFile(mediaType, parsedPath.ext)) { // Seperate out non-media files + nonMediaFilePaths.push(path) + return false + } + return true + } else if (mediaType === 'book' && isMediaFile(mediaType, parsedPath.ext)) { // (book media type supports single file audiobooks/ebooks in root dir) + return true + } + return false }) // Step 2: Sort by least number of directories @@ -64,6 +75,17 @@ function groupFilesIntoLibraryItemPaths(mediaType, paths) { } } }) + + // Step 4: Add in non-media files if they fit into item group + if (nonMediaFilePaths.length) { + for (const nonMediaFilePath of nonMediaFilePaths) { + const pathDir = Path.dirname(nonMediaFilePath) + if (itemGroup[pathDir]) { + itemGroup[pathDir].push(nonMediaFilePath) + } + } + } + return itemGroup } module.exports.groupFilesIntoLibraryItemPaths = groupFilesIntoLibraryItemPaths