From d227537575dc8366e876cc7d91c4cda92ccf3a27 Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Fri, 14 Jul 2023 20:04:20 -0600 Subject: [PATCH] feat: implement upload files via S3 (still no accessing) --- backend/routers/index.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/backend/routers/index.ts b/backend/routers/index.ts index f53ca40..5dc6180 100644 --- a/backend/routers/index.ts +++ b/backend/routers/index.ts @@ -6,6 +6,7 @@ import { log } from '../log'; import { UserConfig } from '../UserConfig'; import { random } from '../generators'; import { BusBoyFile, AssFile } from 'ass'; +import { uploadFileS3 } from '../s3'; const router = Router({ caseSensitive: true }); @@ -30,6 +31,7 @@ router.post('/', async (req, res) => { // Does the file actually exist if (!req.files || !req.files['file']) return res.status(400).type('text').send('No file was provided!'); + else log.debug('Upload request received', `Using ${UserConfig.config.s3 != null ? 'S3' : 'local'} storage`); // Type-check the file data const bbFile: BusBoyFile = req.files['file']; @@ -37,27 +39,42 @@ router.post('/', async (req, res) => { // Prepare file move const uploads = UserConfig.config.uploadsDir; const timestamp = Date.now().toString(); - const destination = `${uploads}${uploads.endsWith('/') ? '' : '/'}${timestamp}_${bbFile.filename}`; - // todo: S3 + const fileKey = `${timestamp}_${bbFile.filename}`; + const destination = `${uploads}${uploads.endsWith('/') ? '' : '/'}${fileKey}`; + + // S3 configuration + const s3 = UserConfig.config.s3 != null ? UserConfig.config.s3 : false; try { // Move the file - await fs.move(bbFile.file, destination); + if (!s3) await fs.move(bbFile.file, destination); + else await uploadFileS3(await fs.readFile(bbFile.file), bbFile.mimetype, fileKey); // Build ass metadata const assFile: AssFile = { fakeid: random({ length: UserConfig.config.idSize }), // todo: more generators id: nanoid(32), + fileKey, mimetype: bbFile.mimetype, filename: bbFile.filename, timestamp, uploader: '0', // todo: users - save: { local: destination }, + save: {}, sha256: '0' // todo: hashing }; - log.debug('File saved to', assFile.save.local!); + // Set the save location + if (!s3) assFile.save.local = destination; + else { + + // Using S3 doesn't move temp file, delete it now + await fs.rm(bbFile.file); + + // todo: get S3 save data + } + + log.debug('File saved to', !s3 ? assFile.save.local! : 'S3'); // todo: save metadata