-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·63 lines (49 loc) · 1.59 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
#!/usr/bin/env node
const exifParser = require('exif-parser');
const fs = require('fs');
const glob = require('glob');
const minimist = require('minimist');
const path = require('path');
function formatDate(timestamp) {
const date = new Date(timestamp * 1000);
const year = date.getFullYear();
const month = `${date.getMonth() + 1}`.padStart(2, '0');
const day = date.getDate();
const hour = `${date.getHours()}`.padStart(2, '0');
const minute = `${date.getMinutes()}`.padStart(2, '0');
const second = `${date.getSeconds()}`.padStart(2, '0');
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
function renameFile(filePath, args) {
if (!fs.existsSync(filePath)) return;
try {
const fileBuffer = fs.readFileSync(filePath);
const parser = exifParser.create(fileBuffer);
const exifData = parser.parse();
const exifDate = exifData.tags.DateTimeOriginal;
if (!exifDate) return;
const parsedPath = path.parse(filePath);
const newPath = path.format({
dir: parsedPath.dir,
ext: parsedPath.ext,
name: args.format ? formatDate(exifDate) : exifDate,
});
fs.renameSync(filePath, newPath);
console.log(`${filePath} -> ${newPath}`);
} catch (error) {
console.log(error);
}
}
function main() {
const args = minimist(process.argv.slice(2));
const paths = glob
.sync(args._.join(','), { nodir: true })
.map((filePath) => path.resolve(filePath));
if (!paths.length) {
console.log('No paths specified.');
return;
}
paths.forEach((filePath) => renameFile(filePath, args));
console.log('Done.');
}
main();