From 6f250ca0b57efc65128eae612700ab7537d779f0 Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:50:12 -0400 Subject: [PATCH] Create multi-upload status check --- functions/api/uploads/status.ts | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 functions/api/uploads/status.ts diff --git a/functions/api/uploads/status.ts b/functions/api/uploads/status.ts new file mode 100644 index 0000000..609baa8 --- /dev/null +++ b/functions/api/uploads/status.ts @@ -0,0 +1,44 @@ +export async function onRequestPost(context: RequestContext) { + const { body } = context.data; + + if ( + !Array.isArray(body) || + body.find((attachment) => typeof attachment !== "string") + ) + return new Response( + '{"error":"Request body must be an array of strings"}', + { + headers: { + "content-type": "application/json", + }, + status: 400, + }, + ); + + if (body.length > 3) + return new Response('{"error":"Too many video ids"}', { + headers: { + "content-type": "application/json", + }, + status: 400, + }); + + const kvPromises = []; + + for (const attachment of body) + kvPromises.push(context.env.DATA.get(`videoprocessing_${attachment}`)); + + const kvResults = await Promise.allSettled(kvPromises); + + if (kvResults.find((result) => result.status === "rejected")) + return new Response('{"error":"Failed to check status of attachments"}', { + headers: { + "content-type": "application/json", + }, + status: 500, + }); + + return new Response(null, { + status: kvResults.find((result) => result !== null) ? 409 : 204, + }); +}