-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.js
49 lines (47 loc) · 2.14 KB
/
optimize.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
const sharp = require('sharp');
const fs = require('fs');
const fsp = require('fs/promises');
async function fun() {
if (!fs.existsSync(`backup`)) {
await fsp.mkdir('backup');
}
const articles = (await fsp.readdir('content/articles')).filter(dir => dir !== '.DS_Store');
for (const article of articles) {
if (!fs.existsSync(`backup/${article}`)) {
await fsp.mkdir(`backup/${article}`);
}
const content = (await fsp.readFile(`content/articles/${article}/index.exp`)).toString();
const photos = (await fsp.readdir(`content/articles/${article}`)).filter(file => file.slice(-4) === '.png');
for (const photo of photos) {
if (!fs.existsSync(`backup/${article}/${photo}`)) {
await fsp.copyFile(`content/articles/${article}/${photo}`, `backup/${article}/${photo}`);
const stream = sharp(`content/articles/${article}/${photo}`);
const info = await stream.metadata();
const match = content.match(new RegExp(`!\\[(?<width>\\d+); .+\\]\\(${photo}\\)`));
const width = Math.min(1800, (match == null ? 1800 : parseInt(match.groups.width)) * 2);
if (info.width > width) {
await stream
.resize(width)
.png({ quality: 80 })
.toFile(`content/articles/${article}/_${photo}`);
}
else {
await stream
.png({ quality: 80 })
.toFile(`content/articles/${article}/_${photo}`);
}
await fsp.unlink(`content/articles/${article}/${photo}`);
await fsp.rename(`content/articles/${article}/_${photo}`, `content/articles/${article}/${photo}`);
}
else {
await fsp.copyFile(`content/articles/${article}/${photo}`, `backup/${article}/_${photo}`);
await fsp.copyFile(`backup/${article}/${photo}`, `content/articles/${article}/_${photo}`);
await fsp.unlink(`content/articles/${article}/${photo}`);
await fsp.unlink(`backup/${article}/${photo}`);
await fsp.rename(`content/articles/${article}/_${photo}`, `content/articles/${article}/${photo}`);
await fsp.rename(`backup/${article}/_${photo}`, `backup/${article}/${photo}`);
}
}
}
}
fun();