mirror of
https://github.com/C4illin/ConvertX.git
synced 2026-06-29 15:35:49 +00:00
delete is working
This commit is contained in:
105
src/index.ts
105
src/index.ts
@@ -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);
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
<option>SVG</option>
|
||||
<option>PDF</option>
|
||||
<option>DOCX</option>
|
||||
<option>Yaml</option>
|
||||
</select>
|
||||
</article>
|
||||
<input type="submit" value="Convert">
|
||||
|
||||
@@ -31,13 +31,16 @@ fileInput.addEventListener("change", (e) => {
|
||||
|
||||
// Add a onclick for the delete button
|
||||
const deleteRow = (target) => {
|
||||
const fileName = target.parentElement.parentElement.children[0].textContent;
|
||||
const filename = target.parentElement.parentElement.children[0].textContent;
|
||||
const row = target.parentElement.parentElement;
|
||||
row.remove();
|
||||
|
||||
|
||||
fetch("/delete", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ fileName }),
|
||||
body: JSON.stringify({ filename: filename }),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.then((data) => {
|
||||
|
||||
Reference in New Issue
Block a user