diff --git a/backend/data.ts b/backend/data.ts index f3998db..dd6aa45 100644 --- a/backend/data.ts +++ b/backend/data.ts @@ -167,3 +167,14 @@ export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Prom reject(err); } }); + +export const get = (sector: DataSector, key: NID): Promise => new Promise(async (resolve, reject) => { + try { + const data: AssFile | AssUser | undefined = (MySql.ready) + ? (await MySql.get(sector === 'files' ? 'assfiles' : 'assusers', key) as AssFile | AssUser | undefined) + : (await fs.readJson(PATHS[sector]))[sector][key]; + (!data) ? reject(false) : resolve(data); + } catch (err) { + reject(err); + } +}); diff --git a/backend/routers/index.ts b/backend/routers/index.ts index f64c576..6090e77 100644 --- a/backend/routers/index.ts +++ b/backend/routers/index.ts @@ -92,11 +92,12 @@ router.get('/direct/:fakeId', async (req, res) => { // Get the ID const fakeId = req.params.fakeId; - if (!files.has(fakeId)) return res.status(404).send(); - else { + // Get the file metadata + const _data = await data.get('files', fakeId); - // Get file metadata - const meta = files.get(fakeId)!; + if (!_data) return res.status(404).send(); + else { + const meta = _data as AssFile; // File data can come from either S3 or local filesystem let output: Readable | NodeJS.ReadableStream; diff --git a/backend/sql/mysql.ts b/backend/sql/mysql.ts index 2379b0d..d4e5122 100644 --- a/backend/sql/mysql.ts +++ b/backend/sql/mysql.ts @@ -113,4 +113,20 @@ VALUES ('${key}', '${JSON.stringify(data)}'); .catch((err) => reject(err)); }); } + + public static get(table: TableNamesType, key: NID): Promise { + return new Promise(async (resolve, reject) => { + try { + // Run query + const [rowz, _fields] = await MySql._pool.query(`SELECT Data FROM ${table} WHERE NanoID = '${key}';`); + + // Disgustingly interpret the query results + const rows_tableData = (rowz as unknown as { [key: string]: string }[])[0] as unknown as ({ Data: UploadToken | AssFile | AssUser | undefined }); + + resolve(rows_tableData?.Data ?? undefined); + } catch (err) { + reject(err); + } + }); + } }