Skip to content
Permalink
Newer
Older
100644 67 lines (55 sloc) 1.9 KB
1
import { AwsClient } from "aws4fetch";
2
import { jsonError } from "../common.js";
3
4
export async function onRequestPost(context: RequestContext) {
5
const { searchParams } = new URL(context.request.url);
6
const attachment = searchParams.get("attachment");
7
const token = searchParams.get("token");
8
9
if (!attachment || !token)
10
return jsonError("Invalid report id or token", 400);
12
const coconutToken: string | null = await context.env.DATA.get(
March 26, 2024 23:32
13
`coconutjob_${attachment}`,
14
);
15
16
if (!coconutToken)
17
return jsonError("Request is stale or otherwise invalid", 400);
18
19
if (coconutToken !== token) return jsonError("Forbidden", 403);
March 26, 2024 23:32
21
await context.env.DATA.delete(`coconutjob_${attachment}`);
23
const aws = new AwsClient({
24
accessKeyId: context.env.R2_ACCESS_KEY,
25
secretAccessKey: context.env.R2_SECRET_KEY,
26
});
27
March 26, 2024 23:53
28
const { event } = await context.request.json();
29
30
if (event === "job.failed") {
31
await fetch(context.env.REPORTS_WEBHOOK, {
32
body: JSON.stringify({
33
embeds: [
34
{
35
title: "Transcoding Failed",
36
color: 16711680,
March 26, 2024 23:32
37
description: `Attachment ${attachment} could not be transcoded, please log in to Coconut to see what went wrong.`,
38
},
39
],
40
}),
41
headers: {
42
"content-type": "application/json",
43
},
44
method: "POST",
45
});
46
} else {
47
const R2_URL = `https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com`;
March 26, 2024 23:32
48
const copyResp = await aws.fetch(`${R2_URL}/${attachment}`, {
49
headers: {
March 26, 2024 23:32
50
"x-amz-copy-source": `/car-crushers/${attachment}.mp4`,
March 26, 2024 04:38
51
"x-amz-metadata-directive": "COPY",
52
},
53
method: "PUT",
54
});
March 26, 2024 23:32
55
March 27, 2024 00:04
56
await context.env.R2.delete(`${attachment}.mp4`);
March 26, 2024 23:32
57
58
if (!copyResp.ok)
59
console.log(
60
`Copy status: ${copyResp.status}\n\nCopy response body: ${await copyResp.text()}`,
61
);
62
}
63
64
return new Response(null, {
65
status: 204,
66
});
67
}