-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.js
66 lines (56 loc) · 1.94 KB
/
loader.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
const path = require("path");
const fs = require("fs");
const superagent = require("superagent");
async function getImages(rootPath) {
const content = await fs.promises.readdir(rootPath, { withFileTypes: true });
const images = content
.filter((x) => !x.isDirectory())
.map((x) => x.name)
.filter((x) => x !== ".gitkeep");
return images;
}
async function deleteImages(rootPath, fileNamesLocal, fileNamesServer) {
const diff = fileNamesLocal.filter((x) => !fileNamesServer.includes(x));
for (fileName of diff) {
await fs.promises.unlink(path.join("/", rootPath, fileName));
}
}
async function fetchServerImages(agent, config) {
await agent
.post(config.url + "?format=json&method=pwg.session.login")
.field("username", config.username)
.field("password", config.password);
const data = await agent.get(
config.url + "?format=json&method=pwg.categories.getImages"
);
respText = JSON.parse(data.res.text);
images = respText.result.images;
if (images.length === 0) {
throw Error(
"There were no images found! Check connection details or upload images."
);
}
return images;
}
async function downloadImage(agent, url, targetPath) {
await new Promise((resolve) =>
agent.get(url).pipe(fs.createWriteStream(targetPath)).on("finish", resolve)
);
}
async function syncImages(rootPath, config) {
const agent = superagent.agent();
const images = await fetchServerImages(agent, config);
const localFileNames = await getImages(rootPath);
const newImages = images.filter((img) => !localFileNames.includes(img.file));
for (img of newImages) {
const url = img.derivatives[config.size].url;
await downloadImage(agent, url, path.join("/", rootPath, img.file));
}
await deleteImages(
rootPath,
localFileNames,
images.map((i) => i.file)
);
}
const exportedForTesting = { deleteImages, fetchServerImages, downloadImage };
module.exports = { getImages, syncImages, exportedForTesting };