Added `skynet.ts` with functions for uploading and downloading

pull/100/head
tycrek 3 years ago
parent efbef491a7
commit b1680c39b3
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -0,0 +1,37 @@
import { FileData } from "./definitions";
import fs, { ReadStream } from 'fs-extra';
import { path } from './utils';
const { SkynetClient } = require('@skynetlabs/skynet-nodejs');
function getFullPath(fileData: FileData) {
return path('share', '.skynet', `${fileData.randomId}${fileData.ext}`.replace(/sia\:\/\//gi, ''));
}
// Create the SkyNet client
export const Skynet = new SkynetClient();
export function SkynetUpload(path: string): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
const skylink = await Skynet.uploadFile(path);
resolve(skylink);
} catch (error) {
reject(error);
}
});
}
export function SkynetDownload(fileData: FileData): Promise<ReadStream> {
return new Promise((resolve: Function, reject) =>
fs.ensureDir(path('share', '.skynet'))
.then(async () => {
await Skynet.downloadFile(getFullPath(fileData), fileData.randomId);
return fs.createReadStream(getFullPath(fileData))
})
.then((stream) => resolve(stream))
.catch(reject));
}
export function SkynetDelete(fileData: FileData) {
return fs.remove(getFullPath(fileData));
}
Loading…
Cancel
Save