-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (39 loc) · 1.05 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const DOWNLOAD_DIR = 'PASTE_YOUR_DOWNLOAD_DIR_PATH_HERE';
const DEST_DIR = 'PASTE_DESTINATION_DIR_PATH';
const fs = require('fs');
const path = require('path');
let alreadyExistsFiles = [];
const watcher = require('watcher');
let watch = new watcher(DOWNLOAD_DIR);
watch.on('ready', () => {
fs.readdir(DOWNLOAD_DIR, (err, files) => {
if (err) throw err;
files = files.map(file => {
return DOWNLOAD_DIR+'\\'+file;
})
alreadyExistsFiles = files;
})
});
watch.on('add', (file) => {
if(!ifAlreadyExists(file)){
if(file.endsWith('.tmp')){
return;
}
let destPath = path.join(DEST_DIR, path.basename(file));
moveTo(file, destPath);
}
});
function ifAlreadyExists(file){
if(alreadyExistsFiles.includes(file)){
return true;
}
return false;
}
function moveTo(from, to){
fs.copyFile( from, to, (err) => {
if (err) throw err;
fs.unlink(from, (err) => {
console.log(`${from} moved to ${to}`);
})
})
}