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) {
const {
actions,
bypass,
description,
files,
turnstileResponse,
usernames,
} = context.data.body;
const { actions, bypass, description, files, turnstileResponse, usernames } =
context.data.body;
if (!context.data.current_user) {
if (typeof turnstileResponse !== "string")
@ -35,7 +29,7 @@ export async function onRequestPost(context: RequestContext) {
"content-type": "application/json",
},
method: "POST",
}
},
);
const { success }: { success: boolean } = await turnstileAPIResponse.json();
@ -74,18 +68,18 @@ export async function onRequestPost(context: RequestContext) {
typeof file.name !== "string" ||
typeof file.size !== "number" ||
file.size < 0 ||
file.size > 536870912
file.size > 536870912,
)
)
return errorResponse(
"One or more files contain an invalid name or size",
400
400,
);
if (!usernames.length || usernames.length > 20)
return errorResponse(
"Number of usernames provided must be between 1 and 20",
400
400,
);
for (const username of usernames) {
@ -108,13 +102,13 @@ export async function onRequestPost(context: RequestContext) {
"content-type": "application/json",
},
method: "POST",
}
},
);
if (!rbxSearchReq.ok)
return errorResponse(
"Failed to locate Roblox users due to upstream error",
500
500,
);
const rbxSearchData: { data: { [k: string]: any }[] } =
@ -130,7 +124,7 @@ export async function onRequestPost(context: RequestContext) {
return errorResponse(
`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(
`File ${file.name} cannot be uploaded as it is unsupported`,
415
415,
);
const fileUploadKey = `${crypto.randomUUID().replaceAll("-", "")}/${crypto
.randomUUID()
.replaceAll("-", "")}${context.request.headers.get(
"cf-ray"
"cf-ray",
)}${Date.now()}`;
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 reportId = `${Date.now()}${context.request.headers.get(
"cf-ray"
"cf-ray",
)}${crypto.randomUUID().replaceAll("-", "")}`;
const { current_user: currentUser } = context.data;
@ -194,7 +193,7 @@ export async function onRequestPost(context: RequestContext) {
await context.env.DATA.put(
`reportprocessing_${reportId}`,
currentUser?.id || context.request.headers.get("CF-Connecting-IP"),
{ expirationTtl: 3600 }
{ expirationTtl: 3600 },
);
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();
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");
}
@ -230,12 +231,12 @@ export async function onRequestPost(context: RequestContext) {
: null,
target_ids: metaIDs,
target_usernames: metaNames,
})
}),
);
try {
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)
.run();
@ -247,6 +248,6 @@ export async function onRequestPost(context: RequestContext) {
headers: {
"content-type": "application/json",
},
}
},
);
}