Added Sia Skynet uploading and downloading/viewing

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

@ -4,7 +4,8 @@ import fs from 'fs-extra';
import escape from 'escape-html';
import fetch, { Response } from 'node-fetch';
import { deleteS3 } from '../storage';
const { diskFilePath, s3enabled, viewDirect } = require('../../config.json');
import { SkynetDelete, SkynetDownload } from '../skynet';
const { diskFilePath, s3enabled, viewDirect, useSia } = require('../../config.json');
import { path, log, getTrueHttp, getTrueDomain, formatBytes, formatTimestamp, getS3url, getDirectUrl, getResourceColor, replaceholder } from '../utils';
const { CODE_UNAUTHORIZED, CODE_NOT_FOUND, } = require('../../MagicNumbers.json');
import { data } from '../data';
@ -70,13 +71,16 @@ router.get('/direct*', (req: AssRequest, res: AssResponse, next) => data.get(req
file.headers.forEach((value, header) => res.setHeader(header, value));
file.body?.pipe(res);
}),
sia: () => SkynetDownload(fileData)
.then((stream) => stream.pipe(res))
.then(() => SkynetDelete(fileData)),
local: () => {
res.header('Accept-Ranges', 'bytes').header('Content-Length', `${fileData.size}`).type(fileData.mimetype);
fs.createReadStream(fileData.path).pipe(res);
}
};
uploaders[s3enabled ? 's3' : 'local']();
return uploaders[s3enabled ? 's3' : useSia ? 'sia' : 'local']();
}).catch(next));
// Thumbnail response
@ -123,7 +127,7 @@ router.get('/delete/:deleteId', (req: AssRequest, res: AssResponse, next) => {
// Save the file information
return Promise.all([
s3enabled ? deleteS3(fileData) : fs.rmSync(path(fileData.path)),
s3enabled ? deleteS3(fileData) : !useSia ? fs.rmSync(path(fileData.path)) : () => Promise.resolve(),
(!fileData.is || (fileData.is.image || fileData.is.video)) && fs.existsSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail))
? fs.rmSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail)) : () => Promise.resolve()]);
})

@ -8,7 +8,7 @@ import Thumbnail from './thumbnails';
import Vibrant from './vibrant';
import Hash from './hash';
import { generateId, log } from './utils';
const { s3enabled, s3endpoint, s3bucket, s3usePathStyle, s3accessKey, s3secretKey, diskFilePath, saveAsOriginal, saveWithDate, mediaStrict, maxUploadSize } = require('../config.json');
import { SkynetUpload } from './skynet';
const { s3enabled, s3endpoint, s3bucket, s3usePathStyle, s3accessKey, s3secretKey, diskFilePath, saveAsOriginal, saveWithDate, mediaStrict, maxUploadSize, useSia } = require('../config.json');
const { CODE_UNSUPPORTED_MEDIA_TYPE } = require('../MagicNumbers.json');
@ -98,22 +98,27 @@ export function processUploaded(req: AssRequest, res: AssResponse, next: Functio
.then(() => log.debug('Saving file', req.file!.originalname, s3enabled ? 'in S3' : useSia ? 'on Sia blockchain' : 'on disk'))
.then(() =>
// skipcq: JS-0229
new Promise((resolve, reject) => s3enabled
new Promise((resolve, reject) => {
// Upload to Amazon S3
? s3.putObject({
if (s3enabled) return s3.putObject({
Bucket: s3bucket,
Key: req.file!.randomId.concat(req.file!.ext),
ACL: 'public-read',
ContentType: req.file!.mimetype,
Body: fs.createReadStream(req.file!.path)
}).promise().then(resolve).catch(reject)
}).promise().then(resolve).catch(reject);
// Use Sia Skynet
else if (useSia) return SkynetUpload(req.file!.path)
.then((skylink) => req.file!.randomId = skylink)
.then(resolve).catch(reject);
// Save to local storage
: fs.ensureDir(getDatedDirname())
else return fs.ensureDir(getDatedDirname())
.then(() => fs.copy(req.file!.path, getLocalFilename(req), { preserveTimestamps: true }))
.then(resolve)
.catch(reject)
.then(resolve).catch(reject);
}))
.then(() => log.debug('File saved', req.file!.originalname, s3enabled ? 'in S3' : useSia ? 'on Sia blockchain' : 'on disk'))
.catch((err) => next(err))

Loading…
Cancel
Save