start on pandoc

This commit is contained in:
C4illin 2024-05-19 23:51:27 +02:00
parent 391ef063f7
commit 13cc37d5a2
18 changed files with 782 additions and 408 deletions

View file

@ -8,14 +8,19 @@ import { Database } from "bun:sqlite";
import { Elysia, t } from "elysia";
import { BaseHtml } from "./components/base";
import { Header } from "./components/header";
import { mainConverter, possibleConversions } from "./converters/main";
import {
mainConverter,
getPossibleConversions,
getAllTargets,
} from "./converters/main";
import { normalizeFiletype } from "./helpers/normalizeFiletype";
const db = new Database("./data/mydb.sqlite", { create: true });
const uploadsDir = "./data/uploads/";
const outputDir = "./data/output/";
const jobs = {};
const accountRegistration =
process.env.ACCOUNT_REGISTRATION === "true" || false;
// fileNames: fileNames,
// filesToConvert: fileNames.length,
@ -31,19 +36,42 @@ CREATE TABLE IF NOT EXISTS users (
);
CREATE TABLE IF NOT EXISTS file_names (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job_id TEXT NOT NULL,
job_id INTEGER NOT NULL,
file_name TEXT NOT NULL,
output_file_name TEXT NOT NULL
output_file_name TEXT NOT NULL,
FOREIGN KEY (job_id) REFERENCES jobs(id)
);
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,
status TEXT DEFAULT 'pending',
converted_files INTEGER DEFAULT 0
status TEXT DEFAULT 'not started',
num_files INTEGER DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users(id)
);`);
interface IUser {
id: number;
email: string;
password: string;
}
interface IFileNames {
id: number;
job_id: number;
file_name: string;
output_file_name: string;
}
interface IJobs {
finished_files: number;
id: number;
user_id: number;
date_created: string;
status: string;
num_files: number;
}
// enable WAL mode
db.exec("PRAGMA journal_mode = WAL;");
@ -70,17 +98,32 @@ const app = new Elysia()
return (
<BaseHtml title="ConvertX | Register">
<Header />
<main class="container-fluid">
<form method="post">
<input type="email" name="email" placeholder="Email" required />
<input
type="password"
name="password"
placeholder="Password"
required
/>
<input type="submit" value="Register" />
</form>
<main class="container">
<article>
<form method="post">
<fieldset>
<label>
Email
<input
type="email"
name="email"
placeholder="Email"
required
/>
</label>
<label>
Password
<input
type="password"
name="password"
placeholder="Password"
required
/>
</label>
</fieldset>
<input type="submit" value="Register" />
</form>
</article>
</main>
</BaseHtml>
);
@ -99,20 +142,26 @@ const app = new Elysia()
}
const savedPassword = await Bun.password.hash(body.password);
db.run(
"INSERT INTO users (email, password) VALUES (?, ?)",
db.query("INSERT INTO users (email, password) VALUES (?, ?)").run(
body.email,
savedPassword,
);
const user = await db
const user = (await db
.query("SELECT * FROM users WHERE email = ?")
.get(body.email);
.get(body.email)) as IUser;
const accessToken = await jwt.sign({
id: String(user.id),
});
if (!auth) {
set.status = 500;
return {
message: "No auth cookie, perhaps your browser is blocking cookies.",
};
}
// set cookie
auth.set({
value: accessToken,
@ -133,70 +182,103 @@ const app = new Elysia()
return (
<BaseHtml title="ConvertX | Login">
<Header />
<main class="container-fluid">
<form method="post">
<input type="email" name="email" placeholder="Email" required />
<input
type="password"
name="password"
placeholder="Password"
required
/>
<div role="group">
<a href="/register" role="button" class="secondary">
Register an account
</a>
<input type="submit" value="Login" />
</div>
</form>
<main class="container">
<article>
<form method="post">
<fieldset>
<label>
Email
<input
type="email"
name="email"
placeholder="Email"
required
/>
</label>
<label>
Password
<input
type="password"
name="password"
placeholder="Password"
required
/>
</label>
</fieldset>
<div role="group">
<a href="/register" role="button" class="secondary">
Register an account
</a>
<input type="submit" value="Login" />
</div>
</form>
</article>
</main>
</BaseHtml>
);
})
.post("/login", async function handler({ body, set, jwt, cookie: { auth } }) {
const existingUser = await db
.query("SELECT * FROM users WHERE email = ?")
.get(body.email);
.post(
"/login",
async function handler({ body, set, redirect, jwt, cookie: { auth } }) {
// if already logged in, redirect to home
if (auth?.value) {
const user = await jwt.verify(auth.value);
if (user) {
return redirect("/");
}
auth.remove();
}
if (!existingUser) {
set.status = 403;
return {
message: "Invalid credentials.",
const existingUser = (await db
.query("SELECT * FROM users WHERE email = ?")
.get(body.email)) as IUser;
if (!existingUser) {
set.status = 403;
return {
message: "Invalid credentials.",
};
}
const validPassword = await Bun.password.verify(
body.password,
existingUser.password,
);
if (!validPassword) {
set.status = 403;
return {
message: "Invalid credentials.",
};
}
const accessToken = await jwt.sign({
id: String(existingUser.id),
});
if (!auth) {
set.status = 500;
return {
message: "No auth cookie, perhaps your browser is blocking cookies.",
};
}
// set cookie
auth.set({
value: accessToken,
httpOnly: true,
secure: true,
maxAge: 60 * 60 * 24 * 7,
sameSite: "strict",
});
// redirect to home
set.status = 302;
set.headers = {
Location: "/",
};
}
const validPassword = await Bun.password.verify(
body.password,
existingUser.password,
);
if (!validPassword) {
set.status = 403;
return {
message: "Invalid credentials.",
};
}
const accessToken = await jwt.sign({
id: String(existingUser.id),
});
// set cookie
// set cookie
auth.set({
value: accessToken,
httpOnly: true,
secure: true,
maxAge: 60 * 60 * 24 * 7,
sameSite: "strict",
});
// redirect to home
set.status = 302;
set.headers = {
Location: "/",
};
})
},
)
.get("/logout", ({ redirect, cookie: { auth } }) => {
if (auth?.value) {
auth.remove();
@ -211,6 +293,9 @@ const app = new Elysia()
return redirect("/login");
})
.get("/", async ({ jwt, redirect, cookie: { auth, jobId } }) => {
if (!auth?.value) {
return redirect("/login");
}
// validate jwt
const user = await jwt.verify(auth.value);
if (!user) {
@ -218,9 +303,9 @@ const app = new Elysia()
}
// make sure user exists in db
const existingUser = await db
const existingUser = (await db
.query("SELECT * FROM users WHERE id = ?")
.get(user.id);
.get(user.id)) as IUser;
if (!existingUser) {
if (auth?.value) {
@ -229,28 +314,36 @@ const app = new Elysia()
return redirect("/login");
}
// create a unique job id
// 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: randomUUID(),
value: id,
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 (
<BaseHtml>
<Header loggedIn />
<main class="container-fluid">
<main class="container">
<article>
<h1>Convert</h1>
<table id="file-list" />
<input type="file" name="file" multiple />
</article>
@ -261,12 +354,10 @@ const app = new Elysia()
<option selected disabled value="">
Convert to
</option>
<option>JPG</option>
<option>PNG</option>
<option>SVG</option>
<option>PDF</option>
<option>DOCX</option>
<option>Yaml</option>
{getAllTargets().map((target) => (
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
<option value={target}>{target}</option>
))}
</select>
</article>
<input type="submit" value="Convert" />
@ -276,10 +367,22 @@ const app = new Elysia()
</BaseHtml>
);
})
.post("/conversions", ({ body }) => {
console.log(body);
return (
<select name="convert_to" aria-label="Convert to" required>
<option selected disabled value="">
Convert to
</option>
{getPossibleConversions(body.fileType).map((target) => (
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
<option value={target}>{target}</option>
))}
</select>
);
})
.post("/upload", async ({ body, redirect, jwt, cookie: { auth, jobId } }) => {
// validate jwt
if (!auth?.value) {
// redirect to login
return redirect("/login");
}
@ -288,7 +391,17 @@ const app = new Elysia()
return redirect("/login");
}
// let filesUploaded = [];
if (!jobId?.value) {
return redirect("/");
}
const existingJob = await db
.query("SELECT * FROM jobs WHERE id = ? AND user_id = ?")
.get(jobId.value, user.id);
if (!existingJob) {
return redirect("/");
}
const userUploadsDir = `${uploadsDir}${user.id}/${jobId.value}/`;
@ -307,15 +420,26 @@ const app = new Elysia()
message: "Files uploaded successfully.",
};
})
.post("/delete", async ({ body, set, jwt, cookie: { auth, jobId } }) => {
.post("/delete", async ({ body, redirect, jwt, cookie: { auth, jobId } }) => {
if (!auth?.value) {
return redirect("/login");
}
const user = await jwt.verify(auth.value);
if (!user) {
// redirect to login
set.status = 302;
set.headers = {
Location: "/login",
};
return;
return redirect("/login");
}
if (!jobId?.value) {
return redirect("/");
}
const existingJob = await db
.query("SELECT * FROM jobs WHERE id = ? AND user_id = ?")
.get(jobId.value, user.id);
if (!existingJob) {
return redirect("/");
}
const userUploadsDir = `${uploadsDir}${user.id}/${jobId.value}/`;
@ -324,21 +448,28 @@ const app = new Elysia()
})
.post(
"/convert",
async ({ body, set, redirect, jwt, cookie: { auth, jobId } }) => {
async ({ body, redirect, jwt, cookie: { auth, jobId } }) => {
if (!auth?.value) {
return redirect("/login");
}
const user = await jwt.verify(auth.value);
if (!user) {
// redirect to login
set.status = 302;
set.headers = {
Location: "/login",
};
return;
return redirect("/login");
}
if (!jobId?.value) {
return redirect("/");
}
const existingJob = (await db
.query("SELECT * FROM jobs WHERE id = ? AND user_id = ?")
.get(jobId.value, user.id)) as IJobs;
if (!existingJob) {
return redirect("/");
}
const userUploadsDir = `${uploadsDir}${user.id}/${jobId.value}/`;
const userOutputDir = `${outputDir}${user.id}/${jobId.value}/`;
@ -353,76 +484,196 @@ const app = new Elysia()
}
const convertTo = normalizeFiletype(body.convert_to);
const fileNames = JSON.parse(body.file_names);
const fileNames: string[] = JSON.parse(body.file_names) as string[];
jobs[jobId.value] = {
fileNames: fileNames,
filesToConvert: fileNames.length,
convertedFiles: 0,
outputFiles: [],
};
if (!Array.isArray(fileNames) || fileNames.length === 0) {
return redirect("/");
}
db.run(
"UPDATE jobs SET num_files = ? WHERE id = ?",
fileNames.length,
jobId.value,
);
const query = db.query(
"INSERT INTO file_names (job_id, file_name, output_file_name) VALUES (?, ?, ?)",
);
for (const fileName of fileNames) {
const filePath = `${userUploadsDir}${fileName}`;
const fileTypeOrig = fileName.split(".").pop();
const fileTypeOrig = fileName.split(".").pop() as string;
const fileType = normalizeFiletype(fileTypeOrig);
const newFileName = fileName.replace(fileTypeOrig, convertTo);
const targetPath = `${userOutputDir}${newFileName}`;
await mainConverter(filePath, fileType, convertTo, targetPath);
jobs[jobId.value].convertedFiles++;
jobs[jobId.value].outputFiles.push(newFileName);
query.run(jobId.value, fileName, newFileName);
}
console.log(
"sending to results page...",
`http://${app.server?.hostname}:${app.server?.port}/results/${jobId.value}`,
);
// redirect to results
set.status = 302;
set.headers = {
Location: `/results/${jobId.value}`,
};
return redirect(`/results/${jobId.value}`);
},
)
.get("/results", async ({ params, jwt, set, redirect, cookie: { auth } }) => {
.get("/histt", async ({ jwt, redirect, cookie: { auth } }) => {
console.log("results page");
if (!auth?.value) {
console.log("no auth value");
return redirect("/login");
}
const user = await jwt.verify(auth.value);
if (!user) {
console.log("no user");
return redirect("/login");
}
const userJobs = await db
const userJobs = db
.query("SELECT * FROM jobs WHERE user_id = ?")
.all(user.id);
.all(user.id) as {
id: number;
user_id: number;
date_created: string;
status: string;
num_files: number;
finished_files: number;
}[];
for (const job of userJobs) {
const files = db
.query("SELECT * FROM file_names WHERE job_id = ?")
.all(job.id) as IFileNames[];
job.finished_files = files.length;
}
return (
<BaseHtml title="ConvertX | Results">
<Header loggedIn />
<main class="container-fluid">
<article>
<h1>Results</h1>
<ul>
{userJobs.map((job) => (
<li>
<a href={`/results/${job.job_id}`}>{job.job_id}</a>
</li>
))}
</ul>
<table>
<thead>
<tr>
<th>Time</th>
<th>Files</th>
<th>Files Done</th>
<th>Status</th>
<th>View</th>
</tr>
</thead>
<tbody>
{userJobs.map((job) => (
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
<tr>
<td>{job.date_created}</td>
<td>{job.num_files}</td>
<td>{job.finished_files}</td>
<td>{job.status}</td>
<td>
<a href={`/results/${job.id}`}>View</a>
</td>
</tr>
))}
</tbody>
</table>
</article>
</main>
</BaseHtml>
);
// list all jobs belonging to the user
})
.get(
"/results/:jobId",
async ({ params, jwt, set, redirect, cookie: { auth } }) => {
async ({ params, jwt, set, redirect, cookie: { auth, job_id } }) => {
if (!auth?.value) {
return redirect("/login");
}
if (job_id?.value) {
// clear the job_id cookie since we are viewing the results
job_id.remove();
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect("/login");
}
const job = (await db
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
.get(user.id, params.jobId)) as IJobs;
if (!job) {
set.status = 404;
return {
message: "Job not found.",
};
}
const outputPath = `${user.id}/${params.jobId}/`;
const files = db
.query("SELECT * FROM file_names WHERE job_id = ?")
.all(params.jobId) as IFileNames[];
return (
<BaseHtml title="ConvertX | Result">
<Header loggedIn />
<main class="container-fluid">
<article>
<div class="grid">
<h1>Results</h1>
<div>
<button
type="button"
style={{ width: "10rem", float: "right" }}
>
Download All
</button>
</div>
</div>
<progress max={job.num_files} value={files.length} />
<table>
<thead>
<tr>
<th>Converted File Name</th>
<th>View</th>
<th>Download</th>
</tr>
</thead>
<tbody>
{files.map((file) => (
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
<tr>
<td>{file.output_file_name}</td>
<td>
<a
href={`/download/${outputPath}${file.output_file_name}`}
>
View
</a>
</td>
<td>
<a
href={`/download/${outputPath}${file.output_file_name}`}
download={file.output_file_name}
>
Download
</a>
</td>
</tr>
))}
</tbody>
</table>
</article>
</main>
</BaseHtml>
);
},
)
.get(
"/download/:userId/:jobId/:fileName",
async ({ params, jwt, redirect, cookie: { auth } }) => {
if (!auth?.value) {
return redirect("/login");
}
@ -433,35 +684,15 @@ const app = new Elysia()
}
const job = await db
.query("SELECT * FROM jobs WHERE user_id = ? AND job_id = ?")
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
.get(user.id, params.jobId);
if (!job) {
set.status = 404;
return {
message: "Job not found.",
};
return redirect("/results");
}
return (
<BaseHtml>
<Header loggedIn />
<main class="container-fluid">
<article>
<h1>Results</h1>
<ul>
{jobs[params.jobId].outputFiles.map((file: string) => (
<li>
<a href={`/output/${user.id}/${params.jobId}/${file}`}>
{file}
</a>
</li>
))}
</ul>
</article>
</main>
</BaseHtml>
);
const filePath = `${outputDir}${params.userId}/${params.jobId}/${params.fileName}`;
return Bun.file(filePath);
},
)
.onError(({ code, error, request }) => {