Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Set maximum expiration of 10 minutes for video processing key
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent a1d6596 commit 074dd1f
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions functions/api/reports/submit.ts
Expand Up @@ -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")
Expand All @@ -35,7 +29,7 @@ export async function onRequestPost(context: RequestContext) {
"content-type": "application/json",
},
method: "POST",
}
},
);

const { success }: { success: boolean } = await turnstileAPIResponse.json();
Expand Down Expand Up @@ -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) {
Expand All @@ -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 }[] } =
Expand All @@ -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,
);
}

Expand Down Expand Up @@ -169,32 +163,37 @@ 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;

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"))
Expand All @@ -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");
}
Expand All @@ -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();
Expand All @@ -247,6 +248,6 @@ export async function onRequestPost(context: RequestContext) {
headers: {
"content-type": "application/json",
},
}
},
);
}

0 comments on commit 074dd1f

Please sign in to comment.