|
@@ -6,8 +6,6 @@ import cookie from "@elysiajs/cookie";
|
|
|
import { unlink } from "node:fs/promises";
|
|
|
import { randomUUID } from "node:crypto";
|
|
|
import { jwt } from "@elysiajs/jwt";
|
|
|
-// import { Lucia } from "lucia";
|
|
|
-// import { BunSQLiteAdapter } from "@lucia-auth/adapter-sqlite";
|
|
|
|
|
|
const db = new Database("./mydb.sqlite");
|
|
|
const uploadsDir = "./uploads/";
|
|
@@ -19,14 +17,12 @@ CREATE TABLE IF NOT EXISTS users (
|
|
|
email TEXT NOT NULL,
|
|
|
password TEXT NOT NULL
|
|
|
);
|
|
|
-`);
|
|
|
-
|
|
|
-const basicAuthModel = new Elysia().model({
|
|
|
- basicAuthModel: t.Object({
|
|
|
- email: t.String(),
|
|
|
- password: t.String(),
|
|
|
- }),
|
|
|
-});
|
|
|
+CREATE TABLE IF NOT EXISTS jobs (
|
|
|
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
+ user_id INTEGER NOT NULL,
|
|
|
+ job_id TEXT NOT NULL,
|
|
|
+ date_created TEXT NOT NULL
|
|
|
+);`);
|
|
|
|
|
|
const app = new Elysia()
|
|
|
.use(cookie())
|
|
@@ -148,7 +144,7 @@ const app = new Elysia()
|
|
|
Location: "/login",
|
|
|
};
|
|
|
})
|
|
|
- .get("/", async ({ jwt, set, cookie: { auth } }) => {
|
|
|
+ .get("/", async ({ jwt, set, cookie: { auth, jobId } }) => {
|
|
|
// validate jwt
|
|
|
const user = await jwt.verify(auth.value);
|
|
|
if (!user) {
|
|
@@ -159,25 +155,90 @@ const app = new Elysia()
|
|
|
};
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+ // make sure user exists in db
|
|
|
+ const existingUser = await db
|
|
|
+ .query("SELECT * FROM users WHERE id = ?")
|
|
|
+ .get(user.id);
|
|
|
+
|
|
|
+ if (!existingUser) {
|
|
|
+ // redirect to login and clear cookie
|
|
|
+ auth.remove();
|
|
|
+ set.status = 302;
|
|
|
+ set.headers = {
|
|
|
+ Location: "/login",
|
|
|
+ };
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // create a unique job id
|
|
|
+ jobId.set({
|
|
|
+ value: randomUUID(),
|
|
|
+ httpOnly: true,
|
|
|
+ secure: true,
|
|
|
+ maxAge: 24 * 60 * 60,
|
|
|
+ sameSite: "strict",
|
|
|
+ });
|
|
|
+
|
|
|
+ // insert job id into db
|
|
|
+ db.run(
|
|
|
+ "INSERT INTO jobs (user_id, job_id, date_created) VALUES (?, ?, ?)",
|
|
|
+ user.id,
|
|
|
+ jobId.value,
|
|
|
+ new Date().toISOString(),
|
|
|
+ );
|
|
|
+
|
|
|
return Bun.file("src/pages/index.html");
|
|
|
})
|
|
|
- .post("/upload", async (ctx) => {
|
|
|
- console.log(ctx.body);
|
|
|
- if (ctx.body?.file) {
|
|
|
- await Bun.write(`${uploadsDir}${ctx.body.file.name}`, ctx.body.file);
|
|
|
- } else if (ctx.body?.files) {
|
|
|
- if (Array.isArray(ctx.body.files)) {
|
|
|
- for (const file of ctx.body.files) {
|
|
|
+ .post("/upload", async ({ body, set, jwt, cookie: { auth, jobId } }) => {
|
|
|
+ // validate jwt
|
|
|
+ const user = await jwt.verify(auth.value);
|
|
|
+ if (!user) {
|
|
|
+ // redirect to login
|
|
|
+ set.status = 302;
|
|
|
+ set.headers = {
|
|
|
+ Location: "/login",
|
|
|
+ };
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // let filesUploaded = [];
|
|
|
+
|
|
|
+ const userUploadsDir = `${uploadsDir}${user.id}/${jobId.value}/`;
|
|
|
+
|
|
|
+ if (body?.file) {
|
|
|
+ await Bun.write(`${userUploadsDir}${body.file.name}`, body.file);
|
|
|
+ // filesUploaded.push(body.file.name);
|
|
|
+ } else if (body?.files) {
|
|
|
+ if (Array.isArray(body.files)) {
|
|
|
+ for (const file of body.files) {
|
|
|
console.log(file);
|
|
|
- await Bun.write(`${uploadsDir}${file.name}`, file);
|
|
|
+ await Bun.write(`${userUploadsDir}${file.name}`, file);
|
|
|
+ // filesUploaded.push(file.name);
|
|
|
}
|
|
|
} else {
|
|
|
- await Bun.write(`${uploadsDir}${ctx.body.files.name}`, ctx.body.files);
|
|
|
+ await Bun.write(`${userUploadsDir}${body.files.name}`, body.files);
|
|
|
+ // filesUploaded.push(body.files.name);
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
- .post("/delete/:file", async (ctx) => {
|
|
|
- await unlink(`${uploadsDir}${ctx.params.file}`);
|
|
|
+ .post("/delete", async ({ body, set, jwt, cookie: { auth, jobId } }) => {
|
|
|
+ const user = await jwt.verify(auth.value);
|
|
|
+ if (!user) {
|
|
|
+ // redirect to login
|
|
|
+ set.status = 302;
|
|
|
+ set.headers = {
|
|
|
+ Location: "/login",
|
|
|
+ };
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const userUploadsDir = `${uploadsDir}${user.id}/${jobId.value}/`;
|
|
|
+
|
|
|
+ await unlink(`${userUploadsDir}${body.filename}`);
|
|
|
+ })
|
|
|
+ .post("/convert", async (ctx) => {
|
|
|
+ console.log(ctx.body);
|
|
|
})
|
|
|
.listen(3000);
|
|
|
|