-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_thumbs.js
177 lines (146 loc) · 4.38 KB
/
process_thumbs.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const utils = require('./utils.js');
const fs = require('fs');
const sharp = require('sharp');
const thumbler = require('video-thumb');
const createThumbs = (path, thumbPath, width = 100, height = 100, missingOnly = false) => {
fs.readdir(path, (err, files) => {
if (err) {
console.error("ERROR: Error reading image files:", err);
return;
}
files = utils.filterMedia(files);
console.log("Generating thumbnails..");
var i = 0;
files.forEach((val) => {
if (missingOnly) {
if (fs.existsSync(`${thumbPath}/${val}`)) {
return;
}
}
const ext = utils.getFileExtension(val);
if (utils.imageTypes.includes(ext)) {
sharp(`${path}/${val}`)
.resize(width, height)
.withMetadata() // IMPORTANT!! Preserve Exif, rotation, orientation, etc.
.toFile(`${thumbPath}/${val}`)
.then(() => {
i++;
if (i >= files.length) console.log("Generating thumbnails done!");
})
.catch((error) => {
console.error("ERROR: while processing thumbnail.");
console.trace(error);
});
} else if (utils.videoTypes.includes(ext)) {
thumbler.extract(`${path}/${val}`, `${thumbPath}/${val}.png`, '00:00:00', `${width}x${height}`, (err) => {
if (err) {
console.error("ERROR: Unexpected error during thumbnail generation for video.");
console.trace(err);
}
});
} else {
console.warn("WARNING: Unknown file type, omitting.");
}
});
});
};
const processThumbs = (path, thumbPath, width, height, missingOnly) => {
fs.access(thumbPath, fs.constants.R_OK | fs.constants.W_OK, (err) => {
if (err) {
console.error("ERROR: Error accessing thumbnails path:", err);
return;
}
fs.readdir(thumbPath, (err, files) => {
if (err) {
console.error("ERROR: Unexpected error during old thumbnails processing:", err);
throw err;
}
if (files.length <= 0) {
console.log("No previous thumbnails found.");
createThumbs(path, thumbPath, width, height, missingOnly);
return;
}
if (!missingOnly) {
console.log("Deleting old thumbnails..");
for (var i = 0; i < files.length; i++) {
fs.unlink(`${thumbPath}/${files[i]}`, (erro) => {
if (erro) {
console.error("ERROR: Failed to remove old thumbnail:", erro);
return;
}
});
}
console.log("Deleting old thumbnails done!");
}
createThumbs(path, thumbPath, width, height, missingOnly);
});
});
};
module.exports.processThumbnails = (path, width, height, missingOnly) => {
if (!path) {
console.error("ERROR: Invalid path provided:", path);
return;
}
width = (width != null && !isNaN(Number(width))) ? Number(width) : undefined;
height = (height != null && !isNaN(Number(height))) ? Number(height) : undefined;
fs.exists(path, (ex) => {
if (!ex) {
console.error("ERROR: Provided image directory does not exist:", path);
return;
}
const thumbPath = path + '/thumbnails';
console.log(`Targeting thumbnails directory: ${thumbPath}`);
fs.exists(thumbPath, (ex) => {
if (!ex) {
fs.mkdir(thumbPath, { 'recursive': false, 'mode': 0775 }, (err) => {
if (err) {
console.error("ERROR: Failed to create thumbnails directory:", err);
throw err;
}
console.log("Created thumbnails directory.");
});
} else {
console.log("Thumbnails dir already exists.");
}
processThumbs(path, thumbPath, width, height, missingOnly);
});
});
};
// args 0: /path/to/node
// args 1: /path/to/script.js
// args 2: arg 1
// args 3: arg 2
// ...
const args = process.argv;
const printHelp = () => {
console.log("Usage: node process_thumbs.js <path> [missing] <width> <height>");
};
if (args[2] == "help") {
printHelp();
process.exit(0);
}
let path;
let missing;
let width;
let height;
if (!args[2]) {
console.error("ERROR: No image path given as argument. Enter the path to the images to generate thumbnails from.");
printHelp();
process.exit(1);
}
path = args[2];
missing = args[3] === '1' || args[3].toLowerCase() === 'true' ? true : false;
if (!args[4]) {
console.warn("WARNING: No target image width for thumbnails given. Using default value.");
printHelp();
} else {
width = args[4];
}
if (!args[5]) {
console.warn("WARNING: No target image height for thumbnails given. Using default value.");
printHelp();
} else {
height = args[5];
}
console.log("Will process with arguments:", path, width, height, missing);
this.processThumbnails(path, width, height, missing);