2023-09-08 21:50:59 +02:00
|
|
|
const Path = require('path')
|
2021-09-07 03:14:04 +02:00
|
|
|
const EventEmitter = require('events')
|
2022-05-29 03:01:20 +02:00
|
|
|
const Watcher = require('./libs/watcher/watcher')
|
2021-09-07 03:14:04 +02:00
|
|
|
const Logger = require('./Logger')
|
2023-09-04 18:50:55 +02:00
|
|
|
const LibraryScanner = require('./scanner/LibraryScanner')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2023-01-06 00:45:27 +01:00
|
|
|
const { filePathToPOSIX } = require('./utils/fileUtils')
|
|
|
|
|
2023-09-04 18:50:55 +02:00
|
|
|
/**
|
|
|
|
* @typedef PendingFileUpdate
|
|
|
|
* @property {string} path
|
|
|
|
* @property {string} relPath
|
|
|
|
* @property {string} folderId
|
|
|
|
* @property {string} type
|
|
|
|
*/
|
2021-08-18 00:01:11 +02:00
|
|
|
class FolderWatcher extends EventEmitter {
|
2021-10-05 05:11:42 +02:00
|
|
|
constructor() {
|
2021-08-18 00:01:11 +02:00
|
|
|
super()
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2023-09-04 18:50:55 +02:00
|
|
|
/** @type {{id:string, name:string, folders:import('./objects/Folder')[], paths:string[], watcher:Watcher[]}[]} */
|
2021-10-05 05:11:42 +02:00
|
|
|
this.libraryWatchers = []
|
2023-09-04 18:50:55 +02:00
|
|
|
/** @type {PendingFileUpdate[]} */
|
2021-10-05 05:11:42 +02:00
|
|
|
this.pendingFileUpdates = []
|
2021-09-11 02:55:02 +02:00
|
|
|
this.pendingDelay = 4000
|
|
|
|
this.pendingTimeout = null
|
2022-02-24 00:52:21 +01:00
|
|
|
|
2023-09-04 18:50:55 +02:00
|
|
|
/** @type {string[]} */
|
2022-03-22 01:24:38 +01:00
|
|
|
this.ignoreDirs = []
|
2022-02-24 00:52:21 +01:00
|
|
|
this.disabled = false
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
get pendingFilePaths() {
|
|
|
|
return this.pendingFileUpdates.map(f => f.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildLibraryWatcher(library) {
|
|
|
|
if (this.libraryWatchers.find(w => w.id === library.id)) {
|
|
|
|
Logger.warn('[Watcher] Already watching library', library.name)
|
|
|
|
return
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-11-26 01:39:02 +01:00
|
|
|
Logger.info(`[Watcher] Initializing watcher for "${library.name}".`)
|
2023-09-04 18:50:55 +02:00
|
|
|
|
|
|
|
const folderPaths = library.folderPaths
|
2021-11-26 01:39:02 +01:00
|
|
|
folderPaths.forEach((fp) => {
|
|
|
|
Logger.debug(`[Watcher] Init watcher for library folder path "${fp}"`)
|
|
|
|
})
|
2023-09-04 18:50:55 +02:00
|
|
|
const watcher = new Watcher(folderPaths, {
|
2021-10-05 05:11:42 +02:00
|
|
|
ignored: /(^|[\/\\])\../, // ignore dotfiles
|
|
|
|
renameDetection: true,
|
|
|
|
renameTimeout: 2000,
|
|
|
|
recursive: true,
|
|
|
|
ignoreInitial: true,
|
|
|
|
persistent: true
|
|
|
|
})
|
|
|
|
watcher
|
|
|
|
.on('add', (path) => {
|
|
|
|
this.onNewFile(library.id, path)
|
|
|
|
}).on('change', (path) => {
|
|
|
|
// This is triggered from metadata changes, not what we want
|
|
|
|
// this.onFileUpdated(path)
|
|
|
|
}).on('unlink', path => {
|
|
|
|
this.onFileRemoved(library.id, path)
|
|
|
|
}).on('rename', (path, pathNext) => {
|
|
|
|
this.onRename(library.id, path, pathNext)
|
|
|
|
}).on('error', (error) => {
|
2021-10-06 04:10:49 +02:00
|
|
|
Logger.error(`[Watcher] ${error}`)
|
2021-10-05 05:11:42 +02:00
|
|
|
}).on('ready', () => {
|
2021-10-06 04:10:49 +02:00
|
|
|
Logger.info(`[Watcher] "${library.name}" Ready`)
|
2021-11-26 01:39:02 +01:00
|
|
|
}).on('close', () => {
|
|
|
|
Logger.debug(`[Watcher] "${library.name}" Closed`)
|
2021-10-05 05:11:42 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
this.libraryWatchers.push({
|
|
|
|
id: library.id,
|
|
|
|
name: library.name,
|
|
|
|
folders: library.folders,
|
|
|
|
paths: library.folderPaths,
|
|
|
|
watcher
|
|
|
|
})
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
initWatcher(libraries) {
|
|
|
|
libraries.forEach((lib) => {
|
2022-04-15 00:15:52 +02:00
|
|
|
if (!lib.settings.disableWatcher) {
|
2022-02-26 23:34:54 +01:00
|
|
|
this.buildLibraryWatcher(lib)
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
})
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
addLibrary(library) {
|
2022-04-15 00:15:52 +02:00
|
|
|
if (this.disabled || library.settings.disableWatcher) return
|
2021-10-05 05:11:42 +02:00
|
|
|
this.buildLibraryWatcher(library)
|
|
|
|
}
|
2021-09-11 02:55:02 +02:00
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
updateLibrary(library) {
|
2022-04-15 00:15:52 +02:00
|
|
|
if (this.disabled || library.settings.disableWatcher) return
|
2021-10-05 05:11:42 +02:00
|
|
|
var libwatcher = this.libraryWatchers.find(lib => lib.id === library.id)
|
|
|
|
if (libwatcher) {
|
|
|
|
libwatcher.name = library.name
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2022-03-14 15:56:24 +01:00
|
|
|
// If any folder paths were added or removed then re-init watcher
|
2021-10-05 05:11:42 +02:00
|
|
|
var pathsToAdd = library.folderPaths.filter(path => !libwatcher.paths.includes(path))
|
2022-03-14 15:56:24 +01:00
|
|
|
var pathsRemoved = libwatcher.paths.filter(path => !library.folderPaths.includes(path))
|
|
|
|
if (pathsToAdd.length || pathsRemoved.length) {
|
|
|
|
Logger.info(`[Watcher] Re-Initializing watcher for "${library.name}".`)
|
|
|
|
|
|
|
|
libwatcher.watcher.close()
|
|
|
|
this.libraryWatchers = this.libraryWatchers.filter(lw => lw.id !== libwatcher.id)
|
|
|
|
this.buildLibraryWatcher(library)
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
2021-09-07 03:14:04 +02:00
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
removeLibrary(library) {
|
2022-02-24 00:52:21 +01:00
|
|
|
if (this.disabled) return
|
2021-10-05 05:11:42 +02:00
|
|
|
var libwatcher = this.libraryWatchers.find(lib => lib.id === library.id)
|
|
|
|
if (libwatcher) {
|
|
|
|
Logger.info(`[Watcher] Removed watcher for "${library.name}"`)
|
|
|
|
libwatcher.watcher.close()
|
|
|
|
this.libraryWatchers = this.libraryWatchers.filter(lib => lib.id !== library.id)
|
|
|
|
} else {
|
|
|
|
Logger.error(`[Watcher] Library watcher not found for "${library.name}"`)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
close() {
|
|
|
|
return this.libraryWatchers.map(lib => lib.watcher.close())
|
|
|
|
}
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
onNewFile(libraryId, path) {
|
2022-03-22 01:24:38 +01:00
|
|
|
if (this.checkShouldIgnorePath(path)) {
|
|
|
|
return
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
Logger.debug('[Watcher] File Added', path)
|
|
|
|
this.addFileUpdate(libraryId, path, 'added')
|
|
|
|
}
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
onFileRemoved(libraryId, path) {
|
2022-03-22 01:24:38 +01:00
|
|
|
if (this.checkShouldIgnorePath(path)) {
|
|
|
|
return
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
Logger.debug('[Watcher] File Removed', path)
|
|
|
|
this.addFileUpdate(libraryId, path, 'deleted')
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onFileUpdated(path) {
|
2021-10-05 05:11:42 +02:00
|
|
|
Logger.debug('[Watcher] Updated File', path)
|
|
|
|
}
|
|
|
|
|
|
|
|
onRename(libraryId, pathFrom, pathTo) {
|
2022-03-22 01:24:38 +01:00
|
|
|
if (this.checkShouldIgnorePath(pathTo)) {
|
|
|
|
return
|
|
|
|
}
|
2021-10-05 05:11:42 +02:00
|
|
|
Logger.debug(`[Watcher] Rename ${pathFrom} => ${pathTo}`)
|
|
|
|
this.addFileUpdate(libraryId, pathTo, 'renamed')
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-08-26 00:36:54 +02:00
|
|
|
|
2023-09-04 18:50:55 +02:00
|
|
|
/**
|
|
|
|
* File update detected from watcher
|
|
|
|
* @param {string} libraryId
|
|
|
|
* @param {string} path
|
|
|
|
* @param {string} type
|
|
|
|
*/
|
2021-10-05 05:11:42 +02:00
|
|
|
addFileUpdate(libraryId, path, type) {
|
2023-01-06 00:45:27 +01:00
|
|
|
path = filePathToPOSIX(path)
|
2021-10-05 05:11:42 +02:00
|
|
|
if (this.pendingFilePaths.includes(path)) return
|
2021-09-07 03:14:04 +02:00
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
// Get file library
|
2023-01-06 00:45:27 +01:00
|
|
|
const libwatcher = this.libraryWatchers.find(lw => lw.id === libraryId)
|
2021-10-05 05:11:42 +02:00
|
|
|
if (!libwatcher) {
|
|
|
|
Logger.error(`[Watcher] Invalid library id from watcher ${libraryId}`)
|
2021-09-11 02:55:02 +02:00
|
|
|
return
|
2021-09-07 03:14:04 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
// Get file folder
|
2023-01-06 00:45:27 +01:00
|
|
|
const folder = libwatcher.folders.find(fold => path.startsWith(filePathToPOSIX(fold.fullPath)))
|
2021-10-05 05:11:42 +02:00
|
|
|
if (!folder) {
|
|
|
|
Logger.error(`[Watcher] New file folder not found in library "${libwatcher.name}" with path "${path}"`)
|
|
|
|
return
|
|
|
|
}
|
2023-09-08 21:50:59 +02:00
|
|
|
|
2023-01-06 00:45:27 +01:00
|
|
|
const folderFullPath = filePathToPOSIX(folder.fullPath)
|
2021-10-05 05:11:42 +02:00
|
|
|
|
2023-09-04 18:50:55 +02:00
|
|
|
const relPath = path.replace(folderFullPath, '')
|
2021-11-06 23:26:44 +01:00
|
|
|
|
2023-09-08 21:50:59 +02:00
|
|
|
if (Path.extname(relPath).toLowerCase() === '.part') {
|
|
|
|
Logger.debug(`[Watcher] Ignoring .part file "${relPath}"`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-04 18:50:55 +02:00
|
|
|
// Ignore files/folders starting with "."
|
|
|
|
const hasDotPath = relPath.split('/').find(p => p.startsWith('.'))
|
2021-11-06 23:26:44 +01:00
|
|
|
if (hasDotPath) {
|
|
|
|
Logger.debug(`[Watcher] Ignoring dot path "${relPath}" | Piece "${hasDotPath}"`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-06 04:10:49 +02:00
|
|
|
Logger.debug(`[Watcher] Modified file in library "${libwatcher.name}" and folder "${folder.id}" with relPath "${relPath}"`)
|
2021-10-05 05:11:42 +02:00
|
|
|
|
|
|
|
this.pendingFileUpdates.push({
|
|
|
|
path,
|
|
|
|
relPath,
|
|
|
|
folderId: folder.id,
|
|
|
|
libraryId,
|
|
|
|
type
|
|
|
|
})
|
|
|
|
|
|
|
|
// Notify server of update after "pendingDelay"
|
2021-09-11 02:55:02 +02:00
|
|
|
clearTimeout(this.pendingTimeout)
|
|
|
|
this.pendingTimeout = setTimeout(() => {
|
2023-09-04 18:50:55 +02:00
|
|
|
// this.emit('files', this.pendingFileUpdates)
|
|
|
|
LibraryScanner.scanFilesChanged(this.pendingFileUpdates)
|
2021-10-05 05:11:42 +02:00
|
|
|
this.pendingFileUpdates = []
|
2021-09-11 02:55:02 +02:00
|
|
|
}, this.pendingDelay)
|
2021-08-26 00:36:54 +02:00
|
|
|
}
|
2022-03-22 01:24:38 +01:00
|
|
|
|
|
|
|
checkShouldIgnorePath(path) {
|
|
|
|
return !!this.ignoreDirs.find(dirpath => {
|
2023-01-06 00:45:27 +01:00
|
|
|
return filePathToPOSIX(path).startsWith(dirpath)
|
2022-03-22 01:24:38 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanDirPath(path) {
|
2023-01-06 00:45:27 +01:00
|
|
|
path = filePathToPOSIX(path)
|
2022-03-22 01:24:38 +01:00
|
|
|
if (path.endsWith('/')) path = path.slice(0, -1)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
addIgnoreDir(path) {
|
|
|
|
path = this.cleanDirPath(path)
|
|
|
|
if (this.ignoreDirs.includes(path)) return
|
|
|
|
Logger.debug(`[Watcher] Ignoring directory "${path}"`)
|
|
|
|
this.ignoreDirs.push(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
removeIgnoreDir(path) {
|
|
|
|
path = this.cleanDirPath(path)
|
|
|
|
if (!this.ignoreDirs.includes(path)) return
|
|
|
|
Logger.debug(`[Watcher] No longer ignoring directory "${path}"`)
|
|
|
|
this.ignoreDirs = this.ignoreDirs.filter(p => p !== path)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = FolderWatcher
|