Significantly improve loading data & config

pull/125/head
tycrek 3 years ago
parent dbb024815e
commit 29add713c3
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -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'));

@ -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);

@ -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);
});

@ -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)})`;

@ -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

Loading…
Cancel
Save