* * @param {import('./models/Library')} library
(library)
| 51 | * @param {import('./models/Library')} library |
| 52 | */ |
| 53 | buildLibraryWatcher(library) { |
| 54 | if (this.libraryWatchers.find((w) => w.id === library.id)) { |
| 55 | Logger.warn('[Watcher] Already watching library', library.name) |
| 56 | return |
| 57 | } |
| 58 | Logger.info(`[Watcher] Initializing watcher for "${library.name}".`) |
| 59 | |
| 60 | const folderPaths = library.libraryFolders.map((f) => f.path) |
| 61 | folderPaths.forEach((fp) => { |
| 62 | Logger.debug(`[Watcher] Init watcher for library folder path "${fp}"`) |
| 63 | }) |
| 64 | const watcher = new Watcher(folderPaths, { |
| 65 | ignored: /(^|[\/\\])\../, // ignore dotfiles |
| 66 | renameDetection: true, |
| 67 | renameTimeout: 2000, |
| 68 | recursive: true, |
| 69 | ignoreInitial: true, |
| 70 | persistent: true |
| 71 | }) |
| 72 | watcher |
| 73 | .on('add', (path) => { |
| 74 | this.onFileAdded(library.id, filePathToPOSIX(path)) |
| 75 | }) |
| 76 | .on('change', (path) => { |
| 77 | // This is triggered from metadata changes, not what we want |
| 78 | }) |
| 79 | .on('unlink', (path) => { |
| 80 | this.onFileRemoved(library.id, filePathToPOSIX(path)) |
| 81 | }) |
| 82 | .on('rename', (path, pathNext) => { |
| 83 | this.onFileRename(library.id, filePathToPOSIX(path), filePathToPOSIX(pathNext)) |
| 84 | }) |
| 85 | .on('error', (error) => { |
| 86 | Logger.error(`[Watcher] ${error}`) |
| 87 | }) |
| 88 | .on('ready', () => { |
| 89 | Logger.info(`[Watcher] "${library.name}" Ready`) |
| 90 | }) |
| 91 | .on('close', () => { |
| 92 | Logger.debug(`[Watcher] "${library.name}" Closed`) |
| 93 | }) |
| 94 | |
| 95 | this.libraryWatchers.push({ |
| 96 | id: library.id, |
| 97 | name: library.name, |
| 98 | libraryFolders: library.libraryFolders, |
| 99 | paths: folderPaths, |
| 100 | watcher |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * |
no test coverage detected