diff --git a/src/ass.ts b/src/ass.ts index 101ede6..97b31a8 100644 --- a/src/ass.ts +++ b/src/ass.ts @@ -45,7 +45,7 @@ const ROUTERS = { // Read users and data import { onStart as AuthOnStart, users } from './auth'; -import { data } from './data'; +import { onStart as DataOnStart, data } from './data'; //#endregion // Create thumbnails directory @@ -133,6 +133,7 @@ app.use((err: ErrWrap, _req: Request, res: Response) => log.error(err.message).e (async function start() { await AuthOnStart(); + await DataOnStart(); if (data() == null) setTimeout(start, 100); else log diff --git a/src/data.ts b/src/data.ts index a053559..c7a2a30 100644 --- a/src/data.ts +++ b/src/data.ts @@ -8,11 +8,18 @@ import { JsonDataEngine } from '@tycrek/papito' let theData: any; -// Actual data engine -const { dataEngine }: Config = fs.readJsonSync('config.json'); -import(dataEngine) - .then(({ _ENGINE_ }) => theData = _ENGINE_(new JsonDataEngine())) - .catch(err => console.error(err)); +/** + * Called by ass.ts on startup + * @since v0.14.2 + */ +export const onStart = () => new Promise((resolve, reject) => { + // Actual data engine + const { dataEngine }: Config = fs.readJsonSync('config.json'); + import(dataEngine) + .then(({ _ENGINE_ }) => theData = _ENGINE_(new JsonDataEngine())) + .then(resolve) + .catch(reject); +}); // Export a self-calling const function returning the data export const data = ((): any => theData); diff --git a/src/routers/upload.ts b/src/routers/upload.ts index f04ebd4..1bf8c4c 100644 --- a/src/routers/upload.ts +++ b/src/routers/upload.ts @@ -50,7 +50,7 @@ router.use('/', (err: ErrWrap, _req: Request, res: Response, next: Function) => // Process uploaded file router.post('/', (req: Request, res: Response, next: Function) => { // Load overrides - const trueDomain = getTrueDomain(req.headers['x-ass-domain']); + const trueDomain = getTrueDomain(req.headers['x-ass-domain']?.toString() ?? undefined); const generator = req.headers['x-ass-access']?.toString() || resourceIdType; // Save domain with file diff --git a/src/utils.ts b/src/utils.ts index abdb81a..0a48a43 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,4 @@ +import { Config } from 'ass-json'; import { FileData } from './types/definitions'; import fs from 'fs-extra'; import Path from 'path'; @@ -16,7 +17,9 @@ 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 + const configPath = Path.join(process.cwd(), 'config.json'); + if (!fs.existsSync(configPath)) throw new Error('Config file not found'); + var { useSsl, port, domain, isProxied, diskFilePath, s3bucket, s3endpoint, s3usePathStyle }: Config = fs.readJsonSync(configPath); } catch (ex) { // @ts-ignore if (ex.code !== 'MODULE_NOT_FOUND' || !ex.toString().includes('Unexpected end')) console.error(ex);