feat: adjusted some types

pull/243/head
Josh Moore 1 year ago
parent fa40db0662
commit 6efe86fe9b

@ -44,15 +44,17 @@ export const ensureFiles = (): Promise<void> => new Promise(async (resolve, reje
// * Default files.json
await createEmptyJson(PATHS.files, {
files: [],
files: {},
useSql: false,
meta: {}
} as FilesSchema);
// * Default users.json
await createEmptyJson(PATHS.users, {
tokens: [],
users: [],
users: {},
cliKey: nanoid(32),
useSql: false,
meta: {}
} as UsersSchema);

@ -51,14 +51,13 @@ router.post('/', async (req, res) => {
try {
// Move the file
// * Move the file
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,
@ -71,11 +70,8 @@ router.post('/', async (req, res) => {
// 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
assFile.save.s3 = true;
}

@ -2,6 +2,8 @@ import mysql, { Pool } from 'mysql2/promise';
import { UserConfig } from '../UserConfig';
import { log } from '../log';
type TableNamesType = 'assfiles' | 'assusers' | 'asstokens';
export class MySql {
private static _pool: Pool;
@ -45,18 +47,20 @@ export class MySql {
} else {
// There's at least one row, do further checks
const tablesExist = { files: false, users: false };
const tablesExist = { files: false, users: false, tokens: false };
// Check which tables ACTUALLY do exist
for (let row of rows_tableData) {
const table = row[`Tables_in_${UserConfig.config.sql!.mySql!.database}`] as 'assfiles' | 'assusers';
const table = row[`Tables_in_${UserConfig.config.sql!.mySql!.database}`
] as TableNamesType;
if (table === 'assfiles') tablesExist.files = true;
if (table === 'assusers') tablesExist.users = true;
if (table === 'asstokens') tablesExist.tokens = true;
// ! Don't use `= table === ''` because this is a loop
}
// Mini-function for creating a one-off table
const createOneTable = async (name: string) => {
const createOneTable = async (name: TableNamesType) => {
log.warn('MySQL', `Table '${name}' missing, creating`);
await MySql._tableManager('create', name);
log.success('MySQL', `Table '${name}' created`);
@ -65,6 +69,7 @@ export class MySql {
// Check & create tables
if (!tablesExist.files) await createOneTable('assfiles');
if (!tablesExist.users) await createOneTable('assusers');
if (!tablesExist.users) await createOneTable('asstokens');
// ! temp: drop tables for testing
/* await MySql._tableManager('drop', 'assfiles');

16
common/types.d.ts vendored

@ -107,11 +107,7 @@ declare module 'ass' {
/**
* Public identifier used in the URL
*/
fakeid: string;
/**
* Internal identifier
*/
id: NID;
fakeid: NID;
/**
* Unique-but-human-readable ID. Combination of Epoch and filename.
* This allows users to search for their file while also avoiding conflicts.
@ -170,7 +166,10 @@ declare module 'ass' {
* JSON schema for files.json
*/
interface FilesSchema {
files: AssFile[];
files: {
[key: NID]: AssFile;
}
useSql: boolean;
meta: { [key: string]: any };
}
@ -179,8 +178,11 @@ declare module 'ass' {
*/
interface UsersSchema {
tokens: UploadToken[];
users: AssUser[];
users: {
[key: NID]: AssUser;
};
cliKey: string;
useSql: boolean;
meta: { [key: string]: any };
}
}

Loading…
Cancel
Save