delete is working
This commit is contained in:
parent
c2f36e9723
commit
0f0bc6c4e5
3 changed files with 90 additions and 25 deletions
105
src/index.ts
105
src/index.ts
|
|
@ -6,8 +6,6 @@ import cookie from "@elysiajs/cookie";
|
||||||
import { unlink } from "node:fs/promises";
|
import { unlink } from "node:fs/promises";
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { jwt } from "@elysiajs/jwt";
|
import { jwt } from "@elysiajs/jwt";
|
||||||
// import { Lucia } from "lucia";
|
|
||||||
// import { BunSQLiteAdapter } from "@lucia-auth/adapter-sqlite";
|
|
||||||
|
|
||||||
const db = new Database("./mydb.sqlite");
|
const db = new Database("./mydb.sqlite");
|
||||||
const uploadsDir = "./uploads/";
|
const uploadsDir = "./uploads/";
|
||||||
|
|
@ -19,14 +17,12 @@ CREATE TABLE IF NOT EXISTS users (
|
||||||
email TEXT NOT NULL,
|
email TEXT NOT NULL,
|
||||||
password TEXT NOT NULL
|
password TEXT NOT NULL
|
||||||
);
|
);
|
||||||
`);
|
CREATE TABLE IF NOT EXISTS jobs (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
const basicAuthModel = new Elysia().model({
|
user_id INTEGER NOT NULL,
|
||||||
basicAuthModel: t.Object({
|
job_id TEXT NOT NULL,
|
||||||
email: t.String(),
|
date_created TEXT NOT NULL
|
||||||
password: t.String(),
|
);`);
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const app = new Elysia()
|
const app = new Elysia()
|
||||||
.use(cookie())
|
.use(cookie())
|
||||||
|
|
@ -148,7 +144,7 @@ const app = new Elysia()
|
||||||
Location: "/login",
|
Location: "/login",
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.get("/", async ({ jwt, set, cookie: { auth } }) => {
|
.get("/", async ({ jwt, set, cookie: { auth, jobId } }) => {
|
||||||
// validate jwt
|
// validate jwt
|
||||||
const user = await jwt.verify(auth.value);
|
const user = await jwt.verify(auth.value);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
|
@ -159,25 +155,90 @@ const app = new Elysia()
|
||||||
};
|
};
|
||||||
return;
|
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");
|
return Bun.file("src/pages/index.html");
|
||||||
})
|
})
|
||||||
.post("/upload", async (ctx) => {
|
.post("/upload", async ({ body, set, jwt, cookie: { auth, jobId } }) => {
|
||||||
console.log(ctx.body);
|
// validate jwt
|
||||||
if (ctx.body?.file) {
|
const user = await jwt.verify(auth.value);
|
||||||
await Bun.write(`${uploadsDir}${ctx.body.file.name}`, ctx.body.file);
|
if (!user) {
|
||||||
} else if (ctx.body?.files) {
|
// redirect to login
|
||||||
if (Array.isArray(ctx.body.files)) {
|
set.status = 302;
|
||||||
for (const file of ctx.body.files) {
|
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);
|
console.log(file);
|
||||||
await Bun.write(`${uploadsDir}${file.name}`, file);
|
await Bun.write(`${userUploadsDir}${file.name}`, file);
|
||||||
|
// filesUploaded.push(file.name);
|
||||||
}
|
}
|
||||||
} else {
|
} 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) => {
|
.post("/delete", async ({ body, set, jwt, cookie: { auth, jobId } }) => {
|
||||||
await unlink(`${uploadsDir}${ctx.params.file}`);
|
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);
|
.listen(3000);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,7 @@
|
||||||
<option>SVG</option>
|
<option>SVG</option>
|
||||||
<option>PDF</option>
|
<option>PDF</option>
|
||||||
<option>DOCX</option>
|
<option>DOCX</option>
|
||||||
|
<option>Yaml</option>
|
||||||
</select>
|
</select>
|
||||||
</article>
|
</article>
|
||||||
<input type="submit" value="Convert">
|
<input type="submit" value="Convert">
|
||||||
|
|
|
||||||
|
|
@ -31,13 +31,16 @@ fileInput.addEventListener("change", (e) => {
|
||||||
|
|
||||||
// Add a onclick for the delete button
|
// Add a onclick for the delete button
|
||||||
const deleteRow = (target) => {
|
const deleteRow = (target) => {
|
||||||
const fileName = target.parentElement.parentElement.children[0].textContent;
|
const filename = target.parentElement.parentElement.children[0].textContent;
|
||||||
const row = target.parentElement.parentElement;
|
const row = target.parentElement.parentElement;
|
||||||
row.remove();
|
row.remove();
|
||||||
|
|
||||||
fetch("/delete", {
|
fetch("/delete", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ fileName }),
|
body: JSON.stringify({ filename: filename }),
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.then((res) => res.json())
|
.then((res) => res.json())
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue