Properly handle multiple jobs per report
This commit is contained in:
45
functions/api/coconut.ts
Normal file
45
functions/api/coconut.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { jsonError } from "../common.js";
|
||||
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { searchParams } = new URL(context.request.url);
|
||||
const id = searchParams.get("attachment");
|
||||
const token = searchParams.get("token");
|
||||
|
||||
if (!id || !token) return jsonError("Invalid report id or token", 400);
|
||||
|
||||
const coconutData: { token: string } | null = await context.env.DATA.get(
|
||||
`coconutjob_${id}`,
|
||||
{ type: "json" },
|
||||
);
|
||||
|
||||
if (!coconutData)
|
||||
return jsonError("Request is stale or otherwise invalid", 400);
|
||||
|
||||
if (coconutData.token !== token) return jsonError("Forbidden", 403);
|
||||
|
||||
await context.env.DATA.delete(`coconutjob_${id}`);
|
||||
|
||||
const { event } = await context.request.json();
|
||||
|
||||
if (event === "job.failed") {
|
||||
await fetch(context.env.REPORTS_WEBHOOK, {
|
||||
body: JSON.stringify({
|
||||
embeds: [
|
||||
{
|
||||
title: "Transcoding Failed",
|
||||
color: 16711680,
|
||||
description: `Attachment ${id} could not be transcoded, please log in to Coconut to see what went wrong.`,
|
||||
},
|
||||
],
|
||||
}),
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user