From 8d6e631abdd9654d0b0cc3e1dd4db248b91e9c29 Mon Sep 17 00:00:00 2001 From: Regalijan Date: Tue, 26 Mar 2024 23:32:20 -0400 Subject: [PATCH] Fix file copy not working --- functions/api/coconut.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/functions/api/coconut.ts b/functions/api/coconut.ts index 38ebac6..c4c38b5 100644 --- a/functions/api/coconut.ts +++ b/functions/api/coconut.ts @@ -9,9 +9,8 @@ export async function onRequestPost(context: RequestContext) { if (!attachment || !token) return jsonError("Invalid report id or token", 400); - const id = attachment.replace(/\.mp4$/, ""); const coconutToken: string | null = await context.env.DATA.get( - `coconutjob_${id}`, + `coconutjob_${attachment}`, ); if (!coconutToken) @@ -19,14 +18,14 @@ export async function onRequestPost(context: RequestContext) { if (coconutToken !== token) return jsonError("Forbidden", 403); - await context.env.DATA.delete(`coconutjob_${id}`); + await context.env.DATA.delete(`coconutjob_${attachment}`); const aws = new AwsClient({ accessKeyId: context.env.R2_ACCESS_KEY, secretAccessKey: context.env.R2_SECRET_KEY, }); - const { event } = await context.request.json(); + const { data, event } = await context.request.json(); if (event === "job.failed") { await fetch(context.env.REPORTS_WEBHOOK, { @@ -35,7 +34,7 @@ export async function onRequestPost(context: RequestContext) { { title: "Transcoding Failed", color: 16711680, - description: `Attachment ${id} could not be transcoded, please log in to Coconut to see what went wrong.`, + description: `Attachment ${attachment} could not be transcoded, please log in to Coconut to see what went wrong.`, }, ], }), @@ -46,16 +45,20 @@ export async function onRequestPost(context: RequestContext) { }); } else { const R2_URL = `https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com`; - await aws.fetch(`${R2_URL}/${id}`, { + const copyResp = await aws.fetch(`${R2_URL}/${attachment}`, { headers: { - "x-amz-copy-source": `/car-crushers/${attachment}`, + "x-amz-copy-source": `/car-crushers/${attachment}.mp4`, "x-amz-metadata-directive": "COPY", }, method: "PUT", }); - await aws.fetch(`${R2_URL}/${attachment}`, { - method: "DELETE", - }); + + await context.env.R2.delete(attachment); + + if (!copyResp.ok) + console.log( + `Copy status: ${copyResp.status}\n\nCopy response body: ${await copyResp.text()}`, + ); } return new Response(null, {