Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Properly handle multiple jobs per report
- Loading branch information
Showing
3 changed files
with
60 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters