forked from NebulousLabs/nodejs-skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (90 loc) · 3.06 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const p = require('path')
const axios = require('axios')
const FormData = require('form-data')
const fs = require('fs')
const DefaultUploadOptions = {
portalUrl: "https://siasky.net",
portalUploadPath: "/skynet/skyfile",
portalFileFieldname: "file",
portalDirectoryFileFieldname: "files[]",
customFilename: "",
}
const DefaultDownloadOptions = {
portalUrl: "https://siasky.net"
}
function UploadFile(path, opts) {
const options = opts.customFilename ? { filename: opts.customFilename } : {}
const formData = new FormData();
formData.append(opts.portalFileFieldname, fs.createReadStream(path), options);
const url = `${trimTrailingSlash(opts.portalUrl)}${trimTrailingSlash(opts.portalUploadPath)}`
return new Promise((resolve, reject) => {
axios.post(url, formData, { headers: formData.getHeaders() })
.then(resp => {
resolve(`sia://${resp.data.skylink}`)
}).catch(error => {
reject(error)
})
})
}
function UploadDirectory(path, opts) {
const stat = fs.statSync(path)
if (!stat.isDirectory()) {
throw new Error(`Given path is not a directory: ${path}`)
}
const formData = new FormData();
for (const file of walkDirectory(path)) {
formData.append(opts.portalDirectoryFileFieldname, fs.createReadStream(file), { filepath: file });
}
const url = `${trimTrailingSlash(opts.portalUrl)}${trimTrailingSlash(opts.portalUploadPath)}?filename=${opts.customFilename || path}`
return new Promise((resolve, reject) => {
axios.post(url, formData, { headers: formData.getHeaders() })
.then(resp => {
resolve(`sia://${resp.data.skylink}`)
}).catch(error => {
reject(error)
})
})
}
function DownloadFile(path, skylink, opts) {
const url = `${trimTrailingSlash(opts.portalUrl)}/${trimSiaPrefix(skylink)}`
const writer = fs.createWriteStream(path)
return new Promise((resolve, reject) => {
axios.get(url, { responseType: 'stream' })
.then(response => {
response.data.pipe(writer)
writer.on('finish', resolve)
writer.on('error', reject)
})
.catch(error => {
reject(error)
})
})
}
function walkDirectory(path, out) {
let files = []
if (!fs.existsSync(path)) {
return files;
}
for (const subpath of fs.readdirSync(path)) {
const fullpath = p.join(path, subpath)
if (fs.statSync(fullpath).isDirectory()) {
files = files.concat(walkDirectory(fullpath, out))
continue
}
files.push(fullpath)
}
return files
}
function trimSiaPrefix(str) {
return str.replace("sia://", "")
}
function trimTrailingSlash(str) {
return str.replace(/\/$/, "");
}
module.exports = {
DefaultUploadOptions: DefaultUploadOptions,
DefaultDownloadOptions: DefaultDownloadOptions,
UploadFile: UploadFile,
UploadDirectory: UploadDirectory,
DownloadFile: DownloadFile,
}