-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
170 lines (119 loc) · 5.95 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
let fs = require("fs");
var path = require('path');
let dirTree = require("directory-tree");
let fileType = require('file-type');
const fsPromises = require("fs").promises;
const { cpuUsage } = require("process");
var dirFiles = 0;
var dirToSendMovies = "";
var dirToSendSeries = "";
var dirToSendAudio = "";
var args = process.argv.slice(2);
var pathToSort = args[0];
var pathOfFileOrFolder = pathToSort.split("\\");
var nameOfFileOrFolder = pathOfFileOrFolder[pathOfFileOrFolder.length - 1];
var extToDel = ['.txt', '.nfo', '.url', '.lnk'];
//Trying pull requests
var stats = fs.statSync(pathToSort);
if(stats.isFile()) {
//It's a movie (or an episode of a serie) or an audio file
console.log("DETECTED : Movie or 1 music");
(async () => {
var isAFileToCopy = true;
var type = await fileType.fromFile(pathToSort);
if(type.mime.toString().includes("audio")){
//It's an audio file
console.log("DETECTED : Music");
fs.copyFile(pathToSort, dirToSendAudio + "\\" + nameOfFileOrFolder, (err) => {
if (err) throw err;
console.log(nameOfFileOrFolder + " was copied to " + dirToSendAudio);
});
} else if(type.mime.toString().includes("video")) {
//It's a movies
console.log("DETECTED : Movie")
fs.copyFile(pathToSort, dirToSendMovies + "\\" + nameOfFileOrFolder, (err) => {
if (err) throw err;
console.log(nameOfFileOrFolder + " was copied to " + dirToSendMovies);
});
} else {
console.log("Meh.")
}
})();
} else if(stats.isDirectory()) {
var audios = 0;
var videos = 0;
//It's a serie season or an audio folder
console.log("DETECTED : Serie season or audio folder")
var tree = dirTree(pathToSort);
dirFiles = tree.children.length;
if(dirFiles === 0) {
console.log("ERROR : Folder is empty");
} else {
var childrenTree = tree.children;
for(let i = 0 ; i < dirFiles ; i++) {
console.log(childrenTree[i].name);
(async () => {
var isAFileToCopy = true;
var type = await fileType.fromFile(pathToSort + "\\" + childrenTree[i].name);
for(var j = 0 ; j < extToDel.length ; j++) {
if(path.extname(pathToSort + "\\" + childrenTree[i].name) === extToDel[j]) {
isAFileToCopy = false;
console.log("DETECTED : extToDel");
}
}
if(isAFileToCopy) {
if(type.mime.toString().includes("audio")){
//It's an audio file
console.log("DETECTED : Audio")
if(checkFileExists(dirToSendAudio + "\\" + nameOfFileOrFolder)){
fsPromises.mkdir(dirToSendAudio + "\\" + nameOfFileOrFolder).then(function() {
}).catch(function() {
});
fs.copyFile(pathToSort + "\\" + childrenTree[i].name, dirToSendAudio + "\\" + nameOfFileOrFolder + "\\" + childrenTree[i].name, (err) => {
if (err) throw err;
console.log(childrenTree[i].name + " was copied to " + dirToSendAudio + "\\" + nameOfFileOrFolder);
});
} else {
fs.copyFile(pathToSort + "\\" + childrenTree[i].name, dirToSendAudio + "\\" + nameOfFileOrFolder + "\\" + childrenTree[i].name, (err) => {
if (err) throw err;
console.log(childrenTree[i].name + " was copied to " + dirToSendAudio + "\\" + nameOfFileOrFolder);
});
}
} else if(type.mime.toString().includes("video")) {
//It's a serie
console.log("DETECTED : Video");
if(checkFileExists(dirToSendSeries + "\\" + nameOfFileOrFolder)){
fsPromises.mkdir(dirToSendSeries + "\\" + nameOfFileOrFolder).then(function() {
}).catch(function() {
});
fs.copyFile(pathToSort + "\\" + childrenTree[i].name, dirToSendSeries + "\\" + nameOfFileOrFolder + "\\" + childrenTree[i].name, (err) => {
if (err) throw err;
console.log(childrenTree[i].name + " was copied to " + dirToSendSeries + "\\" + nameOfFileOrFolder);
});
} else {
fs.copyFile(pathToSort + "\\" + childrenTree[i].name, dirToSendSeries + "\\" + nameOfFileOrFolder + "\\" + childrenTree[i].name, (err) => {
if (err) throw err;
console.log(childrenTree[i].name + " was copied to " + dirToSendSeries + "\\" + nameOfFileOrFolder);
});
}
} else {
console.log("Meh.");
}
}
})();
}
}
}
async function checkFileExists(file) {
return fs.promises.access(file, fs.constants.F_OK)
.then(() => true)
.catch(() => false)
}
async function createDir(dir) {
try {
await fsPromises.access(dir, fs.constants.F_OK);
} catch (e) {
await fsPromises.mkdir(dir);
}
}