-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.js
125 lines (114 loc) · 3.57 KB
/
compress.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
import fs from 'fs';
import path from 'path';
import https from 'https';
import { URL } from 'url';
import ora from 'ora';
import { getRandomIP } from './utils.js';
const defaultOptions = {
method: 'POST',
hostname: 'tinypng.com',
path: '/web/shrink',
headers: {
rejectUnauthorized: false,
'Postman-Token': Date.now(),
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent':
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
},
};
const getCompressedFile = (newImgPath, obj) => {
const options = new URL(obj.output.url);
const spinner = ora('Downloading').start();
const req = https.request(options, res => {
let body = '';
res.setEncoding('binary');
res.on('data', data => {
body += data;
});
res.on('end', () => {
fs.writeFile(newImgPath, body, 'binary', err => {
if (err) {
spinner.fail(`Failed: ${err}`);
return console.error(err);
}
spinner.succeed(
`[${newImgPath}] \n 压缩成功,压缩大小: ${obj.output.size}B,优化比例: ${
obj.output.ratio * 100
}%`,
);
});
});
});
req.on('error', e => {
spinner.fail(`Failed: ${e}`);
});
req.end();
};
const isSuitableFile = (stats, filePath) =>
stats.isFile() && stats.size <= global.maxSize && global.ext.includes(path.extname(filePath));
const getSavePath = imgPath => {
if (global.isOverwrite) {
return imgPath;
}
const extname = path.extname(imgPath); // eg: '.jpg'
const filename = path.basename(imgPath, extname);
return imgPath.replace(filename, `${filename}-compressed`);
};
const fileUpdate = (imgPath, obj) => {
const newImgPath = getSavePath(imgPath, global.isOverwrite);
getCompressedFile(newImgPath, obj);
};
// 异步API,压缩图片
// {"error":"Bad request","message":"Request is invalid"}
// {"input": { "size": 887, "type": "image/png" },"output": { "size": 785, "type": "image/png", "width": 81, "height": 81, "ratio": 0.885, "url": "https://tinypng.com/web/output/7aztz90nq5p9545zch8gjzqg5ubdatd6" }}
const uploadFile = (imgPath, options) => {
const spinner = ora('Uploading').start();
const req = https.request(options, res => {
res.on('data', buf => {
const obj = JSON.parse(buf.toString());
if (obj.error) {
// TODO
spinner.fail(`[${imgPath}]: 压缩失败!报错: ${obj.message}`);
} else {
spinner.succeed(`[${imgPath}] \n 上传成功,原始大小: ${obj.input.size}B`);
fileUpdate(imgPath, obj);
}
});
});
req.write(fs.readFileSync(imgPath), 'binary');
req.on('error', e => {
spinner.fail(`Failed: ${e}`);
console.error(e);
});
req.end();
};
const filterFile = filePath => {
fs.stat(filePath, (err, stats) => {
if (err) return console.error(err);
if (isSuitableFile(stats, filePath)) {
// 通过 X-Forwarded-For 头部伪造客户端IP
const options = { ...defaultOptions };
options.headers['X-Forwarded-For'] = getRandomIP();
uploadFile(filePath, options);
}
});
};
export const startCompress = folder => {
fs.readdir(folder, (err, files) => {
if (err) console.error(err);
files.forEach(file => {
const pathName = path.join(folder, file);
fs.stat(pathName, (_, stats) => {
if (stats.isDirectory() && global.isRecursion) {
startCompress(`pathName${'/'}`);
} else {
filterFile(pathName);
}
});
});
});
};
// module.exports = {
// startCompress,
// };