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 // * Default files.json
await createEmptyJson(PATHS.files, { await createEmptyJson(PATHS.files, {
files: [], files: {},
useSql: false,
meta: {} meta: {}
} as FilesSchema); } as FilesSchema);
// * Default users.json // * Default users.json
await createEmptyJson(PATHS.users, { await createEmptyJson(PATHS.users, {
tokens: [], tokens: [],
users: [], users: {},
cliKey: nanoid(32), cliKey: nanoid(32),
useSql: false,
meta: {} meta: {}
} as UsersSchema); } as UsersSchema);

@ -51,14 +51,13 @@ router.post('/', async (req, res) => {
try { try {
// Move the file // * Move the file
if (!s3) await fs.move(bbFile.file, destination); if (!s3) await fs.move(bbFile.file, destination);
else await uploadFileS3(await fs.readFile(bbFile.file), bbFile.mimetype, fileKey); else await uploadFileS3(await fs.readFile(bbFile.file), bbFile.mimetype, fileKey);
// Build ass metadata // Build ass metadata
const assFile: AssFile = { const assFile: AssFile = {
fakeid: random({ length: UserConfig.config.idSize }), // todo: more generators fakeid: random({ length: UserConfig.config.idSize }), // todo: more generators
id: nanoid(32),
fileKey, fileKey,
mimetype: bbFile.mimetype, mimetype: bbFile.mimetype,
filename: bbFile.filename, filename: bbFile.filename,
@ -71,11 +70,8 @@ router.post('/', async (req, res) => {
// Set the save location // Set the save location
if (!s3) assFile.save.local = destination; if (!s3) assFile.save.local = destination;
else { else {
// Using S3 doesn't move temp file, delete it now // Using S3 doesn't move temp file, delete it now
await fs.rm(bbFile.file); await fs.rm(bbFile.file);
// todo: get S3 save data
assFile.save.s3 = true; assFile.save.s3 = true;
} }

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

16
common/types.d.ts vendored

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

Loading…
Cancel
Save