Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (63 loc) · 2.21 KB
/
Copy pathindex.js
File metadata and controls
78 lines (63 loc) · 2.21 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
const fs = require('fs-extra');
const path = require('path');
const FormData = require('form-data');
const axios = require('axios').default;
const pLimit = require('p-limit');
const IMAGE_HOSTING_URL = 'https://imgkr.com/api/files/upload';
function convertMimeToExt(mimeType) {
return `.${mimeType.split('/')[1]}`;
}
function transferImage(image, tmpDir) {
const matchArr = image.match(/!\[.*?\]\((.+?)\)/);
const originalUrl = matchArr[1];
return new Promise((resolve, reject) => {
console.log(`Downloading from ${originalUrl} ...`);
axios({
method: 'get',
url: originalUrl,
responseType: 'stream',
})
.then((response) => {
const ext = convertMimeToExt(response.headers['content-type']);
const filename = path.join(tmpDir, Math.random().toString(16).slice(2, 10) + ext);
const writeStream = fs.createWriteStream(filename);
response.data.pipe(writeStream);
writeStream.on('close', async () => {
const form = new FormData();
form.append('file', fs.createReadStream(filename));
axios
.post(IMAGE_HOSTING_URL, form, {
headers: form.getHeaders(),
})
.then((response) => {
const newUrl = response.data.data;
console.log('Uploaded to', newUrl);
resolve({ originalUrl, newUrl });
})
.catch((err) => reject(err));
});
})
.catch((err) => reject(err));
});
}
function moveImages(srcPath, destPath = null, concurrency = 2) {
const tmpDir = 'tmp';
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
}
let content = fs.readFileSync(srcPath).toString();
const images = content.match(/!\[.*?\]\(.+?\)/g);
const limit = pLimit(concurrency);
const tasks = images.map((image) => limit(() => transferImage(image, tmpDir)));
Promise.all(tasks)
.then((results) => {
results.forEach(({ originalUrl, newUrl }) => {
content = content.replace(originalUrl, newUrl);
});
fs.writeFileSync(destPath || srcPath, content);
console.log(`Saved to ${destPath}`);
fs.removeSync(tmpDir);
})
.catch((err) => console.log(err));
}
exports.moveImages = moveImages;