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
Create multi-upload status check
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 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,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, | ||
}); | ||
} |