From 29add713c39fb99c895a37dbe48b07589c7bd93f Mon Sep 17 00:00:00 2001 From: tycrek Date: Mon, 14 Mar 2022 19:21:22 -0600 Subject: [PATCH] Significantly improve loading data & config --- src/ass.ts | 28 +++++++++++----------------- src/data.ts | 16 +++++++++++----- src/routers/resource.ts | 14 +++++++------- src/routers/upload.ts | 4 ++-- src/utils.ts | 1 + 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/ass.ts b/src/ass.ts index 6a99e85..ddc33bf 100644 --- a/src/ass.ts +++ b/src/ass.ts @@ -1,22 +1,5 @@ import { ErrWrap } from './types/definitions'; import { Config, MagicNumbers, Package } from 'ass-json'; -let doSetup = null; -try { - // Check if config.json exists - require('../config.json'); -} catch (err) { - doSetup = require('./setup').doSetup; -} - -// Run first time setup if using Docker (pseudo-process, setup will be run with docker exec) -if (doSetup) { - doSetup(); - // @ts-ignore - return; -} - -// Load the config -const { host, port, useSsl, isProxied, s3enabled, frontendName, indexFile, useSia } = require('../config.json'); //#region Imports import fs from 'fs-extra'; @@ -28,6 +11,17 @@ import helmet from 'helmet'; import { path, log, getTrueHttp, getTrueDomain } from './utils'; //#endregion + +//#region Setup - Run first time setup if using Docker (pseudo-process, setup will be run with docker exec) +import { doSetup } from './setup'; +const configPath = path('config.json'); +if (!fs.existsSync(configPath)) { + doSetup(); + // @ts-ignore + return; +} +//#endregion + // Load the JSON const { host, port, useSsl, isProxied, s3enabled, frontendName, indexFile, useSia }: Config = fs.readJsonSync(path('config.json')); const { CODE_INTERNAL_SERVER_ERROR }: MagicNumbers = fs.readJsonSync(path('MagicNumbers.json')); diff --git a/src/data.ts b/src/data.ts index ce2c320..a053559 100644 --- a/src/data.ts +++ b/src/data.ts @@ -2,11 +2,17 @@ * Used for global data management */ -// Old data -const { JsonDataEngine } = require('@tycrek/papito'); +import fs from 'fs-extra'; +import { Config } from 'ass-json'; +import { JsonDataEngine } from '@tycrek/papito' + +let theData: any; // Actual data engine -const { dataEngine } = require('../config.json'); -const { _ENGINE_ } = require(dataEngine); +const { dataEngine }: Config = fs.readJsonSync('config.json'); +import(dataEngine) + .then(({ _ENGINE_ }) => theData = _ENGINE_(new JsonDataEngine())) + .catch(err => console.error(err)); -export const data = _ENGINE_(new JsonDataEngine()); +// Export a self-calling const function returning the data +export const data = ((): any => theData); diff --git a/src/routers/resource.ts b/src/routers/resource.ts index ea0ace3..1381d81 100644 --- a/src/routers/resource.ts +++ b/src/routers/resource.ts @@ -22,13 +22,13 @@ router.use((req: Request, res: Response, next) => { req.ass = { resourceId: escape(req.resourceId || '').split('.')[0] }; // If the ID is invalid, return 404. Otherwise, continue normally - data.has(req.ass.resourceId) + data().has(req.ass.resourceId) .then((has: boolean) => has ? next() : res.sendStatus(CODE_NOT_FOUND)) // skipcq: JS-0229 .catch(next); }); // View file -router.get('/', (req: Request, res: Response, next) => data.get(req.ass?.resourceId).then((fileData: FileData) => { +router.get('/', (req: Request, res: Response, next) => data().get(req.ass?.resourceId).then((fileData: FileData) => { const resourceId = req.ass!.resourceId; // Build OpenGraph meta tags @@ -62,7 +62,7 @@ router.get('/', (req: Request, res: Response, next) => data.get(req.ass?.resourc }).catch(next)); // Direct resource -router.get('/direct*', (req: Request, res: Response, next) => data.get(req.ass?.resourceId).then((fileData: FileData) => { +router.get('/direct*', (req: Request, res: Response, next) => data().get(req.ass?.resourceId).then((fileData: FileData) => { // Send file as an attachement for downloads if (req.query.download) res.header('Content-Disposition', `attachment; filename="${fileData.originalname}"`); @@ -89,7 +89,7 @@ router.get('/direct*', (req: Request, res: Response, next) => data.get(req.ass?. // Thumbnail response router.get('/thumbnail', (req: Request, res: Response, next) => - data.get(req.ass?.resourceId) + data().get(req.ass?.resourceId) .then(({ is, thumbnail }: { is: IsPossible, thumbnail: string }) => fs.readFile((!is || (is.image || is.video)) ? path(diskFilePath, 'thumbnails/', thumbnail) : is.audio ? 'views/ass-audio-icon.png' : 'views/ass-file-icon.png')) .then((fileData: Buffer) => res.type('jpg').send(fileData)) .catch(next)); @@ -98,7 +98,7 @@ router.get('/thumbnail', (req: Request, res: Response, next) => // https://oembed.com/ // https://old.reddit.com/r/discordapp/comments/82p8i6/a_basic_tutorial_on_how_to_get_the_most_out_of/ router.get('/oembed', (req: Request, res: Response, next) => - data.get(req.ass?.resourceId) + data().get(req.ass?.resourceId) .then((fileData: FileData) => res.type('json').send({ version: '1.0', @@ -117,7 +117,7 @@ router.get('/oembed', (req: Request, res: Response, next) => // Delete file router.get('/delete/:deleteId', (req: Request, res: Response, next) => { let oldName: string, oldType: string; // skipcq: JS-0119 - data.get(req.ass?.resourceId) + data().get(req.ass?.resourceId) .then((fileData: FileData) => { // Extract info for logs oldName = fileData.originalname; @@ -135,7 +135,7 @@ router.get('/delete/:deleteId', (req: Request, res: Response, next) => { (!fileData.is || (fileData.is.image || fileData.is.video)) && fs.existsSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail)) ? fs.rmSync(path(diskFilePath, 'thumbnails/', fileData.thumbnail)) : () => Promise.resolve()]); }) - .then(() => data.del(req.ass?.resourceId)) + .then(() => data().del(req.ass?.resourceId)) .then(() => (log.success('Deleted', oldName, oldType), res.type('text').send('File has been deleted!'))) // skipcq: JS-0090 .catch(next); }); diff --git a/src/routers/upload.ts b/src/routers/upload.ts index bfeb059..502822a 100644 --- a/src/routers/upload.ts +++ b/src/routers/upload.ts @@ -92,7 +92,7 @@ router.post('/', (req: Request, res: Response, next: Function) => { function genCheckId(resolve: Function, reject: Function) { const uniqueId = gen(); attempts.count++; - data.has(uniqueId) + data().has(uniqueId) .then((exists: boolean) => { log.debug('ID check', exists ? 'Taken' : 'Available'); return attempts.count - 1 >= attempts.max ? reject(new Error('No ID\'s remaining')) : exists ? genCheckId(resolve, reject) : resolve(uniqueId); @@ -106,7 +106,7 @@ router.post('/', (req: Request, res: Response, next: Function) => { resourceId = uniqueId; log.debug('Saving data', data.name); }) - .then(() => data.put(resourceId.split('.')[0], req.file)) + .then(() => data().put(resourceId.split('.')[0], req.file)) .then(() => { // Log the upload const logInfo = `${req.file!.originalname} (${req.file!.mimetype}, ${formatBytes(req.file!.size)})`; diff --git a/src/utils.ts b/src/utils.ts index d7a78b6..481cc7c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -14,6 +14,7 @@ const { HTTP, HTTPS, KILOBYTES } = require('../MagicNumbers.json'); // Catch config.json not existing when running setup script try { + // todo: fix this var { useSsl, port, domain, isProxied, diskFilePath, s3bucket, s3endpoint, s3usePathStyle } = require('../config.json'); // skipcq: JS-0239, JS-0102 } catch (ex) { // @ts-ignore