forked from rohenaz/bmap-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitfs.ts
40 lines (34 loc) · 1.08 KB
/
bitfs.ts
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
import BetterQueue from 'better-queue'
import chalk from 'chalk'
import fs from 'fs'
import fetch from 'node-fetch'
// ToDo - Using a queue so if the file download fails for some reason we can add it back to the queue?
const q = new BetterQueue(
function (file) {
// TODO if data dir doesnt exist this will error out on mac
let path = 'data/' + file + '.bitfs'
// See if the file exists already before fetching it
try {
fs.access(path, fs.constants.F_OK, async (err) => {
if (err) {
// Fetch from BitFS and store to local file
console.log(chalk.cyan('saving https://bitfs.network/' + file))
let res = await fetch('https://x.bitfs.network/' + file)
res.body.pipe(fs.createWriteStream(path))
return
}
// file exists
console.log(chalk.cyan('file already exists'))
})
} catch (err) {
console.log('error checking or writing file', err)
}
},
{ afterProcessDelay: 10 }
)
const saveFiles = (bitfs) => {
for (let file of bitfs) {
q.push(file)
}
}
export { saveFiles }