Handle new report submission process

This commit is contained in:
2023-10-19 16:50:11 -04:00
parent a132fd667f
commit a3a5308d81
2 changed files with 188 additions and 84 deletions

View File

@ -10,8 +10,16 @@ function errorResponse(error: string, status: number): Response {
}
export async function onRequestPost(context: RequestContext) {
const { actions, bypass, filename, filesize, turnstileResponse, usernames } =
context.data.body;
const {
actions,
bypass,
description,
filename,
files,
filesize,
turnstileResponse,
usernames,
} = context.data.body;
if (!context.data.current_user) {
if (typeof turnstileResponse !== "string")
@ -21,6 +29,7 @@ export async function onRequestPost(context: RequestContext) {
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
{
body: JSON.stringify({
remoteip: context.request.headers.get("CF-Connecting-IP"),
response: turnstileResponse,
secret: context.env.TURNSTILE_SECRETKEY,
}),
@ -45,12 +54,42 @@ export async function onRequestPost(context: RequestContext) {
if (!Array.isArray(usernames))
return errorResponse("Usernames must be type of array", 400);
if (
!["string", "undefined"].includes(typeof description) ||
description?.length > 512
)
return errorResponse("Invalid description", 400);
if (
!Array.isArray(files) ||
files.find((file) => {
const keys = Object.keys(file);
return !keys.includes("name") || !keys.includes("size");
})
)
return errorResponse("File list missing name(s) and/or size(s)", 400);
if (typeof filename !== "string")
return errorResponse("Invalid file name", 400);
if (typeof filesize !== "number" || filesize < 0 || filesize > 536870912)
return errorResponse("Invalid file size", 400);
if (
files.find(
(file) =>
typeof file.name !== "string" ||
typeof file.size !== "number" ||
file.size < 0 ||
file.size > 536870912
)
)
return errorResponse(
"One or more files contain an invalid name or size",
400
);
if (!usernames.length || usernames.length > 20)
return errorResponse(
"Number of usernames provided must be between 1 and 20",
@ -111,45 +150,53 @@ export async function onRequestPost(context: RequestContext) {
metaNames.push(data.name);
}
const fileParts = filename.split(".");
let fileExt = fileParts[fileParts.length - 1];
const uploadUrlPromises: Promise<string>[] = [];
if (
fileParts.length < 2 ||
![
"mkv",
"mp4",
"wmv",
"jpg",
"png",
"m4v",
"jpeg",
"jfif",
"gif",
"webm",
"heif",
"heic",
"webp",
"mov",
].includes(fileExt.toLowerCase())
)
return errorResponse("This type of file cannot be uploaded", 415);
for (const file of files) {
const filePartes = file.name.split(".");
let fileExten = filePartes.at(-1);
const fileKey = `${crypto.randomUUID().replaceAll("-", "")}/${crypto
.randomUUID()
.replaceAll("-", "")}${context.request.headers.get("cf-ray")}${Date.now()}`;
if (
filePartes.length < 2 ||
![
"mkv",
"mp4",
"wmv",
"jpg",
"png",
"m4v",
"jpeg",
"jfif",
"gif",
"webm",
"heif",
"heic",
"webp",
"mov",
].includes(fileExten.toLowerCase())
)
return errorResponse(
`File ${file.name} cannot be uploaded as it is unsupported`,
415
);
const fileUploadKey = `${crypto.randomUUID().replaceAll("-", "")}/${crypto
.randomUUID()
.replaceAll("-", "")}${context.request.headers.get(
"cf-ray"
)}${Date.now()}`;
uploadUrlPromises.push(
GenerateUploadURL(context.env, `t/${fileUploadKey}`, file.size, fileExten)
);
}
const uploadUrls = await Promise.allSettled(uploadUrlPromises);
const reportId = `${Date.now()}${context.request.headers.get(
"cf-ray"
)}${crypto.randomUUID().replaceAll("-", "")}`;
const uploadUrl = await GenerateUploadURL(
context.env,
fileKey,
filesize,
fileExt
);
const { current_user: currentUser } = context.data;
await context.env.DATA.put(
@ -158,22 +205,32 @@ export async function onRequestPost(context: RequestContext) {
{ expirationTtl: 3600 }
);
if (["mkv", "mov", "wmv"].includes(fileExt.toLowerCase()))
await context.env.DATA.put(`videoprocessing_${fileKey}.${fileExt}`, "1", {
expirationTtl: 3600,
});
if (uploadUrls.find((uploadUrl) => uploadUrl.status === "rejected"))
return errorResponse("Failed to generate upload url", 500);
const attachments: string[] = [];
for (const urlResult of uploadUrls) {
let url = urlResult.toString().replace("t/", "");
const extension = (url.split(".").at(-1) as string).toLowerCase();
if (["mkv", "mov", "wmv"].includes(extension)) {
await context.env.DATA.put(`videoprocessing_${url}.${extension}`, "1");
url = url.replace(`.${extension}`, ".mp4");
}
attachments.push(url);
}
await context.env.DATA.put(
`report_${reportId}`,
JSON.stringify({
attachment: `${fileKey}.${
["mkv", "mov", "wmv"].includes(fileExt.toLowerCase()) ? "mp4" : fileExt
}`,
attachments,
id: reportId,
open: !bypass,
user: currentUser
? {
discriminator: currentUser.discriminator,
email: currentUser.email,
id: currentUser.id,
username: currentUser.username,
@ -192,9 +249,12 @@ export async function onRequestPost(context: RequestContext) {
.run();
} catch {}
return new Response(JSON.stringify({ id: reportId, upload_url: uploadUrl }), {
headers: {
"content-type": "application/json",
},
});
return new Response(
JSON.stringify({ id: reportId, upload_urls: uploadUrls }),
{
headers: {
"content-type": "application/json",
},
}
);
}