Skip to content

Commit 4d65cc7

Browse files
feat: add button and API endpoint to delete job (#423)
1 parent 1a4e2de commit 4d65cc7

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

src/icons/delete.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function DeleteIcon() {
2+
return (
3+
<svg
4+
xmlns="http://www.w3.org/2000/svg"
5+
fill="none"
6+
viewBox="0 0 24 24"
7+
strokeWidth="1.5"
8+
stroke="currentColor"
9+
class={`size-6`}
10+
>
11+
<path
12+
strokeLinecap="round"
13+
strokeLinejoin="round"
14+
d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"
15+
/>
16+
</svg>
17+
);
18+
}

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { AUTO_DELETE_EVERY_N_HOURS, WEBROOT } from "./helpers/env";
1010
import { chooseConverter } from "./pages/chooseConverter";
1111
import { convert } from "./pages/convert";
1212
import { deleteFile } from "./pages/deleteFile";
13+
import { deleteJob } from "./pages/deleteJob";
1314
import { download } from "./pages/download";
1415
import { history } from "./pages/history";
1516
import { listConverters } from "./pages/listConverters";
@@ -42,6 +43,7 @@ const app = new Elysia({
4243
.use(history)
4344
.use(convert)
4445
.use(download)
46+
.use(deleteJob)
4547
.use(results)
4648
.use(deleteFile)
4749
.use(listConverters)

src/pages/deleteJob.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { rmSync } from "node:fs";
2+
import { Elysia } from "elysia";
3+
import { outputDir, uploadsDir } from "..";
4+
import db from "../db/db";
5+
import { WEBROOT } from "../helpers/env";
6+
import { userService } from "./user";
7+
import { Jobs } from "../db/types";
8+
9+
export const deleteJob = new Elysia().use(userService).get(
10+
"/delete/:userId/:jobId",
11+
async ({ params, redirect, user }) => {
12+
const job = db
13+
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
14+
.as(Jobs)
15+
.get(user.id, params.jobId);
16+
17+
if (!job) {
18+
return redirect(`${WEBROOT}/results`, 302);
19+
}
20+
21+
// delete the directories
22+
rmSync(`${outputDir}${job.user_id}/${job.id}`, {
23+
recursive: true,
24+
force: true,
25+
});
26+
rmSync(`${uploadsDir}${job.user_id}/${job.id}`, {
27+
recursive: true,
28+
force: true,
29+
});
30+
31+
// delete the job
32+
db.query("DELETE FROM jobs WHERE id = ?").run(job.id);
33+
return redirect(`${WEBROOT}/history`, 302);
34+
},
35+
{
36+
auth: true,
37+
},
38+
);

src/pages/results.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import db from "../db/db";
66
import { Filename, Jobs } from "../db/types";
77
import { ALLOW_UNAUTHENTICATED, WEBROOT } from "../helpers/env";
88
import { DownloadIcon } from "../icons/download";
9+
import { DeleteIcon } from "../icons/delete";
910
import { EyeIcon } from "../icons/eye";
1011
import { userService } from "./user";
1112

@@ -39,6 +40,14 @@ function ResultsArticle({
3940
<button class="flex btn-primary flex-row gap-2 text-contrast" onclick="downloadAll()">
4041
<DownloadIcon /> <p>All</p>
4142
</button>
43+
<a
44+
style={files.length !== job.num_files ? "pointer-events: none;" : ""}
45+
class="flex btn-primary flex-row gap-2 text-contrast"
46+
href={`${WEBROOT}/delete/${user.id}/${job.id}`}
47+
{...(files.length !== job.num_files ? { disabled: true, "aria-busy": "true" } : "")}
48+
>
49+
<DeleteIcon /> <p>Delete</p>
50+
</a>
4251
</div>
4352
</div>
4453
<progress

0 commit comments

Comments
 (0)