From 6b85d517cac0b77fd84d5cc81acd8dd159bac041 Mon Sep 17 00:00:00 2001 From: Regalijan Date: Tue, 26 Mar 2024 03:55:49 -0400 Subject: [PATCH] Actually do what coconut was asking me to --- functions/api/coconut.ts | 23 +++++++++++++++++++++-- functions/api/reports/complete.ts | 13 ++----------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/functions/api/coconut.ts b/functions/api/coconut.ts index b97b4c9..dfc7d81 100644 --- a/functions/api/coconut.ts +++ b/functions/api/coconut.ts @@ -1,12 +1,15 @@ +import { AwsClient } from "aws4fetch"; import { jsonError } from "../common.js"; export async function onRequestPost(context: RequestContext) { const { searchParams } = new URL(context.request.url); - const id = searchParams.get("attachment"); + const attachment = searchParams.get("attachment"); const token = searchParams.get("token"); - if (!id || !token) return jsonError("Invalid report id or token", 400); + if (!attachment || !token) + return jsonError("Invalid report id or token", 400); + const id = attachment.replace(/\.mp4$/, ""); const coconutData: { token: string } | null = await context.env.DATA.get( `coconutjob_${id}`, { type: "json" }, @@ -19,6 +22,11 @@ export async function onRequestPost(context: RequestContext) { await context.env.DATA.delete(`coconutjob_${id}`); + const aws = new AwsClient({ + accessKeyId: context.env.R2_ACCESS_KEY, + secretAccessKey: context.env.R2_SECRET_KEY, + }); + const { event } = await context.request.json(); if (event === "job.failed") { @@ -37,6 +45,17 @@ export async function onRequestPost(context: RequestContext) { }, method: "POST", }); + } else { + const R2_URL = `https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com`; + await aws.fetch(`${R2_URL}/${id}`, { + headers: { + "x-amz-copy-source": `car-crushers/${attachment}`, + }, + method: "PUT", + }); + await aws.fetch(`${R2_URL}/${attachment}`, { + method: "DELETE", + }); } return new Response(null, { diff --git a/functions/api/reports/complete.ts b/functions/api/reports/complete.ts index 31a9b28..1964530 100644 --- a/functions/api/reports/complete.ts +++ b/functions/api/reports/complete.ts @@ -29,15 +29,6 @@ export async function onRequestPost(context: RequestContext) { 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(); @@ -57,7 +48,7 @@ export async function onRequestPost(context: RequestContext) { 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]}`, + key: `/t/${attachment}`, region: "us-east-1", service: "s3other", }, @@ -71,7 +62,7 @@ export async function onRequestPost(context: RequestContext) { }, outputs: { mp4: { - path: `/${attachment}`, + path: `/${attachment}.mp4`, }, }, storage: {