-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (61 loc) · 2.24 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
import dotenv from 'dotenv';
dotenv.config();
import fs from 'fs';
import path from 'path';
import Downloader from 'nodejs-file-downloader'
import { Web3Storage, getFilesFromPath } from 'web3.storage'
(async() => {
let fileName = "";
const url = process.argv.slice(2)[0];
console.log('downloading: ', url);
const downloader = new Downloader({
url: url,
directory: "./downloads",
maxAttempts: 3,
cloneFiles: false,
onProgress: function(percentage) {
console.log("Downloading : ", percentage + "%");
},
shouldStop: function(error) {
if (e.statusCode && e.statusCode === 404) {
console.log("Not Found 404")
return true;
}
},
onBeforeSave: (deducedName) => {
fileName = deducedName;
},
});
try {
await downloader.download();
var fileLocation = path.join(downloader.config.directory, fileName)
const fileDownload = await getFilesFromPath(fileLocation);
async function storeWithProgress(files, client) {
let ccid = "";
const onRootCidReady = cid => {
ccid = cid
console.log('uploading files with cid:', ccid);
}
const totalSize = files.map(f => f.size).reduce((a, b) => a + b, 0)
let uploaded = 0
const onStoredChunk = size => {
uploaded += size
const pct = (uploaded / totalSize) * 100;
console.log(`Uploading... ${pct.toFixed(0)}% complete`);
if (pct.toFixed(0) == 100) {
if (fs.existsSync(fileLocation)) {
fs.rm(fileLocation, { recursive: true }, () => {
console.log("Upload Complete https://" + ccid + ".ipfs.dweb.link");
process.exit();
});
}
}
}
return client.put(files, { onRootCidReady, onStoredChunk })
};
const client = new Web3Storage({ token: process.env.API_KEY });
await storeWithProgress(fileDownload, client);
} catch (error) {
console.log("Download failed", error);
}
})();