-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (61 loc) · 2.05 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const fetch = require('node-fetch');
const ipfs = require('ipfs-http-client');
const { urlSource } = ipfs;
var node;
async function start() {
node = await ipfs('http://localhost:5001');
let res = await fetch('https://linux.exchange/distros.json');
res = await res.json();
await updateRepo(res);
process.exit();
}
async function updateRepo(distros) {
let timeInBase64 = new Buffer(new Date().getTime().toString()).toString('base64');
let versions = distros.distros.map(distro => distro['versions']).flat();
await node.repo.gc();
let newHashes = versions.map(version => version['ipfs-hash']);
let hashesAlreadyThere = [];
for await (const hash of node.pin.ls({"type": "recursive"})) {
hashesAlreadyThere.push(hash.cid.toString());
};
var toRemove = hashesAlreadyThere.diff(newHashes);
let toAdd = newHashes.diff(hashesAlreadyThere);
console.log('Need to remove ' + toRemove.length + ' and add ' + toAdd.length);
for (let hash of toRemove) {
console.log("Removing " + hash);
await removeFile(hash);
}
for (let hash of toAdd) {
let version = versions.find(a => a['ipfs-hash'] == hash);
console.log("Adding " + version['direct-download-url'].substring(version['direct-download-url'].lastIndexOf('/') + 1));
await addFile(version['direct-download-url'].replace('{{base64time}}', timeInBase64));
}
return;
}
async function removeFile(hash) {
try {
await node.pin.rm(hash);
await node.repo.gc();
console.log("Removed " + hash);
return;
}
catch(e) {
console.error('Problem removing: ' + e.toString());
}
}
async function addFile(url) {
try {
for await (const file of node.add(urlSource(url, {"timeout": 60 * 1000, "headers": {"user-agent": "Wget/"}}))) {
console.log("Added " + url.substring(url.lastIndexOf('/') + 1));
return;
}
}
catch(e) {
console.error('Problem downloading: ' + e.toString());
}
}
// https://stackoverflow.com/a/4026828/2700296
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
start();