Use direct uploads for infraction (files are processed serverside)
This commit is contained in:
parent
6f2bb0ebd1
commit
fe024c37da
@ -1,4 +1,4 @@
|
|||||||
import { GenerateUploadURL } from "../../gcloud.js";
|
import { GetAccessToken } from "../../gcloud.js";
|
||||||
import { jsonError } from "../../common.js";
|
import { jsonError } from "../../common.js";
|
||||||
|
|
||||||
const allowedFileTypes = [
|
const allowedFileTypes = [
|
||||||
@ -69,11 +69,13 @@ export async function onRequestPost(context: RequestContext) {
|
|||||||
files.push(file);
|
files.push(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
const urlPromises = [];
|
const uploadPromises = [];
|
||||||
const origin = context.request.headers.get("Origin");
|
const origin = context.request.headers.get("Origin");
|
||||||
|
|
||||||
if (!origin) return jsonError("Origin header missing", 400);
|
if (!origin) return jsonError("Origin header missing", 400);
|
||||||
|
|
||||||
|
const accessToken = await GetAccessToken(context.env);
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if (!allowedFileTypes.includes(file.type))
|
if (!allowedFileTypes.includes(file.type))
|
||||||
return jsonError(`File ${file.name} is not valid`, 415);
|
return jsonError(`File ${file.name} is not valid`, 415);
|
||||||
@ -82,35 +84,26 @@ export async function onRequestPost(context: RequestContext) {
|
|||||||
Math.random() * 10000000,
|
Math.random() * 10000000,
|
||||||
).toString()}/${file.name}`;
|
).toString()}/${file.name}`;
|
||||||
|
|
||||||
urlPromises.push(
|
uploadPromises.push(
|
||||||
GenerateUploadURL(
|
fetch(
|
||||||
context.env,
|
`https://storage.googleapis.com/upload/storage/v1/b/portal-carcrushers-cc/o?name=${encodeURIComponent(
|
||||||
attachmentKey,
|
attachmentKey,
|
||||||
file.size,
|
)}&uploadType=media`,
|
||||||
(allowedFileTypes.find((t) => t === file.type) as string).split("/")[1],
|
{
|
||||||
origin,
|
headers: {
|
||||||
|
authorization: `Bearer ${accessToken}`,
|
||||||
|
"content-type": file.type,
|
||||||
|
},
|
||||||
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const settledURLPromises = await Promise.allSettled(urlPromises);
|
await Promise.allSettled(uploadPromises);
|
||||||
|
|
||||||
if (settledURLPromises.find((p) => p.status === "rejected"))
|
|
||||||
return jsonError("Failed to process one or more files", 500);
|
|
||||||
|
|
||||||
const infractionId = `${body.get(
|
const infractionId = `${body.get(
|
||||||
"user",
|
"user",
|
||||||
)}${Date.now()}${context.request.headers.get("cf-ray")?.split("-")[0]}`;
|
)}${Date.now()}${context.request.headers.get("cf-ray")?.split("-")[0]}`;
|
||||||
const uploadReqs = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < files.length; i++) {
|
|
||||||
uploadReqs.push(
|
|
||||||
fetch(settledURLPromises[i] as unknown as string, {
|
|
||||||
body: files[i],
|
|
||||||
method: "PUT",
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await context.env.DATA.put(
|
await context.env.DATA.put(
|
||||||
infractionId,
|
infractionId,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user