-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (112 loc) · 3.29 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
'use strict';
import path from "path";
import fs from "fs";
import exifr from "exifr";
import moment from "moment";
import ffmpeg from "fluent-ffmpeg";
function chkDir(p) {
if (!fs.existsSync(p)) {
fs.mkdirSync(p);
}
}
function getMetadataFromVideo(srcPath) {
return new Promise(function(resolve, reject) {
ffmpeg.ffprobe(srcPath, function(err, metadata) {
if (err) {
reject(err);
return;
}
resolve(metadata);
});
});
}
function copy(filePath, dirPath) {
let filename = path.basename(filePath);
let dstPath = path.join(dirPath, filename);
let index = 0;
while(fs.existsSync(dstPath)) {
index++;
filename = `${path.basename(filePath, path.extname(filePath))} (${index})${path.extname(filePath)}`;
dstPath = path.join(dirPath, filename);
}
fs.copyFileSync(filePath, dstPath);
}
function readDirs(dirPath) {
return fs.readdirSync(dirPath).reduce(function(prev, curr) {
return fs.statSync(path.join(dirPath, curr)).isFile() ?
[path.join(dirPath, curr), ...prev] :
[...prev, ...readDirs(path.join(dirPath, curr))]
}, []);
}
async function exec(from, to) {
let files = readDirs(from);
const images = files.filter(function(file) {
return [".jpeg", ".jpg", ".tiff", ".heif", ".heic", ".avif", ".png"].indexOf(path.extname(file).toLowerCase()) > -1;
});
const videos = files.filter(function(file) {
return [".mov", ".mp4"].indexOf(path.extname(file).toLowerCase()) > -1;
});
const errorDir = path.join(to, "error");
chkDir(to);
for (const filePath of images) {
try {
const buffer = fs.readFileSync(filePath);
const exif = await exifr.parse(buffer);
if (!exif) {
throw new Error("Metadata not found.");
}
const timestamp = exif.DateTimeOriginal || exif.CreateDate || exif.ModifyDate;
const createdAt = fs.statSync(filePath).birthtime;
if (!timestamp) {
throw new Error("Date not found.");
}
const date = moment(timestamp);
const y = date.get("year");
const m = date.get("month") + 1;
const d = date.get("date");
const yearDir = path.join(to, ""+y);
const monthDir = path.join(to, ""+y, (""+m).padStart(2, "0"));
chkDir(yearDir);
chkDir(monthDir);
copy(filePath, monthDir);
} catch(err) {
console.error(err);
chkDir(errorDir);
copy(filePath, errorDir);
}
}
for (const filePath of videos) {
try {
const metadata = await getMetadataFromVideo(filePath);
if (!metadata) {
throw new Error("Metadata not found.");
}
const timestamp = metadata.format.tags.creation_time;
if (!timestamp) {
throw new Error("Date not found.");
}
const date = moment(timestamp);
const y = date.get("year");
const m = date.get("month") + 1;
const d = date.get("date");
const yearDir = path.join(to, ""+y);
const monthDir = path.join(to, ""+y, (""+m).padStart(2, "0"));
chkDir(yearDir);
chkDir(monthDir);
copy(filePath, monthDir);
} catch(err) {
console.error(err);
chkDir(errorDir);
copy(filePath, errorDir);
}
}
}
const __module__ = {
exec,
}
// esm
export default __module__;
// cjs
// module.exports = __module__;
// browser
// window.jsm = __module__;