Set maximum expiration of 10 minutes for video processing key

This commit is contained in:
regalijan 2023-10-19 16:50:14 -04:00
parent a1d6596668
commit 074dd1fce5
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520

View File

@ -10,14 +10,8 @@ function errorResponse(error: string, status: number): Response {
} }
export async function onRequestPost(context: RequestContext) { export async function onRequestPost(context: RequestContext) {
const { const { actions, bypass, description, files, turnstileResponse, usernames } =
actions, context.data.body;
bypass,
description,
files,
turnstileResponse,
usernames,
} = context.data.body;
if (!context.data.current_user) { if (!context.data.current_user) {
if (typeof turnstileResponse !== "string") if (typeof turnstileResponse !== "string")
@ -35,7 +29,7 @@ export async function onRequestPost(context: RequestContext) {
"content-type": "application/json", "content-type": "application/json",
}, },
method: "POST", method: "POST",
} },
); );
const { success }: { success: boolean } = await turnstileAPIResponse.json(); const { success }: { success: boolean } = await turnstileAPIResponse.json();
@ -74,18 +68,18 @@ export async function onRequestPost(context: RequestContext) {
typeof file.name !== "string" || typeof file.name !== "string" ||
typeof file.size !== "number" || typeof file.size !== "number" ||
file.size < 0 || file.size < 0 ||
file.size > 536870912 file.size > 536870912,
) )
) )
return errorResponse( return errorResponse(
"One or more files contain an invalid name or size", "One or more files contain an invalid name or size",
400 400,
); );
if (!usernames.length || usernames.length > 20) if (!usernames.length || usernames.length > 20)
return errorResponse( return errorResponse(
"Number of usernames provided must be between 1 and 20", "Number of usernames provided must be between 1 and 20",
400 400,
); );
for (const username of usernames) { for (const username of usernames) {
@ -108,13 +102,13 @@ export async function onRequestPost(context: RequestContext) {
"content-type": "application/json", "content-type": "application/json",
}, },
method: "POST", method: "POST",
} },
); );
if (!rbxSearchReq.ok) if (!rbxSearchReq.ok)
return errorResponse( return errorResponse(
"Failed to locate Roblox users due to upstream error", "Failed to locate Roblox users due to upstream error",
500 500,
); );
const rbxSearchData: { data: { [k: string]: any }[] } = const rbxSearchData: { data: { [k: string]: any }[] } =
@ -130,7 +124,7 @@ export async function onRequestPost(context: RequestContext) {
return errorResponse( return errorResponse(
`The following users do not exist or are banned from Roblox: ${missingUsers.toString()}`, `The following users do not exist or are banned from Roblox: ${missingUsers.toString()}`,
400 400,
); );
} }
@ -169,24 +163,29 @@ export async function onRequestPost(context: RequestContext) {
) )
return errorResponse( return errorResponse(
`File ${file.name} cannot be uploaded as it is unsupported`, `File ${file.name} cannot be uploaded as it is unsupported`,
415 415,
); );
const fileUploadKey = `${crypto.randomUUID().replaceAll("-", "")}/${crypto const fileUploadKey = `${crypto.randomUUID().replaceAll("-", "")}/${crypto
.randomUUID() .randomUUID()
.replaceAll("-", "")}${context.request.headers.get( .replaceAll("-", "")}${context.request.headers.get(
"cf-ray" "cf-ray",
)}${Date.now()}`; )}${Date.now()}`;
uploadUrlPromises.push( uploadUrlPromises.push(
GenerateUploadURL(context.env, `t/${fileUploadKey}`, file.size, fileExten) GenerateUploadURL(
context.env,
`t/${fileUploadKey}`,
file.size,
fileExten,
),
); );
} }
const uploadUrls = await Promise.allSettled(uploadUrlPromises); const uploadUrls = await Promise.allSettled(uploadUrlPromises);
const reportId = `${Date.now()}${context.request.headers.get( const reportId = `${Date.now()}${context.request.headers.get(
"cf-ray" "cf-ray",
)}${crypto.randomUUID().replaceAll("-", "")}`; )}${crypto.randomUUID().replaceAll("-", "")}`;
const { current_user: currentUser } = context.data; const { current_user: currentUser } = context.data;
@ -194,7 +193,7 @@ export async function onRequestPost(context: RequestContext) {
await context.env.DATA.put( await context.env.DATA.put(
`reportprocessing_${reportId}`, `reportprocessing_${reportId}`,
currentUser?.id || context.request.headers.get("CF-Connecting-IP"), currentUser?.id || context.request.headers.get("CF-Connecting-IP"),
{ expirationTtl: 3600 } { expirationTtl: 3600 },
); );
if (uploadUrls.find((uploadUrl) => uploadUrl.status === "rejected")) if (uploadUrls.find((uploadUrl) => uploadUrl.status === "rejected"))
@ -207,7 +206,9 @@ export async function onRequestPost(context: RequestContext) {
const extension = (url.split(".").at(-1) as string).toLowerCase(); const extension = (url.split(".").at(-1) as string).toLowerCase();
if (["mkv", "mov", "wmv"].includes(extension)) { if (["mkv", "mov", "wmv"].includes(extension)) {
await context.env.DATA.put(`videoprocessing_${url}.${extension}`, "1"); await context.env.DATA.put(`videoprocessing_${url}.${extension}`, "1", {
expirationTtl: 600,
});
url = url.replace(`.${extension}`, ".mp4"); url = url.replace(`.${extension}`, ".mp4");
} }
@ -230,12 +231,12 @@ export async function onRequestPost(context: RequestContext) {
: null, : null,
target_ids: metaIDs, target_ids: metaIDs,
target_usernames: metaNames, target_usernames: metaNames,
}) }),
); );
try { try {
await context.env.D1.prepare( await context.env.D1.prepare(
"INSERT INTO reports (created_at, id, open, user) VALUES (?, ?, ?, ?);" "INSERT INTO reports (created_at, id, open, user) VALUES (?, ?, ?, ?);",
) )
.bind(Date.now(), reportId, Number(!bypass), currentUser?.id || null) .bind(Date.now(), reportId, Number(!bypass), currentUser?.id || null)
.run(); .run();
@ -247,6 +248,6 @@ export async function onRequestPost(context: RequestContext) {
headers: { headers: {
"content-type": "application/json", "content-type": "application/json",
}, },
} },
); );
} }