Lint errors
This commit is contained in:
parent
0c72d5a37c
commit
1d82c4ffb9
6 changed files with 102 additions and 97 deletions
|
|
@ -6,9 +6,9 @@ const db = new Database("./data/mydb.sqlite", { create: true });
|
||||||
|
|
||||||
function getTableInfo(tableName: string) {
|
function getTableInfo(tableName: string) {
|
||||||
try {
|
try {
|
||||||
return db.query(`PRAGMA table_info('${tableName}')`).all() as Array<{ name: string}>;
|
return db.query(`PRAGMA table_info('${tableName}')`).all() as Array<{ name: string }>;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error getting table info for ${tableName}:`, error)
|
console.error(`Error getting table info for ${tableName}:`, error);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -64,7 +64,9 @@ try {
|
||||||
console.log("Added column file_names.storage_key");
|
console.log("Added column file_names.storage_key");
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentVersion = (db.query("PRAGMA user_version").get() as { user_version?: number}).user_version ?? 0;
|
const currentVersion =
|
||||||
|
(db.query("PRAGMA user_version").get() as { user_version?: number }).user_version ?? 0;
|
||||||
|
|
||||||
if (currentVersion < 2) {
|
if (currentVersion < 2) {
|
||||||
db.exec("PRAGMA user_version = 2;");
|
db.exec("PRAGMA user_version = 2;");
|
||||||
console.log(`Updated database to version 2 (was ${currentVersion}).`);
|
console.log(`Updated database to version 2 (was ${currentVersion}).`);
|
||||||
|
|
|
||||||
|
|
@ -24,19 +24,20 @@ export const download = new Elysia()
|
||||||
const fileName = sanitize(decodeURIComponent(params.fileName));
|
const fileName = sanitize(decodeURIComponent(params.fileName));
|
||||||
|
|
||||||
const fileRow = db
|
const fileRow = db
|
||||||
.query(`
|
.query(
|
||||||
|
`
|
||||||
SELECT storage_key, file_name
|
SELECT storage_key, file_name
|
||||||
FROM file_names
|
FROM file_names
|
||||||
WHERE job_id = ? AND file_name = ?
|
WHERE job_id = ? AND file_name = ?
|
||||||
`,
|
`,
|
||||||
)
|
)
|
||||||
.get(params.jobId, fileName) as { storage_key?: string; file_name?: string } | undefined;
|
.get(params.jobId, fileName) as { storage_key?: string; file_name?: string } | undefined;
|
||||||
if (!fileRow) {
|
if (!fileRow) {
|
||||||
return redirect(`${WEBROOT}/results`, 302);
|
return redirect(`${WEBROOT}/results`, 302);
|
||||||
}
|
}
|
||||||
|
|
||||||
const storage = getStorage();
|
const storage = getStorage();
|
||||||
const stream = await storage.getStream(fileRow.storage_key!);
|
const stream = storage.getStream(fileRow.storage_key!);
|
||||||
|
|
||||||
return new Response(stream, {
|
return new Response(stream, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -57,7 +58,7 @@ export const download = new Elysia()
|
||||||
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
||||||
.get(user.id, params.jobId);
|
.get(user.id, params.jobId);
|
||||||
|
|
||||||
if(!job) {
|
if (!job) {
|
||||||
return redirect(`${WEBROOT}/results`, 302);
|
return redirect(`${WEBROOT}/results`, 302);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,10 +31,12 @@ export const upload = new Elysia().use(userService).post(
|
||||||
const buffer = Buffer.from(await file.arrayBuffer());
|
const buffer = Buffer.from(await file.arrayBuffer());
|
||||||
await storage.save(storageKey, buffer);
|
await storage.save(storageKey, buffer);
|
||||||
|
|
||||||
db.query(`
|
db.query(
|
||||||
|
`
|
||||||
INSERT INTO file_names (job_id, file_name, storage_key)
|
INSERT INTO file_names (job_id, file_name, storage_key)
|
||||||
VALUES (?, ?, ?)
|
VALUES (?, ?, ?)
|
||||||
`).run(jobIdValue, sanitizedFileName, storageKey);
|
`,
|
||||||
|
).run(jobIdValue, sanitizedFileName, storageKey);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (body?.file) {
|
if (body?.file) {
|
||||||
|
|
@ -43,7 +45,7 @@ export const upload = new Elysia().use(userService).post(
|
||||||
await saveFile(file);
|
await saveFile(file);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await saveFile(body.file);;
|
await saveFile(body.file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,42 +3,42 @@ import { promises as fs } from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
export class LocalStorageAdapter implements IStorageAdapter {
|
export class LocalStorageAdapter implements IStorageAdapter {
|
||||||
private baseDir: string;
|
private baseDir: string;
|
||||||
|
|
||||||
constructor(baseDir: string) {
|
constructor(baseDir: string) {
|
||||||
this.baseDir = baseDir;
|
this.baseDir = baseDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
async save(key: string, data: Buffer): Promise<string> {
|
||||||
|
const fullPath = path.join(this.baseDir, key);
|
||||||
|
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
||||||
|
await fs.writeFile(fullPath, data);
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(key: string): Promise<Buffer> {
|
||||||
|
const fullPath = path.join(this.baseDir, key);
|
||||||
|
return fs.readFile(fullPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(key: string): Promise<void> {
|
||||||
|
const fullPath = path.join(this.baseDir, key);
|
||||||
|
try {
|
||||||
|
await fs.unlink(fullPath);
|
||||||
|
} catch (error) {
|
||||||
|
const err = error as NodeJS.ErrnoException;
|
||||||
|
if (err?.code === "ENOENT" || err?.code === "ENOTDIR") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error(`Failed to delete file at ${fullPath}: `, error);
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async save(key: string, data: Buffer): Promise<string> {
|
getStream(key: string): ReadableStream<Uint8Array> {
|
||||||
const fullPath = path.join(this.baseDir, key);
|
const fullPath = path.join(this.baseDir, key);
|
||||||
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
const file = Bun.file(fullPath);
|
||||||
await fs.writeFile(fullPath, data);
|
return file.stream();
|
||||||
return key;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async get(key: string): Promise<Buffer> {
|
|
||||||
const fullPath = path.join(this.baseDir, key);
|
|
||||||
return fs.readFile(fullPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
async delete(key: string): Promise<void> {
|
|
||||||
const fullPath = path.join(this.baseDir, key);
|
|
||||||
try {
|
|
||||||
await fs.unlink(fullPath);
|
|
||||||
} catch (error) {
|
|
||||||
const err = error as NodeJS.ErrnoException;
|
|
||||||
if (err?.code === "ENOENT" || err?.code === "ENOTDIR") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.error(`Failed to delete file at ${fullPath}: `, error);
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getStream(key: string): ReadableStream<Uint8Array> {
|
|
||||||
const fullPath = path.join(this.baseDir, key);
|
|
||||||
const file = Bun.file(fullPath);
|
|
||||||
return file.stream();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -2,43 +2,43 @@ import { s3, S3File } from "bun";
|
||||||
import { IStorageAdapter } from "./index";
|
import { IStorageAdapter } from "./index";
|
||||||
|
|
||||||
export class S3StorageAdapter implements IStorageAdapter {
|
export class S3StorageAdapter implements IStorageAdapter {
|
||||||
private bucket: string;
|
private bucket: string;
|
||||||
|
|
||||||
constructor(bucket: string) {
|
constructor(bucket: string) {
|
||||||
this.bucket = bucket;
|
this.bucket = bucket;
|
||||||
}
|
}
|
||||||
|
|
||||||
async save(key: string, data: Buffer): Promise<string> {
|
async save(key: string, data: Buffer): Promise<string> {
|
||||||
const file: S3File = s3.file(key, {
|
const file: S3File = s3.file(key, {
|
||||||
bucket: this.bucket,
|
bucket: this.bucket,
|
||||||
acl: "private",
|
acl: "private",
|
||||||
});
|
});
|
||||||
|
|
||||||
await file.write(data);
|
await file.write(data);
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(key: string): Promise<Buffer> {
|
async get(key: string): Promise<Buffer> {
|
||||||
const file: S3File = s3.file(key, {
|
const file: S3File = s3.file(key, {
|
||||||
bucket: this.bucket,
|
bucket: this.bucket,
|
||||||
});
|
});
|
||||||
|
|
||||||
const buf = await file.bytes();
|
const buf = await file.bytes();
|
||||||
return Buffer.from(buf);
|
return Buffer.from(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(key: string): Promise<void> {
|
async delete(key: string): Promise<void> {
|
||||||
const file: S3File = s3.file(key, {
|
const file: S3File = s3.file(key, {
|
||||||
bucket: this.bucket,
|
bucket: this.bucket,
|
||||||
})
|
});
|
||||||
|
|
||||||
await file.delete();
|
await file.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
getStream(key: string): ReadableStream<Uint8Array> {
|
getStream(key: string): ReadableStream<Uint8Array> {
|
||||||
const file: S3File = s3.file(key, {
|
const file: S3File = s3.file(key, {
|
||||||
bucket: this.bucket
|
bucket: this.bucket,
|
||||||
});
|
});
|
||||||
return file.stream();
|
return file.stream();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,26 +2,26 @@ import { LocalStorageAdapter } from "./LocalStorageAdapter";
|
||||||
import { S3StorageAdapter } from "./S3StorageAdapter";
|
import { S3StorageAdapter } from "./S3StorageAdapter";
|
||||||
|
|
||||||
export interface IStorageAdapter {
|
export interface IStorageAdapter {
|
||||||
save(key: string, data: Buffer): Promise<string>;
|
save(key: string, data: Buffer): Promise<string>;
|
||||||
get(key: string): Promise<Buffer>;
|
get(key: string): Promise<Buffer>;
|
||||||
delete(key: string): Promise<void>;
|
delete(key: string): Promise<void>;
|
||||||
getStream(key: string): ReadableStream<Uint8Array>;
|
getStream(key: string): ReadableStream<Uint8Array>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getStorageType(): "local" | "s3" {
|
export function getStorageType(): "local" | "s3" {
|
||||||
return process.env.STORAGE_TYPE === "s3" ? "s3" : "local";
|
return process.env.STORAGE_TYPE === "s3" ? "s3" : "local";
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getStorage(): IStorageAdapter {
|
export function getStorage(): IStorageAdapter {
|
||||||
if (getStorageType() === "s3") {
|
if (getStorageType() === "s3") {
|
||||||
const bucket = process.env.S3_BUCKET_NAME;
|
const bucket = process.env.S3_BUCKET_NAME;
|
||||||
if (!bucket) {
|
if (!bucket) {
|
||||||
throw new Error("S3_BUCKET_NAME must be set when STORAGE_TYPE=s3");
|
throw new Error("S3_BUCKET_NAME must be set when STORAGE_TYPE=s3");
|
||||||
}
|
|
||||||
|
|
||||||
return new S3StorageAdapter(bucket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const baseDir = process.env.LOCAL_STORAGE_PATH || "./data/storage";
|
return new S3StorageAdapter(bucket);
|
||||||
return new LocalStorageAdapter(baseDir);
|
}
|
||||||
|
|
||||||
|
const baseDir = process.env.LOCAL_STORAGE_PATH || "./data/storage";
|
||||||
|
return new LocalStorageAdapter(baseDir);
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue