-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress.js
More file actions
66 lines (62 loc) · 2 KB
/
Copy pathcompress.js
File metadata and controls
66 lines (62 loc) · 2 KB
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
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
const rootPath = "D:/115下载/更2"; //把父文件夹地址填充于此!
async function compress() {
let files = walk(rootPath);
for (let index = 0; index < files.length; index++) {
const file = files[index];
const stat = fs.statSync(file);
const metadata = await sharp(file).metadata().catch((err) => {
console.log(err);
console.log(file);
});
const extname = path.extname(file);
const filename = path.basename(file, extname);
const dirname = path.dirname(file);
sharp.cache(false);
try {
if (metadata.width > 1920) {
await sharp(file).resize({width: 1920}).toFile(dirname + '/' + filename + '_compress.jpg');
fs.unlinkSync(file);
} else {
if (metadata.format == 'png') {
await sharp(file).toFile(dirname + '/' + filename + '_compress.jpg');
fs.unlinkSync(file);
} else if (metadata.format == 'webp') {
await sharp(file).toFile(dirname + '/' + filename + '_compress.jpg');
fs.unlinkSync(file);
} else if (metadata.format == 'jpeg') {
if(extname == '.webp') {
fs.renameSync(file, dirname + '/' + filename + '.jpg');
} else {
if ((stat.size / (1024 * 1000)) > 1) {
await sharp(file).jpeg({quality: 80}).toFile(dirname + '/' + filename + '_compress.jpg');
fs.unlinkSync(file);
}
}
}
}
} catch (error) {
console.log(error);
console.log(file);
}
}
}
compress();
function walk(dir) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(walk(file));
} else {
/* Is a file */
results.push(file);
}
});
return results;
}