diff --git a/server/Watcher.js b/server/Watcher.js index b2cbc0e8..c5f64917 100644 --- a/server/Watcher.js +++ b/server/Watcher.js @@ -5,7 +5,7 @@ const Logger = require('./Logger') const Task = require('./objects/Task') const TaskManager = require('./managers/TaskManager') -const { filePathToPOSIX, isSameOrSubPath, getFileMTimeMs } = require('./utils/fileUtils') +const { filePathToPOSIX, isSameOrSubPath, getFileMTimeMs, shouldIgnoreFile } = require('./utils/fileUtils') /** * @typedef PendingFileUpdate @@ -286,15 +286,10 @@ class FolderWatcher extends EventEmitter { const relPath = path.replace(folderPath, '') - if (Path.extname(relPath).toLowerCase() === '.part') { - Logger.debug(`[Watcher] Ignoring .part file "${relPath}"`) - return false - } - - // Ignore files/folders starting with "." - const hasDotPath = relPath.split('/').find((p) => p.startsWith('.')) - if (hasDotPath) { - Logger.debug(`[Watcher] Ignoring dot path "${relPath}" | Piece "${hasDotPath}"`) + // Check for ignored extensions or directories, such as dotfiles and hidden directories + const shouldIgnore = shouldIgnoreFile(relPath) + if (shouldIgnore) { + Logger.debug(`[Watcher] Ignoring ${shouldIgnore} - "${relPath}"`) return false } diff --git a/server/utils/fileUtils.js b/server/utils/fileUtils.js index 19ac2efe..b35a3a99 100644 --- a/server/utils/fileUtils.js +++ b/server/utils/fileUtils.js @@ -131,6 +131,41 @@ async function readTextFile(path) { } module.exports.readTextFile = readTextFile +/** + * Check if file or directory should be ignored. Returns a string of the reason to ignore, or null if not ignored + * + * @param {string} path + * @returns {string} + */ +module.exports.shouldIgnoreFile = (path) => { + var extensionIgnores = ['.part', '.tmp', '.crdownload', '.download', '.bak', '.old', '.temp', '.tempfile', '.tempfile~'] + + // Check extension + if (extensionIgnores.includes(Path.extname(path).toLowerCase())) { + // Return the extension that is ignored + return `${Path.extname(path)} file` + } + + // Check if directory or file name starts with "." + if (Path.basename(path).startsWith('.')) { + return 'dotfile' + } + if (path.split('/').find((p) => p.startsWith('.'))) { + return 'dotpath' + } + + // If these strings exist anywhere in the filename or directory name, ignore. Vendor specific hidden directories + var includeAnywhereIgnore = ['@eaDir'] + var filteredInclude = includeAnywhereIgnore.filter((str) => path.includes(str)) + if (filteredInclude.length) { + return `${filteredInclude[0]} directory` + } + + // Should not ignore this file or directory + return null +} +module.exports.shouldIgnoreFile = this.shouldIgnoreFile + /** * @typedef FilePathItem * @property {string} name - file name e.g. "audiofile.m4b" @@ -197,14 +232,10 @@ async function recurseFiles(path, relPathToReplace = null) { return false } - if (item.extension === '.part') { - Logger.debug(`[fileUtils] Ignoring .part file "${relpath}"`) - return false - } - - // Ignore any file if a directory or the filename starts with "." - if (relpath.split('/').find((p) => p.startsWith('.'))) { - Logger.debug(`[fileUtils] Ignoring path has . "${relpath}"`) + // Check for ignored extensions or directories + const shouldIgnore = this.shouldIgnoreFile(relpath) + if (shouldIgnore) { + Logger.debug(`[fileUtils] Ignoring ${shouldIgnore} - "${relpath}"`) return false }