diff --git a/backend/data.ts b/backend/data.ts index 730f0bd..817c20d 100644 --- a/backend/data.ts +++ b/backend/data.ts @@ -101,3 +101,48 @@ export const setDataModeToSql = (): Promise => new Promise(async (resolve, await bothWriter(files, users); (issue) ? reject(new Error(issue)) : resolve(void 0); }); + +export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Promise => new Promise(async (resolve, reject) => { + try { + const useSql = false; + + if (sector === 'files') { + data = data as AssFile; + + // * 1: Save as files (image, video, etc) + const filesJson = await fs.readJson(PATHS.files) as FilesSchema; + + // Check if key already exists + if (filesJson.files[key] != null) return reject(new Error(`File key ${key} already exists`)); + + // Otherwise add the data + filesJson.files[key] = data; + + // Also save the key to the users file + const usersJson = await fs.readJson(PATHS.users) as UsersSchema; + // todo: uncomment this once users are implemented + // usersJson.users[data.uploader].files.push(key); + + // Save the files + await bothWriter(filesJson, usersJson); + } else { + data = data as AssUser; + + // * 2: Save as users + const usersJson = await fs.readJson(PATHS.users) as UsersSchema; + + // Check if key already exists + if (usersJson.users[key] != null) return reject(new Error(`User key ${key} already exists`)); + + // Otherwise add the data + usersJson.users[key] = data; + + await fs.writeJson(PATHS.users, usersJson, { spaces: '\t' }); + } + + log.info(`PUT data (${useSql ? 'SQL' : 'local JSON'})`, `${key}`, `${sector} sector`); + resolve(void 0); + } catch (err) { + reject(err); + } +}); diff --git a/backend/routers/index.ts b/backend/routers/index.ts index c323e47..f64c576 100644 --- a/backend/routers/index.ts +++ b/backend/routers/index.ts @@ -1,13 +1,13 @@ import fs from 'fs-extra'; import bb from 'express-busboy'; import { Router } from 'express'; -import { nanoid } from 'nanoid'; import { log } from '../log'; import { UserConfig } from '../UserConfig'; import { random } from '../generators'; import { BusBoyFile, AssFile } from 'ass'; import { getFileS3, uploadFileS3 } from '../s3'; import { Readable } from 'stream'; +import * as data from '../data'; const router = Router({ caseSensitive: true }); @@ -24,9 +24,6 @@ bb.extend(router, { // Render or redirect router.get('/', (req, res) => UserConfig.ready ? res.render('index') : res.redirect('/setup')); -// temp file map -const files = new Map(); - // Upload flow router.post('/', async (req, res) => { @@ -75,11 +72,10 @@ router.post('/', async (req, res) => { assFile.save.s3 = true; } - log.debug('File saved to', !s3 ? assFile.save.local! : 'S3'); - - // todo: save metadata - files.set(assFile.fakeid, assFile); + // * Save metadata + data.put('files', assFile.fakeid, assFile); + log.debug('File saved to', !s3 ? assFile.save.local! : 'S3'); return res.type('json').send({ resource: `${req.ass.host}/${assFile.fakeid}` }); } catch (err) { log.error('Failed to upload file', bbFile.filename);