added config parameter 'initialscan' to suppress initial 'add' events
This commit is contained in:
parent
13f5ef0ede
commit
83a4f9b8e8
2 changed files with 50 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const chokidar = require('chokidar');
|
const chokidar = require('chokidar');
|
||||||
|
const { open, writeFile } = require('fs/promises');
|
||||||
|
|
||||||
class Watcher {
|
class Watcher {
|
||||||
|
|
||||||
|
@ -7,17 +8,27 @@ class Watcher {
|
||||||
this.#initialize();
|
this.#initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
#initialize() {
|
async #initialize() {
|
||||||
if (config?.library === undefined) {
|
if (config?.library === undefined) {
|
||||||
throw new Error('library not defined');
|
throw new Error('library not defined');
|
||||||
}
|
}
|
||||||
if (config?.library?.sources === undefined || config?.library?.sources.length === 0) {
|
if (config?.library?.sources === undefined || config?.library?.sources.length === 0) {
|
||||||
throw new Error('no library sources defined');
|
throw new Error('no library sources defined');
|
||||||
}
|
}
|
||||||
|
let initialScan = config?.library?.initialscan?.enabled;
|
||||||
|
if (initialScan === undefined) {
|
||||||
|
initialScan = true;
|
||||||
|
}
|
||||||
for (let index = 0; index < config.library.sources.length; index++) {
|
for (let index = 0; index < config.library.sources.length; index++) {
|
||||||
const directory = path.resolve(config.library.sources[index]);
|
const directory = path.resolve(config.library.sources[index]);
|
||||||
|
let ignoreInitial = !initialScan;
|
||||||
|
if (ignoreInitial === false) {
|
||||||
|
ignoreInitial = (new Date().getTime() - await this.#checkHiddenFile(directory)) < (config?.library?.initialscan?.maxage || 86400000);
|
||||||
|
}
|
||||||
logger.debug('watching directory \'' + directory + '\'...');
|
logger.debug('watching directory \'' + directory + '\'...');
|
||||||
this.#handleEvents(chokidar.watch(directory));
|
this.#handleEvents(chokidar.watch(directory, {
|
||||||
|
ignoreInitial: ignoreInitial
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +46,38 @@ class Watcher {
|
||||||
queue.add(constants.FS_EVENT_CHANGE, file, stats);
|
queue.add(constants.FS_EVENT_CHANGE, file, stats);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async #checkHiddenFile(directory) {
|
||||||
|
if (directory === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const hiddenFile = path.join(directory, '.kannon');
|
||||||
|
let filehandle;
|
||||||
|
try {
|
||||||
|
filehandle = await open(hiddenFile);
|
||||||
|
return (await filehandle.readFile()).toString();
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === 'ENOENT') {
|
||||||
|
return await this.#createHiddenFile(hiddenFile);
|
||||||
|
}
|
||||||
|
logger.error('encountered an error checking the hidden file \'' + hiddenFile + '\' > ' + error);
|
||||||
|
} finally {
|
||||||
|
filehandle?.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async #createHiddenFile(file) {
|
||||||
|
if (file === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const timestamp = new Date().getTime().toString();
|
||||||
|
try {
|
||||||
|
await writeFile(file, timestamp);
|
||||||
|
return timestamp;
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('encountered an error writing the hidden file \'' + file + '\' > ' + error);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Watcher;
|
module.exports = Watcher;
|
|
@ -22,7 +22,11 @@
|
||||||
"formats": [
|
"formats": [
|
||||||
"mp3",
|
"mp3",
|
||||||
"flac"
|
"flac"
|
||||||
]
|
],
|
||||||
|
"initialscan": {
|
||||||
|
"enabled": false,
|
||||||
|
"maxage": 3600000
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"database": {
|
"database": {
|
||||||
"dialect": "postgres",
|
"dialect": "postgres",
|
||||||
|
|
Loading…
Reference in a new issue