136 lines
3.9 KiB
TypeScript
136 lines
3.9 KiB
TypeScript
import { jsonError } from "../../common.js";
|
|
|
|
export async function onRequestPost(context: RequestContext) {
|
|
const { id } = context.data.body;
|
|
|
|
if (!id) return jsonError("No ID provided", 400);
|
|
|
|
const user = await context.env.DATA.get(`reportprocessing_${id}`);
|
|
|
|
if (
|
|
!user ||
|
|
(context.data.current_user
|
|
? user !== context.data.current_user.id
|
|
: user !== context.request.headers.get("CF-Connecting-IP"))
|
|
)
|
|
return jsonError("No report with that ID is processing", 404);
|
|
|
|
await context.env.DATA.delete(`reportprocessing_${id}`);
|
|
|
|
const value = await context.env.DATA.get(`report_${id}`);
|
|
|
|
if (!value) return jsonError("Report is missing", 500);
|
|
|
|
const coconutData = (await context.env.DATA.get(`coconutdata_${id}`, {
|
|
type: "json",
|
|
})) as {
|
|
attachments: string[];
|
|
} | null;
|
|
|
|
if (coconutData) {
|
|
const responsePromises = [];
|
|
const contentTypes: { [k: string]: string } = {
|
|
gif: "image/gif",
|
|
m4v: "video/x-m4v",
|
|
mkv: "video/x-matroska",
|
|
mov: "video/mp4",
|
|
mp4: "video/mp4",
|
|
webm: "video/webm",
|
|
wmv: "video/x-ms-wmv",
|
|
};
|
|
|
|
for (const attachment of coconutData.attachments) {
|
|
const token = crypto.randomUUID();
|
|
const objectMeta = await context.env.R2.head(`t/${attachment}`);
|
|
|
|
if (!objectMeta) continue;
|
|
|
|
responsePromises.push(
|
|
fetch("https://api.coconut.co/v2/jobs", {
|
|
body: JSON.stringify({
|
|
input: {
|
|
bucket: "car-crushers",
|
|
credentials: {
|
|
access_key_id: context.env.R2_ACCESS_KEY,
|
|
secret_access_key: context.env.R2_SECRET_KEY,
|
|
},
|
|
endpoint: `https://${context.env.R2_ZONE}.r2.cloudflarestorage.com`,
|
|
key: `/t/${attachment}.${contentTypes[objectMeta.httpMetadata?.contentType as string]}`,
|
|
region: "us-east-1",
|
|
service: "s3other",
|
|
},
|
|
notification: {
|
|
params: {
|
|
attachment,
|
|
token,
|
|
},
|
|
type: "http",
|
|
url: `https://${context.request.headers.get("host")}/api/coconut`,
|
|
},
|
|
outputs: {
|
|
mp4: {
|
|
path: `/${attachment}`,
|
|
},
|
|
},
|
|
storage: {
|
|
bucket: "car-crushers",
|
|
credentials: {
|
|
access_key_id: context.env.R2_ACCESS_KEY,
|
|
secret_access_key: context.env.R2_SECRET_KEY,
|
|
},
|
|
endpoint: `https://${context.env.R2_ZONE}.r2.cloudflarestorage.com`,
|
|
region: "us-east-1",
|
|
service: "s3other",
|
|
},
|
|
}),
|
|
headers: {
|
|
authorization: `Basic ${btoa(`${context.env.COCONUT_API_KEY}:`)}`,
|
|
"content-type": "application/json",
|
|
},
|
|
method: "POST",
|
|
}),
|
|
context.env.DATA.put(`coconutjob_${attachment}`, token, {
|
|
expirationTtl: 600,
|
|
}),
|
|
);
|
|
}
|
|
|
|
const resolvedPromises = await Promise.allSettled(responsePromises);
|
|
|
|
for (const p of resolvedPromises) {
|
|
if (p.status === "rejected")
|
|
console.log(`Request to coconut failed: ${p}`);
|
|
else {
|
|
if (!p.value?.ok)
|
|
console.log(
|
|
`Request rejected by coconut: ${await (p.value as Response).text()}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
await context.env.DATA.delete(`coconutdata_${id}`);
|
|
}
|
|
|
|
if (context.env.REPORTS_WEBHOOK) {
|
|
await fetch(context.env.REPORTS_WEBHOOK, {
|
|
body: JSON.stringify({
|
|
embeds: [
|
|
{
|
|
title: "Report Submitted",
|
|
color: 3756250,
|
|
description: `View this report at https://carcrushers.cc/mod-queue?id=${id}&type=report`,
|
|
},
|
|
],
|
|
}),
|
|
headers: {
|
|
"content-type": "application/json",
|
|
},
|
|
method: "POST",
|
|
});
|
|
}
|
|
|
|
return new Response(null, {
|
|
status: 204,
|
|
});
|
|
}
|