mirror of
https://github.com/C4illin/ConvertX.git
synced 2025-11-03 13:33:25 +00:00
chore: use auth macro instead of checking it on every path
This commit is contained in:
@@ -74,7 +74,7 @@ export const convert = new Elysia().use(userService).post(
|
|||||||
db.query("UPDATE jobs SET status = 'completed' WHERE id = ?1").run(jobId.value);
|
db.query("UPDATE jobs SET status = 'completed' WHERE id = ?1").run(jobId.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete all uploaded files in userUploadsDir
|
// Delete all uploaded files in userUploadsDir
|
||||||
// rmSync(userUploadsDir, { recursive: true, force: true });
|
// rmSync(userUploadsDir, { recursive: true, force: true });
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
@@ -89,5 +89,6 @@ export const convert = new Elysia().use(userService).post(
|
|||||||
convert_to: t.String(),
|
convert_to: t.String(),
|
||||||
file_names: t.String(),
|
file_names: t.String(),
|
||||||
}),
|
}),
|
||||||
|
auth: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -7,16 +7,7 @@ import { userService } from "./user";
|
|||||||
|
|
||||||
export const deleteFile = new Elysia().use(userService).post(
|
export const deleteFile = new Elysia().use(userService).post(
|
||||||
"/delete",
|
"/delete",
|
||||||
async ({ body, redirect, jwt, cookie: { auth, jobId } }) => {
|
async ({ body, redirect, cookie: { jobId }, user }) => {
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
if (!user) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!jobId?.value) {
|
if (!jobId?.value) {
|
||||||
return redirect(`${WEBROOT}/`, 302);
|
return redirect(`${WEBROOT}/`, 302);
|
||||||
}
|
}
|
||||||
@@ -37,5 +28,5 @@ export const deleteFile = new Elysia().use(userService).post(
|
|||||||
message: "File deleted successfully.",
|
message: "File deleted successfully.",
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
{ body: t.Object({ filename: t.String() }) },
|
{ body: t.Object({ filename: t.String() }), auth: true },
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { Elysia } from "elysia";
|
import { Elysia, t } from 'elysia'
|
||||||
import sanitize from "sanitize-filename";
|
import sanitize from "sanitize-filename";
|
||||||
import * as tar from "tar";
|
import * as tar from "tar";
|
||||||
import { outputDir } from "..";
|
import { outputDir } from "..";
|
||||||
@@ -11,16 +11,7 @@ export const download = new Elysia()
|
|||||||
.use(userService)
|
.use(userService)
|
||||||
.get(
|
.get(
|
||||||
"/download/:userId/:jobId/:fileName",
|
"/download/:userId/:jobId/:fileName",
|
||||||
async ({ params, jwt, redirect, cookie: { auth } }) => {
|
async ({ params, redirect, user }) => {
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
if (!user) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const job = await db
|
const job = await db
|
||||||
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
||||||
.get(user.id, params.jobId);
|
.get(user.id, params.jobId);
|
||||||
@@ -28,7 +19,7 @@ export const download = new Elysia()
|
|||||||
if (!job) {
|
if (!job) {
|
||||||
return redirect(`${WEBROOT}/results`, 302);
|
return redirect(`${WEBROOT}/results`, 302);
|
||||||
}
|
}
|
||||||
// parse from url encoded string
|
// parse from URL encoded string
|
||||||
const userId = decodeURIComponent(params.userId);
|
const userId = decodeURIComponent(params.userId);
|
||||||
const jobId = decodeURIComponent(params.jobId);
|
const jobId = decodeURIComponent(params.jobId);
|
||||||
const fileName = sanitize(decodeURIComponent(params.fileName));
|
const fileName = sanitize(decodeURIComponent(params.fileName));
|
||||||
@@ -36,17 +27,11 @@ export const download = new Elysia()
|
|||||||
const filePath = `${outputDir}${userId}/${jobId}/${fileName}`;
|
const filePath = `${outputDir}${userId}/${jobId}/${fileName}`;
|
||||||
return Bun.file(filePath);
|
return Bun.file(filePath);
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
auth: true,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
.get("/archive/:userId/:jobId", async ({ params, jwt, redirect, cookie: { auth } }) => {
|
.get("/archive/:userId/:jobId", async ({ params, redirect, user }) => {
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
if (!user) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const job = await db
|
const job = await db
|
||||||
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
||||||
.get(user.id, params.jobId);
|
.get(user.id, params.jobId);
|
||||||
@@ -71,4 +56,6 @@ export const download = new Elysia()
|
|||||||
["."],
|
["."],
|
||||||
);
|
);
|
||||||
return Bun.file(outputTar);
|
return Bun.file(outputTar);
|
||||||
|
}, {
|
||||||
|
auth: true,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,16 +9,11 @@ import { userService } from "./user";
|
|||||||
|
|
||||||
export const history = new Elysia()
|
export const history = new Elysia()
|
||||||
.use(userService)
|
.use(userService)
|
||||||
.get("/history", async ({ jwt, redirect, cookie: { auth } }) => {
|
.get("/history", async ({ jwt, redirect, user }) => {
|
||||||
if (HIDE_HISTORY) {
|
if (HIDE_HISTORY) {
|
||||||
return redirect(`${WEBROOT}/`, 302);
|
return redirect(`${WEBROOT}/`, 302);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
return redirect(`${WEBROOT}/login`, 302);
|
||||||
}
|
}
|
||||||
@@ -32,7 +27,7 @@ export const history = new Elysia()
|
|||||||
job.files_detailed = files;
|
job.files_detailed = files;
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter out jobs with no files
|
// Filter out jobs with no files
|
||||||
userJobs = userJobs.filter((job) => job.num_files > 0);
|
userJobs = userJobs.filter((job) => job.num_files > 0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -213,4 +208,6 @@ export const history = new Elysia()
|
|||||||
</>
|
</>
|
||||||
</BaseHtml>
|
</BaseHtml>
|
||||||
);
|
);
|
||||||
|
}, {
|
||||||
|
auth: true
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,16 +8,7 @@ import { userService } from "./user";
|
|||||||
|
|
||||||
export const listConverters = new Elysia()
|
export const listConverters = new Elysia()
|
||||||
.use(userService)
|
.use(userService)
|
||||||
.get("/converters", async ({ jwt, redirect, cookie: { auth } }) => {
|
.get("/converters", async () => {
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
if (!user) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BaseHtml webroot={WEBROOT} title="ConvertX | Converters">
|
<BaseHtml webroot={WEBROOT} title="ConvertX | Converters">
|
||||||
<>
|
<>
|
||||||
@@ -77,4 +68,6 @@ export const listConverters = new Elysia()
|
|||||||
</>
|
</>
|
||||||
</BaseHtml>
|
</BaseHtml>
|
||||||
);
|
);
|
||||||
|
}, {
|
||||||
|
auth: true
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -136,21 +136,12 @@ function ResultsArticle({
|
|||||||
|
|
||||||
export const results = new Elysia()
|
export const results = new Elysia()
|
||||||
.use(userService)
|
.use(userService)
|
||||||
.get("/results/:jobId", async ({ params, jwt, set, redirect, cookie: { auth, job_id } }) => {
|
.get("/results/:jobId", async ({ params, jwt, set, redirect, cookie: { job_id }, user }) => {
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (job_id?.value) {
|
if (job_id?.value) {
|
||||||
// clear the job_id cookie since we are viewing the results
|
// Clear the job_id cookie since we are viewing the results
|
||||||
job_id.remove();
|
job_id.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
if (!user) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const job = db
|
const job = db
|
||||||
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
||||||
.as(Jobs)
|
.as(Jobs)
|
||||||
@@ -186,22 +177,13 @@ export const results = new Elysia()
|
|||||||
</>
|
</>
|
||||||
</BaseHtml>
|
</BaseHtml>
|
||||||
);
|
);
|
||||||
})
|
}, { auth: true })
|
||||||
.post("/progress/:jobId", async ({ jwt, set, params, redirect, cookie: { auth, job_id } }) => {
|
.post("/progress/:jobId", async ({ jwt, set, params, cookie: { job_id }, user }) => {
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (job_id?.value) {
|
if (job_id?.value) {
|
||||||
// clear the job_id cookie since we are viewing the results
|
// Clear the job_id cookie since we are viewing the results
|
||||||
job_id.remove();
|
job_id.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
if (!user) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const job = db
|
const job = db
|
||||||
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
|
||||||
.as(Jobs)
|
.as(Jobs)
|
||||||
@@ -222,4 +204,4 @@ export const results = new Elysia()
|
|||||||
.all(params.jobId);
|
.all(params.jobId);
|
||||||
|
|
||||||
return <ResultsArticle user={user} job={job} files={files} outputPath={outputPath} />;
|
return <ResultsArticle user={user} job={job} files={files} outputPath={outputPath} />;
|
||||||
});
|
}, { auth: true });
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { randomInt } from "node:crypto";
|
import { randomInt } from "node:crypto";
|
||||||
import { Html } from "@elysiajs/html";
|
import { Html } from "@elysiajs/html";
|
||||||
import { JWTPayloadSpec } from "@elysiajs/jwt";
|
import { JWTPayloadSpec } from "@elysiajs/jwt";
|
||||||
import { Elysia } from "elysia";
|
import { Elysia, t } from "elysia";
|
||||||
import { BaseHtml } from "../components/base";
|
import { BaseHtml } from "../components/base";
|
||||||
import { Header } from "../components/header";
|
import { Header } from "../components/header";
|
||||||
import { getAllTargets } from "../converters/main";
|
import { getAllTargets } from "../converters/main";
|
||||||
@@ -65,7 +65,7 @@ export const root = new Elysia()
|
|||||||
user.id &&
|
user.id &&
|
||||||
(Number.parseInt(user.id) < 2 ** 24 || !ALLOW_UNAUTHENTICATED)
|
(Number.parseInt(user.id) < 2 ** 24 || !ALLOW_UNAUTHENTICATED)
|
||||||
) {
|
) {
|
||||||
// make sure user exists in db
|
// Make sure user exists in db
|
||||||
const existingUser = db.query("SELECT * FROM users WHERE id = ?").as(User).get(user.id);
|
const existingUser = db.query("SELECT * FROM users WHERE id = ?").as(User).get(user.id);
|
||||||
|
|
||||||
if (!existingUser) {
|
if (!existingUser) {
|
||||||
@@ -240,4 +240,9 @@ export const root = new Elysia()
|
|||||||
</>
|
</>
|
||||||
</BaseHtml>
|
</BaseHtml>
|
||||||
);
|
);
|
||||||
|
}, {
|
||||||
|
cookie: t.Cookie({
|
||||||
|
auth: t.Optional(t.String()),
|
||||||
|
jobId: t.Optional(t.String()),
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,16 +6,7 @@ import { userService } from "./user";
|
|||||||
|
|
||||||
export const upload = new Elysia().use(userService).post(
|
export const upload = new Elysia().use(userService).post(
|
||||||
"/upload",
|
"/upload",
|
||||||
async ({ body, redirect, jwt, cookie: { auth, jobId } }) => {
|
async ({ body, redirect, user, cookie: { jobId } }) => {
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
if (!user) {
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!jobId?.value) {
|
if (!jobId?.value) {
|
||||||
return redirect(`${WEBROOT}/`, 302);
|
return redirect(`${WEBROOT}/`, 302);
|
||||||
}
|
}
|
||||||
@@ -44,5 +35,5 @@ export const upload = new Elysia().use(userService).post(
|
|||||||
message: "Files uploaded successfully.",
|
message: "Files uploaded successfully.",
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
{ body: t.Object({ file: t.Files() }) },
|
{ body: t.Object({ file: t.Files() }), auth: true },
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -32,28 +32,37 @@ export const userService = new Elysia({ name: "user/service" })
|
|||||||
email: t.String(),
|
email: t.String(),
|
||||||
password: t.String(),
|
password: t.String(),
|
||||||
}),
|
}),
|
||||||
|
session: t.Cookie({
|
||||||
|
auth: t.String(),
|
||||||
|
jobId: t.Optional(t.String()),
|
||||||
|
}),
|
||||||
|
optionalSession: t.Cookie({
|
||||||
|
auth: t.Optional(t.String()),
|
||||||
|
jobId: t.Optional(t.String()),
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.macro({
|
.macro("auth", {
|
||||||
isSignIn(enabled: boolean) {
|
cookie: "session", async resolve({
|
||||||
if (!enabled) return;
|
status, jwt, cookie: { auth }
|
||||||
|
}) {
|
||||||
|
if (!auth.value) {
|
||||||
|
return status(401, {
|
||||||
|
success: false,
|
||||||
|
message: 'Unauthorized'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
const user = await jwt.verify(auth.value);
|
||||||
|
if (!user) {
|
||||||
|
return status(401, {
|
||||||
|
success: false,
|
||||||
|
message: 'Unauthorized'
|
||||||
|
})
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
async beforeHandle({ status, jwt, cookie: { auth } }) {
|
success: true,
|
||||||
if (auth?.value) {
|
user
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
return {
|
|
||||||
success: true,
|
|
||||||
user,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return status(401, {
|
|
||||||
success: false,
|
|
||||||
message: "Unauthorized",
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const user = new Elysia()
|
export const user = new Elysia()
|
||||||
@@ -303,7 +312,8 @@ export const user = new Elysia()
|
|||||||
</>
|
</>
|
||||||
</BaseHtml>
|
</BaseHtml>
|
||||||
);
|
);
|
||||||
})
|
}, { body: "signIn", cookie: "optionalSession" }
|
||||||
|
)
|
||||||
.post(
|
.post(
|
||||||
"/login",
|
"/login",
|
||||||
async function handler({ body, set, redirect, jwt, cookie: { auth } }) {
|
async function handler({ body, set, redirect, jwt, cookie: { auth } }) {
|
||||||
@@ -363,11 +373,7 @@ export const user = new Elysia()
|
|||||||
|
|
||||||
return redirect(`${WEBROOT}/login`, 302);
|
return redirect(`${WEBROOT}/login`, 302);
|
||||||
})
|
})
|
||||||
.get("/account", async ({ jwt, redirect, cookie: { auth } }) => {
|
.get("/account", async ({ user, redirect }) => {
|
||||||
if (!auth?.value) {
|
|
||||||
return redirect(`${WEBROOT}/`);
|
|
||||||
}
|
|
||||||
const user = await jwt.verify(auth.value);
|
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return redirect(`${WEBROOT}/`, 302);
|
return redirect(`${WEBROOT}/`, 302);
|
||||||
@@ -441,6 +447,8 @@ export const user = new Elysia()
|
|||||||
</>
|
</>
|
||||||
</BaseHtml>
|
</BaseHtml>
|
||||||
);
|
);
|
||||||
|
}, {
|
||||||
|
auth: true
|
||||||
})
|
})
|
||||||
.post(
|
.post(
|
||||||
"/account",
|
"/account",
|
||||||
@@ -505,5 +513,6 @@ export const user = new Elysia()
|
|||||||
newPassword: t.MaybeEmpty(t.String()),
|
newPassword: t.MaybeEmpty(t.String()),
|
||||||
password: t.String(),
|
password: t.String(),
|
||||||
}),
|
}),
|
||||||
|
cookie: "session"
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user