feat: implement GET for both SQL and local storage

pull/243/head
Josh Moore 1 year ago
parent 858635f64d
commit 6532bc2545

@ -167,3 +167,14 @@ export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Prom
reject(err); reject(err);
} }
}); });
export const get = (sector: DataSector, key: NID): Promise<AssFile | AssUser | false> => 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);
}
});

@ -92,11 +92,12 @@ router.get('/direct/:fakeId', async (req, res) => {
// Get the ID // Get the ID
const fakeId = req.params.fakeId; const fakeId = req.params.fakeId;
if (!files.has(fakeId)) return res.status(404).send(); // Get the file metadata
else { const _data = await data.get('files', fakeId);
// Get file metadata if (!_data) return res.status(404).send();
const meta = files.get(fakeId)!; else {
const meta = _data as AssFile;
// File data can come from either S3 or local filesystem // File data can come from either S3 or local filesystem
let output: Readable | NodeJS.ReadableStream; let output: Readable | NodeJS.ReadableStream;

@ -113,4 +113,20 @@ VALUES ('${key}', '${JSON.stringify(data)}');
.catch((err) => reject(err)); .catch((err) => reject(err));
}); });
} }
public static get(table: TableNamesType, key: NID): Promise<UploadToken | AssFile | AssUser | undefined> {
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);
}
});
}
} }

Loading…
Cancel
Save