import { Elysia } from "elysia"; import { Html } from "@elysiajs/html"; import { FIRST_RUN, userService } from "./user"; import { ACCOUNT_REGISTRATION, ALLOW_UNAUTHENTICATED, HIDE_HISTORY, HTTP_ALLOWED, WEBROOT } from "../helpers/env"; import { JWTPayloadSpec } from "@elysiajs/jwt"; import { randomInt } from "node:crypto"; import { BaseHtml } from "../components/base"; import { Header } from "../components/header"; import { getAllTargets } from "../converters/main"; import db from "../db/db"; import { User } from "../db/types"; export const root = new Elysia() .use(userService) .get("/", async ({ jwt, redirect, cookie: { auth, jobId } }) => { if (!ALLOW_UNAUTHENTICATED) { if (FIRST_RUN) { return redirect(`${WEBROOT}/setup`, 302); } if (!auth?.value) { return redirect(`${WEBROOT}/login`, 302); } } // validate jwt let user: ({ id: string } & JWTPayloadSpec) | false = false; if (ALLOW_UNAUTHENTICATED) { const newUserId = String( randomInt( 2 ** 24, Math.min(2 ** 48 + 2 ** 24 - 1, Number.MAX_SAFE_INTEGER), ), ); const accessToken = await jwt.sign({ id: newUserId, }); user = { id: newUserId }; if (!auth) { return { message: "No auth cookie, perhaps your browser is blocking cookies.", }; } // set cookie auth.set({ value: accessToken, httpOnly: true, secure: !HTTP_ALLOWED, maxAge: 24 * 60 * 60, sameSite: "strict", }); } else if (auth?.value) { user = await jwt.verify(auth.value); if (user !== false && user.id) { if (Number.parseInt(user.id) < 2 ** 24 || !ALLOW_UNAUTHENTICATED) { // make sure user exists in db const existingUser = db .query("SELECT * FROM users WHERE id = ?") .as(User) .get(user.id); if (!existingUser) { if (auth?.value) { auth.remove(); } return redirect(`${WEBROOT}/login`, 302); } } } } if (!user) { return redirect(`${WEBROOT}/login`, 302); } // create a new job db.query("INSERT INTO jobs (user_id, date_created) VALUES (?, ?)").run( user.id, new Date().toISOString(), ); const id = ( db .query("SELECT id FROM jobs WHERE user_id = ? ORDER BY id DESC") .get(user.id) as { id: number } ).id; if (!jobId) { return { message: "Cookies should be enabled to use this app." }; } jobId.set({ value: id, httpOnly: true, secure: !HTTP_ALLOWED, maxAge: 24 * 60 * 60, sameSite: "strict", }); console.log("jobId set to:", id); return ( <>

Convert

Choose a file or drag it here
{/* Hidden element which determines the format to convert the file too and the converter to use */}