forked from ZJ595/AndroidReverse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
download_images.mjs
91 lines (75 loc) · 2.71 KB
/
download_images.mjs
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
import fs from 'node:fs';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { URL } from 'node:url';
import fetch from 'node-fetch';
const __dirname = import.meta.dirname;
const postsDir = path.join(__dirname, '../Article/');
const CAN_USE_WEBP = testCanConvertWebp();
function testCanConvertWebp() {
const { status } = spawnSync('cwebp', { stdio: 'ignore' });
return status === 0;
}
async function processDir(dirname) {
const children = fs.readdirSync(dirname).map((f) => ({
name: f,
path: dirname + '/' + f,
stat: fs.statSync(dirname + '/' + f),
}));
const files = children.filter((f) => f.stat.isFile() && f.name.toLowerCase().endsWith('.md'));
const dirs = children.filter((f) => f.stat.isDirectory()).map((f) => f.path);
for (const dir of dirs) {
await processDir(dir);
}
for (const f of files) {
await processFile(f.path);
}
}
async function processFile(filePath) {
const fileName = path.basename(filePath);
const filePrefix = fileName.match(/^\d+/)?.[0] || '00';
console.info(`process file: ${fileName}\n`);
const content = fs.readFileSync(filePath, 'utf-8');
const regex = /!\[(.*?)\]\((https?:\/\/.+?)\)/g;
const replacements = [];
let m = null;
while ((m = regex.exec(content)) !== null) {
const [_, alt, url] = m;
const imageName = path.basename(new URL(url).pathname);
const imagePath = path.resolve(filePath, '..', `_assets_${filePrefix}`, imageName);
fs.mkdirSync(path.dirname(imagePath), { recursive: true });
console.log('download from %s...', url);
const buffer = await fetch(url, {
headers: {
Referer: url,
},
}).then((resp) => resp.arrayBuffer());
fs.writeFileSync(imagePath, Buffer.from(buffer));
let finalImagePath = imagePath;
if (CAN_USE_WEBP && imageName.toLowerCase().endsWith('.png')) {
finalImagePath = finalImagePath.replace(/\.png$/i, '.webp');
const { status } = spawnSync('cwebp', ['-lossless', '-z', '9', '-m', '6', imagePath, '-o', finalImagePath], {
stdio: 'inherit',
});
if (status === 0) {
fs.unlinkSync(imagePath);
} else {
console.warn(`cwebp exit with error ${status}`);
}
}
const relativeImagePath = path.relative(postsDir, finalImagePath);
replacements.push({ alt, path: relativeImagePath });
}
if (replacements.length === 0) {
// 没有要转存的图片
return;
}
let replacement_id = 0;
const fixedContent = content.replace(regex, () => {
const replacement = replacements[replacement_id++];
return `![${replacement.alt}](${replacement.path})`;
});
fs.writeFileSync(filePath, fixedContent, 'utf-8');
}
console.info(`\n\n!!!start!!!\n`);
processDir(postsDir);