chore: use auth macro instead of checking it on every path

This commit is contained in:
C4illin 2025-10-02 17:29:58 +02:00
parent 13d9ce09a4
commit c9b65c7652
9 changed files with 69 additions and 113 deletions

View file

@ -1,5 +1,5 @@
import path from "node:path";
import { Elysia } from "elysia";
import { Elysia, t } from 'elysia'
import sanitize from "sanitize-filename";
import * as tar from "tar";
import { outputDir } from "..";
@ -11,16 +11,7 @@ export const download = new Elysia()
.use(userService)
.get(
"/download/:userId/:jobId/:fileName",
async ({ params, jwt, redirect, cookie: { auth } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
async ({ params, redirect, user }) => {
const job = await db
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
.get(user.id, params.jobId);
@ -28,7 +19,7 @@ export const download = new Elysia()
if (!job) {
return redirect(`${WEBROOT}/results`, 302);
}
// parse from url encoded string
// parse from URL encoded string
const userId = decodeURIComponent(params.userId);
const jobId = decodeURIComponent(params.jobId);
const fileName = sanitize(decodeURIComponent(params.fileName));
@ -36,17 +27,11 @@ export const download = new Elysia()
const filePath = `${outputDir}${userId}/${jobId}/${fileName}`;
return Bun.file(filePath);
},
{
auth: true,
}
)
.get("/archive/:userId/:jobId", async ({ params, jwt, redirect, cookie: { auth } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
.get("/archive/:userId/:jobId", async ({ params, redirect, user }) => {
const job = await db
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
.get(user.id, params.jobId);
@ -71,4 +56,6 @@ export const download = new Elysia()
["."],
);
return Bun.file(outputTar);
}, {
auth: true,
});