-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (87 loc) · 2.71 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
"use strict";
/**
* Test with "node index.js testimages output 8"
*/
const util = require('util');
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const validImageExtensions = ['.png', '.jpg', '.jpeg', '.tiff', '.tif', '.gif', '.webp'];
let validArgs = true;
let sourceDir, destinationDir;
let resizeDivisor = 1;
if ((typeof process.argv[2] == 'undefined') || (process.argv[2] == null)) {
validArgs = false;
} else {
sourceDir = process.argv[2];
}
if ((typeof process.argv[3] == 'undefined') || (process.argv[3] == null)) {
validArgs = false;
} else {
destinationDir = process.argv[3];
}
if ((typeof process.argv[4] != 'undefined') && (Number.isInteger(Number(process.argv[4])))) {
resizeDivisor = Number(process.argv[4]);
}
if (!validArgs) {
console.log("Invalid parameters.\nUsage: node index.js sourceDir destinationDir [resizeDivisor]\n");
process.exit();
}
/**
* Manually promisify fs.readdir vs. using util.promisify(fs.readdir)
*
* @param {string} sourceDir
*/
/*
function fsReadDirPromise(sourceDir) {
return new Promise(function(resolve, reject) {
fs.readdir(sourceDir, (err, listing) => {
if (err) {
reject(err);
} else {
resolve(listing);
}
});
});
}*/
function filterImageExtensions(fileName) {
return validImageExtensions.includes(path.extname(fileName).toLowerCase());
}
function resize(file, destinationDir, resizeDivisor) {
return new Promise((resolve, reject) => {
const imagePath = `${sourceDir}/${file}`;
const image = sharp(imagePath);
image.metadata().then((metadata)=>{
image.resize(Math.round(metadata.width / resizeDivisor))
.toFile(destinationDir + '/' + file)
.then((output) => {
console.log(`Resized ${file} to width: ${output.width} height: ${output.height}`);
resolve();
}).catch((err) => {
console.log(`Failed to resize ${imagePath}`);
reject(err);
return;
});
}).catch((err) => {
console.log(`Failed to get metadata for ${imagePath}`);
reject(err);
return;
});
});
}
const readdir = util.promisify(fs.readdir);
readdir(sourceDir)
.then((listing) => {
let imageList = listing.filter(filterImageExtensions);
let resizePromises = [];
for (let file of imageList) {
resizePromises.push(resize(file, destinationDir, resizeDivisor));
}
Promise.all(resizePromises).then(() => {
console.log('Done!!!');
}).catch((err) => {
console.log(err);
});
}).catch((err) => {
console.log(err)
});