-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (60 loc) · 1.82 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var options = require('./stores/config.json');
var watchDir = options.watchDir;
var movieDir = options.movieDir;
var musicDir = options.musicDir;
var imageDir = options.imageDir;
var otherDir = options.otherDir;
var mime = require('mime');
//Initialize mv module to move files around
var mv = require('mv');
//Initialize Chokidar, a file watcher
var chokidar = require('chokidar').watch(watchDir, {
persistent: true,
ignoreInitial: true
});
//Registering ADD event of chokidar to perform certain task
chokidar.on('add', function(path, event) {
var mimeType = mime.lookup(path);
doCatagorize(path,mimeType);
});
function doCatagorize(path,mimeType) {
//Check if File type is Video
var pathArray = path.split('/');
var fileName = pathArray[pathArray.length - 1 ];
if(matchExpression(mimeType,'video*')){
mv(path, movieDir + fileName,{mkdirp:true},function(err){
if(err) {
throw err;
}
});
}
//Check if File type is Audio
else if(matchExpression(mimeType,'audio*')){
mv(path, musicDir + fileName,{mkdirp:true},function(err){
if(err) {
throw err;
}
});
}
//Check if File type is Image
else if(matchExpression(mimeType,'image*')){
mv(path, imageDir + fileName,{mkdirp:true},function(err){
if(err) {
throw err;
}
});
}
//Do for the rest of the files
else {
mv(path, otherDir + fileName,{mkdirp:true},function(err){
if(err) {
throw err;
}
});
}
console.log('Moving :'+ path + ' to '+ movieDir + fileName);
console.log('Mime type :' + mimeType);
}
function matchExpression(str, expression) {
return new RegExp("^" + expression.split("*").join(".*") + "$").test(str);
}