@nebulous/skynet
Advanced tools
Comparing version 0.0.8 to 0.0.9
44
index.js
@@ -0,1 +1,2 @@ | ||
const p = require('path') | ||
const axios = require('axios') | ||
@@ -9,2 +10,3 @@ const FormData = require('form-data') | ||
portalFileFieldname: "file", | ||
portalFilesFieldname: "files[]", | ||
customFilename: "", | ||
@@ -36,2 +38,26 @@ } | ||
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.portalFilesFieldname, fs.createReadStream(file), { filepath: file }); | ||
} | ||
const uuid = generateUUID() | ||
const url = `${trimTrailingSlash(opts.portalUrl)}${trimTrailingSlash(opts.portalUploadPath)}/${uuid}?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) { | ||
@@ -55,2 +81,19 @@ const url = `${trimTrailingSlash(opts.portalUrl)}/${trimSiaPrefix(skylink)}` | ||
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 generateUUID() { | ||
@@ -77,3 +120,4 @@ let uuid = '' | ||
UploadFile: UploadFile, | ||
UploadDirectory: UploadDirectory, | ||
DownloadFile: DownloadFile, | ||
} |
{ | ||
"name": "@nebulous/skynet", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Skynet SDK", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
4996
99