-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (60 loc) · 2.15 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
const mdls = require('mdls');
const { getFilePaths, moveFile, ensureExistingFolder } = require('./utils');
const [inputFolder, outputFolder, uniqueName] = process.argv.slice(2);
console.log(`target folder: "${inputFolder}"`);
console.log(`output folder: "${outputFolder}"`);
function exit(code = 0) {
process.exit(code);
}
if (!inputFolder ||
!outputFolder ||
!ensureExistingFolder(inputFolder, false) ||
!ensureExistingFolder(outputFolder, true)) {
return exit(1); // exit missing crucial info
}
const files = getFilePaths(inputFolder);
console.log(`found ${files.length} files:`);
console.dir(files);
if (files.length <= 0) { // exit no files to process
return exit();
}
async function processFiles(files) {
const addIndexPrefix = uniqueName && (
// is number and equals 1
(!isNaN(uniqueName) && uniqueName === '1') ||
// is non-number and 'true'
uniqueName.toLowerCase() === 'true'
);
const batchSize = 10;
const N = Math.ceil(files.length / batchSize);
for (let i = 0; i < N; i++) {
const start = i * batchSize;
const end = start + batchSize;
const batch = files.slice(start, end);
const batchTasks = batch.map(async (file, idx) => {
try {
const metadata = await mdls(file);
const isAppleFile = (
// MOV file
metadata['ItemContentType'] === 'com.apple.quicktime-movie'
// iPhone created-by content
|| (metadata['ItemAcquisitionMake'] === 'Apple'
&& metadata['ItemAcquisitionModel'].toLowerCase().startsWith('iphone'))
);
if (isAppleFile) {
return moveFile(file, outputFolder, addIndexPrefix ? `${start}_${idx}_` : null);
}
} catch (err) {
console.error(`mdls error for file: "${file}", err=${err}`);
return file;
}
});
const batchResults = await Promise.all(batchTasks);
}
}
processFiles(files).then((err) => {
if (err) {
console.error(err);
}
console.log('done')
});