|
|
@@ -44,16 +44,16 @@ const help = () => {
|
|
|
return console.log(message);
|
|
|
};
|
|
|
|
|
|
-String.prototype.toTitleCase = function() {
|
|
|
+String.prototype.toTitleCase = function () {
|
|
|
var i, j, str, lowers, uppers;
|
|
|
- str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
|
|
|
+ str = this.replace(/([^\W_]+[^\s-]*) */g, function (txt) {
|
|
|
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
|
|
});
|
|
|
// Certain minor words should be left lowercase unless
|
|
|
// they are the first or last words in the string
|
|
|
lowers = (settings && settings.titlecase && settings.titlecase.lowers) || [];
|
|
|
for (i = 0, j = lowers.length; i < j; i++)
|
|
|
- str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), function(txt) {
|
|
|
+ str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), function (txt) {
|
|
|
return txt.toLowerCase();
|
|
|
});
|
|
|
// Certain words such as initialisms or acronyms should be left uppercase
|
|
|
@@ -82,11 +82,7 @@ const findFile = (db, file) => {
|
|
|
|
|
|
// set a file in the db
|
|
|
const setFile = (db, file, payload) => {
|
|
|
- if (!payload && typeof file === 'object')
|
|
|
- return db
|
|
|
- .get('files')
|
|
|
- .push(file)
|
|
|
- .write();
|
|
|
+ if (!payload && typeof file === 'object') return db.get('files').push(file).write();
|
|
|
return db
|
|
|
.get('files')
|
|
|
.find({
|
|
|
@@ -104,7 +100,7 @@ const removeFile = (db, file, soft = true) => {
|
|
|
.find({
|
|
|
input: file,
|
|
|
})
|
|
|
- .assign({ status:"deleted", date: new Date() })
|
|
|
+ .assign({ status: 'deleted', date: new Date() })
|
|
|
.write(); // remove file from database
|
|
|
return db
|
|
|
.get('files')
|
|
|
@@ -115,13 +111,13 @@ const removeFile = (db, file, soft = true) => {
|
|
|
};
|
|
|
|
|
|
// write process output
|
|
|
-const processOutput = str => {
|
|
|
+const processOutput = (str) => {
|
|
|
process.stdout.clearLine();
|
|
|
process.stdout.cursorTo(0);
|
|
|
process.stdout.write(str);
|
|
|
};
|
|
|
|
|
|
-const getFilesize = async file => {
|
|
|
+const getFilesize = async (file) => {
|
|
|
let stats = fs.existsSync(file) ? fs.statSync(file) : false;
|
|
|
let bytes = stats ? stats['size'] : 0;
|
|
|
return bytes && bytes > 0 ? bytes / 1024 : 0;
|
|
|
@@ -138,14 +134,14 @@ const processWithHandbrake = (input, output, preset) => {
|
|
|
output: output,
|
|
|
preset: preset,
|
|
|
})
|
|
|
- .on('start', err => {
|
|
|
+ .on('start', (err) => {
|
|
|
processOutput(` -> "${outputName}" [starting] (${new Date()} with "${preset}")`);
|
|
|
})
|
|
|
- .on('error', err => {
|
|
|
+ .on('error', (err) => {
|
|
|
processOutput(` -> "${outputName}" [errored] (${new Date()}: ${err.message || err})\n`);
|
|
|
reject(err);
|
|
|
})
|
|
|
- .on('progress', progress => {
|
|
|
+ .on('progress', (progress) => {
|
|
|
processOutput(` -> "${outputName}" [transcoding] (${progress.percentComplete}%, ETA: ${progress.eta})`);
|
|
|
})
|
|
|
.on('cancelled', () => {
|
|
|
@@ -159,7 +155,7 @@ const processWithHandbrake = (input, output, preset) => {
|
|
|
});
|
|
|
};
|
|
|
|
|
|
-const processFile = async file => {
|
|
|
+const processFile = async (file) => {
|
|
|
let db,
|
|
|
options,
|
|
|
preset = 'Fast 1080p30',
|
|
|
@@ -234,7 +230,7 @@ const processFile = async file => {
|
|
|
if (fs.existsSync(output)) {
|
|
|
let outputSize = await getFilesize(output); //get output filesize
|
|
|
let inputSize = await getFilesize(input); //get output filesize
|
|
|
- if (outputSize > inputSize/4) {
|
|
|
+ if (outputSize > inputSize / 4) {
|
|
|
//make sure its bigger than 100k
|
|
|
processOutput(` -> "${path.basename(file)}" [skipping] (already processed)\n`);
|
|
|
setFile(db, file, {
|
|
|
@@ -279,7 +275,7 @@ const processFile = async file => {
|
|
|
};
|
|
|
|
|
|
// cleanup removes from the db
|
|
|
-const cleanup = file => {
|
|
|
+const cleanup = (file) => {
|
|
|
let db;
|
|
|
for (let i = 0, l = dirs.length; i < l; i++) {
|
|
|
let dir = dirs[i]; // pointer to the dir
|
|
|
@@ -315,7 +311,7 @@ const opts = {
|
|
|
ignoreInitial: ignoreInitial,
|
|
|
persistent: true,
|
|
|
usePolling: true,
|
|
|
- interval: 10*1000,
|
|
|
+ interval: 10 * 1000,
|
|
|
depth: 1,
|
|
|
awaitWriteFinish: {
|
|
|
stabilityThreshold: 3000,
|
|
|
@@ -361,20 +357,20 @@ const main = async () => {
|
|
|
const watcher = chokidar.watch(watches, opts); // init our watcher
|
|
|
console.log('Watching:', watches);
|
|
|
watcher
|
|
|
- .on('add', async file => {
|
|
|
+ .on('add', async (file) => {
|
|
|
// when a file is added ...
|
|
|
queue.push(file); // push the file onto the queue to be processed
|
|
|
})
|
|
|
- .on('change', file => {
|
|
|
+ .on('change', (file) => {
|
|
|
// when a file changes ...
|
|
|
console.log(` -> "${file}" [changed] (${new Date()})`);
|
|
|
})
|
|
|
- .on('unlink', async file => {
|
|
|
+ .on('unlink', async (file) => {
|
|
|
// when a file is removed ...
|
|
|
console.log(` -> "${file}" [removed] (${new Date()})`);
|
|
|
cleanup(file);
|
|
|
})
|
|
|
- .on('error', error => {
|
|
|
+ .on('error', (error) => {
|
|
|
console.error(` -> "${file}" [errored] (${new Date()}: ${error.message || error})`);
|
|
|
});
|
|
|
};
|
|
|
@@ -386,9 +382,11 @@ const purge = () => {
|
|
|
for (let i = 0, l = dirs.length; i < l; i++) {
|
|
|
let dir = dirs[i]; // pointer to the dir
|
|
|
db = getDbForDir(dir); // init the db connection
|
|
|
- let files = db.get('files').filter(file => file.status && file.status === "deleted" && file.processed && file.date && new Date(file.date).getTime() < ago.getTime());
|
|
|
+ let files = db
|
|
|
+ .get('files')
|
|
|
+ .filter((file) => file.status && file.status === 'deleted' && file.processed && file.date && new Date(file.date).getTime() < ago.getTime());
|
|
|
for (let file of files) {
|
|
|
- console.log(` --> purging`,file.input,`(${new Date()})`);
|
|
|
+ console.log(` --> purging`, file.input, `(${new Date()})`);
|
|
|
if (file && file.input) removeFile(db, file.input, false);
|
|
|
}
|
|
|
}
|
|
|
@@ -397,10 +395,27 @@ const purge = () => {
|
|
|
}, CLEANER);
|
|
|
};
|
|
|
|
|
|
+const prune = () => {
|
|
|
+ let db;
|
|
|
+ console.log(` -> Checking for any "processed" files that need pruning.`);
|
|
|
+ for (let i = 0, l = dirs.length; i < l; i++) {
|
|
|
+ let dir = dirs[i]; // pointer to the dir
|
|
|
+ db = getDbForDir(dir); // init the db connection
|
|
|
+ let files = db.get('files').filter((file) => file && file.processed);
|
|
|
+ for (let file of files) {
|
|
|
+ let exists = fs.existsSync(file.input); // check if the file actually exists
|
|
|
+ if (!exists) {
|
|
|
+ console.log(` --> pruning`, file.input, `(${new Date()})`);
|
|
|
+ removeFile(db, file.input, false); //soft remove
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
|
|
|
(async () => {
|
|
|
main();
|
|
|
purge();
|
|
|
-})().catch(err => {
|
|
|
+ prune();
|
|
|
+})().catch((err) => {
|
|
|
console.log(` -> Error: ${err.message || err}`);
|
|
|
});
|