diff --git a/backend/data.ts b/backend/data.ts index c549279..36505f8 100644 --- a/backend/data.ts +++ b/backend/data.ts @@ -44,15 +44,17 @@ export const ensureFiles = (): Promise => 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); diff --git a/backend/routers/index.ts b/backend/routers/index.ts index 7b88e28..c323e47 100644 --- a/backend/routers/index.ts +++ b/backend/routers/index.ts @@ -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; } diff --git a/backend/sql/mysql.ts b/backend/sql/mysql.ts index 6f07970..fe975a0 100644 --- a/backend/sql/mysql.ts +++ b/backend/sql/mysql.ts @@ -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'); diff --git a/common/types.d.ts b/common/types.d.ts index 84fe8f8..9c895a5 100644 --- a/common/types.d.ts +++ b/common/types.d.ts @@ -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 }; } }