Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle coconut's requirement for file extensions
  • Loading branch information
regalijan committed Mar 26, 2024
1 parent 2f335cf commit c06375e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 14 additions & 2 deletions functions/api/reports/complete.ts
Expand Up @@ -29,9 +29,21 @@ 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();
const objectMeta = await context.env.R2.head(attachment);

if (!objectMeta) continue;

responsePromises.push(
fetch("https://api.coconut.co/v2/jobs", {
Expand All @@ -43,13 +55,13 @@ 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}`,
key: `/t/${attachment}.${contentTypes[objectMeta.httpMetadata?.contentType as string]}`,
region: "us-east-1",
service: "s3other",
},
notification: {
params: {
attachment: attachment,
attachment,
token,
},
type: "http",
Expand Down
10 changes: 8 additions & 2 deletions functions/api/reports/submit.ts
Expand Up @@ -163,7 +163,9 @@ export async function onRequestPost(context: RequestContext) {
uploadUrlPromises.push(
upload(
context.env,
`${["mp4", "m4v", "webm"].includes(fileExten) ? "" : "t/"}${fileUploadKey}`,
["mp4", "m4v", "webm"].includes(fileExten)
? fileUploadKey
: `t/${fileUploadKey}.${fileExten}`,
file.size,
fileExten,
),
Expand Down Expand Up @@ -206,7 +208,11 @@ export async function onRequestPost(context: RequestContext) {

for (const urlResult of uploadUrlResults as PromiseFulfilledResult<string>[]) {
uploadUrls.push(urlResult.value);
attachments.push(new URL(urlResult.value).pathname.replace(/^\/?t?\//, ""));
attachments.push(
new URL(urlResult.value).pathname
.replace(/^\/?t?\//, "")
.replace(/\.w*$/, ""),
);
}

await context.env.DATA.put(
Expand Down

0 comments on commit c06375e

Please sign in to comment.