-
Notifications
You must be signed in to change notification settings - Fork 1
/
exif.js
73 lines (64 loc) · 2.36 KB
/
exif.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
const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
const ExifTransformer = require('exif-be-gone')
const directoryPath = path.join(__dirname, '.');
function removeExifData(filePath) {
const tempFilePath = `${filePath}.tmp`;
const reader = fs.createReadStream(filePath);
const writer = fs.createWriteStream(tempFilePath);
reader.pipe(new ExifTransformer()).pipe(writer).on('finish', () => {
fs.stat(tempFilePath, (err, tempStats) => {
if (err) {
console.error(`Unable to stat temp file: ${err}`);
return;
}
fs.stat(filePath, (err, originalStats) => {
if (err) {
console.error(`Unable to stat original file: ${err}`);
return;
}
if (tempStats.size !== originalStats.size) {
fs.rename(tempFilePath, filePath, (err) => {
if (err) {
console.error(`Unable to rename temp file: ${err}`);
return;
}
console.log(`Removed EXIF data from ${filePath}`);
});
} else {
fs.unlink(tempFilePath, (err) => {
if (err) {
console.error(`Unable to delete temp file: ${err}`);
}
});
}
});
});
});
}
function processDirectory(directory) {
fs.readdir(directory, (err, files) => {
if (err) {
console.error(`Unable to scan directory: ${err}`);
return;
}
files.forEach((file) => {
const filePath = path.join(directory, file);
fs.stat(filePath, (err, stats) => {
if (err) {
console.error(`Unable to stat file: ${err}`);
return;
}
if (stats.isDirectory()) {
if (file !== 'public' && file !== 'themes') {
processDirectory(filePath);
}
} else if (/\.(jpg|jpeg|png|gif|tiff)$/i.test(file)) {
removeExifData(filePath);
}
});
});
});
}
processDirectory(directoryPath);