-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (89 loc) · 3.13 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
const path = require('path');
const fs = require('fs');
const exiftool = require('node-exiftool');
const exifProcess = new exiftool.ExiftoolProcess();
const imagesDir = path.resolve(__dirname, 'images/');
const imageFilePath = (imageName) => path.join(imagesDir, imageName);
/**
* Object containing the date that will get replaced
* To see what data you can replace use exifProcess.readMetadata(pathToImage: string, ['-File:all'])
*/
const dataToReplace = {
ModifyDate: '',
CreateDate: '',
DateTimeOriginal: '',
}
/**
* Starting date that gets modified while the app is running for each next image file.
*/
const currentDate = {
year: 2018,
month: 5,
day: 11,
hour: 8,
minute: 0,
second: 0,
}
/**
* @param {Date} date
* @returns {string} 'YYYY:MM:DD HH:mm:ss'
*/
const formatExifDate = date => {
let year = "" + date.getFullYear();
let month = "" + (date.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
let day = "" + date.getDate(); if (day.length == 1) { day = "0" + day; }
let hour = "" + date.getHours(); if (hour.length == 1) { hour = "0" + hour; }
let minute = "" + date.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
let second = "" + date.getSeconds(); if (second.length == 1) { second = "0" + second; }
return year + ":" + month + ":" + day + " " + hour + ":" + minute + ":" + second;
}
const generateNextDate = () => {
const date = new Date(
currentDate.year, currentDate.month - 1, currentDate.day,
currentDate.hour, currentDate.minute, currentDate.second
);
if (++currentDate.second >= 60) {
currentDate.second = 0;
currentDate.minute++;
}
if (currentDate.minute >= 60) {
currentDate.minute = 0;
currentDate.hour++;
}
return formatExifDate(date);
}
/**
* By default filenames are sorted like: ['img_1', 'img_10', 'img_2', 'img_3', ...]
* This function will sort the array properly like: ['img_1', 'img_2', ..., 'img_10']
*/
const fileNamesNaturalSort = (a, b) => {
return (Number(a.match(/(\d+)/g)[0]) - Number((b.match(/(\d+)/g)[0])));
}
const getImages = () => new Promise((resolve, reject) => {
fs.readdir(imagesDir, (error, files) => {
if (error) reject(error);
resolve(files.sort(fileNamesNaturalSort));
})
});
const replaceDates = (exifProcess, imagesArray, index) => new Promise((resolve, reject) => {
dataToReplace.DateTimeOriginal = dataToReplace.CreateDate = dataToReplace.ModifyDate = generateNextDate();
exifProcess.writeMetadata(imageFilePath(imagesArray[index]), dataToReplace, ['overwrite_original'])
.then(() => {
index++;
console.log(`Done: ${index} of ${imagesArray.length}`);
if (imagesArray.length === index) {
resolve();
} else {
replaceDates(exifProcess, imagesArray, index).then(resolve).catch(reject);
}
})
.catch(reject);
});
exifProcess.open()
.then(getImages)
// .then((imagesArray) => exifProcess.readMetadata(imagesArray[0], ['-File:all'])) // read data of first file
// .then(console.log) // log the data to the console
.then((imagesArray) => replaceDates(exifProcess, imagesArray, 0)) // This will write exif data to your files.
.then(() => exifProcess.close())
.then(() => console.log('ALL DONE!'))
.catch(console.error);