-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·396 lines (334 loc) · 11.8 KB
/
index.js
File metadata and controls
executable file
·396 lines (334 loc) · 11.8 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env node
const fs = require('fs-extra');
const path = require('path');
const axios = require('axios');
const glob = require('glob');
const chalk = require('chalk');
const argv = require('minimist')(process.argv.slice(2));
const { name, version } = require('./package.json');
const CONFIG = {
API: 'https://tinyjpg.com/backend/opt/shrink',
IMG_REGEXP: /\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/,
GLOB_SUFFIX: '/*.+(png|jpg|jpeg|PNG|JPG|JPEG)',
BATCH_SIZE: 5,
BATCH_DELAY: 5 * 1000,
REQUEST_TIMEOUT: 15 * 1000,
FAILED_FILES_PATH: path.join(process.cwd(), '.tinypic-failed.json'),
HEADERS: {
"referer": "https://tinyjpg.com/",
"user-agent": `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_${Math.floor(Math.random() * 10)}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0`,
"x-forwarded-for": `${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`,
"x-real-ip": `${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`,
}
};
// 工具函数
const utils = {
exists: (path) => fs.existsSync(path),
getKb: (bytes) => (bytes / 1024).toFixed(1) + 'k',
getFileSize: (file) => fs.statSync(file).size,
isDirectory: (path) => utils.exists(path) && fs.statSync(path).isDirectory(),
chunkArray: (array, size) => {
const chunks = [];
for (let i = 0; i < array.length; i += size) {
chunks.push(array.slice(i, i + size));
}
return chunks;
},
sleep: (ms) => new Promise(resolve => setTimeout(resolve, ms)),
// 倒计时功能
countdown: async (seconds) => {
return new Promise((resolve) => {
let remaining = seconds;
const timer = setInterval(() => {
process.stdout.write(`\r${chalk.gray(`⏳ wait ${remaining} seconds before next batch...`)}`);
remaining--;
if (remaining < 0) {
clearInterval(timer);
process.stdout.write('\r' + ' '.repeat(50) + '\r'); // 清除倒计时行
resolve();
}
}, 1000);
});
},
// 保存失败文件列表
saveFailedFiles: (failedFiles) => {
try {
const data = {
timestamp: new Date().toISOString(),
files: failedFiles
};
fs.writeFileSync(CONFIG.FAILED_FILES_PATH, JSON.stringify(data, null, 2));
} catch (error) {
console.log(chalk.yellow(`Warning: Could not save failed files list: ${error.message}`));
}
},
// 读取失败文件列表
loadFailedFiles: () => {
try {
if (!utils.exists(CONFIG.FAILED_FILES_PATH)) {
return null;
}
const data = JSON.parse(fs.readFileSync(CONFIG.FAILED_FILES_PATH, 'utf8'));
return data.files || [];
} catch (error) {
console.log(chalk.yellow(`Warning: Could not load failed files list: ${error.message}`));
return null;
}
},
// 清除失败文件列表
clearFailedFiles: () => {
try {
if (utils.exists(CONFIG.FAILED_FILES_PATH)) {
fs.unlinkSync(CONFIG.FAILED_FILES_PATH);
}
} catch (error) {
console.log(chalk.yellow(`Warning: Could not clear failed files list: ${error.message}`));
}
}
};
// 文件收集器
class FileCollector {
constructor(recursive = false) {
this.recursive = recursive;
}
collect() {
const inputFiles = this._getInputFiles();
const imageFiles = this._extractImageFiles(inputFiles);
return [...new Set(imageFiles)]; // 去重
}
_getInputFiles() {
if (!argv._.length) return fs.readdirSync('./');
if (argv._.length === 1) {
const input = argv._[0];
if (input === '.' || input === './') return fs.readdirSync('./');
if (utils.isDirectory(input)) return { directory: input };
return [input];
}
return argv._;
}
_extractImageFiles(files) {
if (files.directory) {
return this._collectFromDirectory(files.directory);
}
const imageFiles = [];
const fileArray = Array.isArray(files) ? files : [files];
fileArray.forEach(file => {
if (this.recursive && utils.exists(file) && utils.isDirectory(file)) {
imageFiles.push(...this._collectRecursively(file));
} else if (this._isImageFile(file)) {
imageFiles.push(...this._validateAndAdd(file));
}
});
return imageFiles;
}
_collectFromDirectory(dir) {
const dirPath = dir.endsWith('/') ? dir : dir + '/';
return fs.readdirSync(dirPath)
.filter(file => this._isImageFile(file))
.map(file => dirPath + file)
.filter(file => utils.exists(file));
}
_collectRecursively(dir) {
return glob.sync(dir + '/**' + CONFIG.GLOB_SUFFIX);
}
_isImageFile(file) {
return CONFIG.IMG_REGEXP.test(file);
}
_validateAndAdd(file) {
if (utils.exists(file)) {
return [file];
} else {
console.log(chalk.bold.red(`✗ ${file} does not exist!`));
return [];
}
}
}
// 图片压缩器
class ImageCompressor {
async compressFile(file) {
const originalSize = utils.getFileSize(file);
const delay = Math.random() * 1000;
await utils.sleep(delay);
try {
const compressedData = await this._uploadForCompression(file);
if (!compressedData.output?.url) {
throw new Error(compressedData.message || 'No compressed output received');
}
const { size: compressedSize, url } = compressedData.output;
const savings = originalSize - compressedSize;
const percentage = (savings / originalSize * 100);
if (percentage < 1) {
console.log(chalk.yellow(`✗ Couldn't compress \`${file}\` any further`));
return { success: true, file, skipped: true };
}
await this._downloadAndSave(url, file);
console.log(chalk.green(`✓ Saved ${utils.getKb(savings)} (${percentage.toFixed(2)}%) for \`${chalk.bold(file)}\``));
return { success: true, file };
} catch (error) {
console.log(chalk.red(`✗ Failed to compress \`${file}\`: ${error.message}`));
return { success: false, file, error: error.message };
}
}
async _uploadForCompression(file) {
const fileStream = fs.createReadStream(file);
const response = await axios.post(CONFIG.API, fileStream, {
headers: CONFIG.HEADERS,
responseType: 'json',
timeout: CONFIG.REQUEST_TIMEOUT,
});
return response.data;
}
async _downloadAndSave(url, file) {
const response = await axios.get(url, { responseType: 'stream' });
const writer = fs.createWriteStream(file);
return new Promise((resolve, reject) => {
response.data.pipe(writer);
writer.on('close', resolve);
writer.on('error', reject);
});
}
}
// 批量处理器
class BatchProcessor {
constructor() {
this.compressor = new ImageCompressor();
this.failedFiles = [];
this.skippedFiles = [];
}
async process(imageFiles) {
if (imageFiles.length === 0) {
console.log(chalk.bold.red('✗ No images found.'));
return;
}
this._printProcessingInfo(imageFiles.length);
const batches = utils.chunkArray(imageFiles, CONFIG.BATCH_SIZE);
for (let i = 0; i < batches.length; i++) {
await this._processBatch(batches[i], i + 1, batches.length);
if (i < batches.length - 1) {
const delaySeconds = Math.ceil(CONFIG.BATCH_DELAY / 1000);
await utils.countdown(delaySeconds);
}
}
this._printFinalSummary(imageFiles.length);
}
_printProcessingInfo(totalImages) {
console.log(chalk.bold.green(`✓ Found ${totalImages} image${totalImages === 1 ? '' : 's'}`));
console.log(chalk.bold.yellow(`Check API ${CONFIG.API} if it does not work`));
const totalBatches = Math.ceil(totalImages / CONFIG.BATCH_SIZE);
console.log(chalk.bold.blue(`📋 Images will be processed in ${totalBatches} batch${totalBatches === 1 ? '' : 'es'} (max ${CONFIG.BATCH_SIZE} images per batch)`));
console.log(chalk.bold('Processing...'));
}
async _processBatch(batch, batchIndex, totalBatches) {
console.log(chalk.cyan(`\n📦 Processing batch ${batchIndex}/${totalBatches} (${batch.length} images)...`));
const results = await Promise.all(batch.map(file => this.compressor.compressFile(file)));
// 收集失败和跳过的文件
results.forEach(result => {
if (!result.success) {
this.failedFiles.push({ file: result.file, error: result.error });
} else if (result.skipped) {
this.skippedFiles.push(result.file);
}
});
console.log(chalk.green(`✅ Batch ${batchIndex}/${totalBatches} completed!`));
}
_printFinalSummary(totalFiles) {
const successCount = totalFiles - this.failedFiles.length;
const skippedCount = this.skippedFiles.length;
const failedCount = this.failedFiles.length;
console.log(chalk.bold.green('\n🎉 All batches completed!'));
console.log(chalk.bold.blue(`📊 Summary: ${successCount} successful, ${skippedCount} skipped, ${failedCount} failed`));
if (this.skippedFiles.length > 0) {
console.log(chalk.yellow(`\n⚠️ Skipped files (couldn't compress further):`));
this.skippedFiles.forEach(file => {
console.log(chalk.yellow(` • ${file}`));
});
}
if (this.failedFiles.length > 0) {
console.log(chalk.red(`\n❌ Failed files:`));
this.failedFiles.forEach(({ file, error }) => {
console.log(chalk.red(` • ${file} - ${error}`));
});
// 保存失败文件列表
utils.saveFailedFiles(this.failedFiles);
console.log(chalk.cyan(`\n💡 Tip: Run ${chalk.bold('tiny failed')} to retry failed files`));
} else {
// 如果没有失败文件,清除之前保存的失败文件列表
utils.clearFailedFiles();
}
}
// 处理失败文件的静态方法
static async processFailedFiles() {
const failedFiles = utils.loadFailedFiles();
if (!failedFiles || failedFiles.length === 0) {
console.log(chalk.yellow('📝 No failed files found to retry'));
return;
}
console.log(chalk.blue(`📋 Found ${failedFiles.length} failed file${failedFiles.length === 1 ? '' : 's'} to retry:`));
failedFiles.forEach(({ file }) => {
console.log(chalk.gray(` • ${file}`));
});
// 只处理文件名,重新压缩
const fileNames = failedFiles.map(({ file }) => file);
const processor = new BatchProcessor();
await processor.process(fileNames);
}
}
// 备份功能
function backupFiles() {
console.log(chalk.green('Backing Up all your FILES...'));
const curPath = process.cwd();
const parentDir = path.resolve(curPath, '../');
const folderName = path.basename(curPath);
const backupPath = path.join(parentDir, '_' + folderName);
fs.copy(curPath, backupPath, err => {
if (err) {
console.error(err);
} else {
console.log(chalk.yellow(`Done! The backup is in \`${backupPath}\` !`));
}
});
}
// 显示帮助信息
function showHelp() {
const helpText = `
Usage
tiny <file or path>
tiny -b // backup all your images into \`_folder\`
tiny failed // retry failed files from last compression
Example
tiny // current dir
tiny . // current dir
tiny -r // shrink images recursively
tiny a.jpg
tiny a.jpg b.jpg
tiny img/test.jpg
tiny folder
tiny failed // retry previously failed files
`;
console.log(chalk.green(helpText));
}
// 主程序
(async () => {
// 检查是否是 failed 命令
if (argv._.includes('failed')) {
await BatchProcessor.processFailedFiles();
return;
}
if (argv.b) {
backupFiles();
return;
}
if (argv.v) {
console.log(name + ' version: ' + chalk.green(version));
return;
}
if (argv.h) {
showHelp();
return;
}
// 压缩图片
const collector = new FileCollector(argv.r);
const imageFiles = collector.collect();
const processor = new BatchProcessor();
await processor.process(imageFiles);
})();