Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use R2 for upload signing endpoint
  • Loading branch information
regalijan committed Mar 25, 2024
1 parent 32f3cf9 commit 89ce6ec
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions functions/api/uploads/[[id]].ts
@@ -1,27 +1,22 @@
import { AwsClient } from "aws4fetch";

export async function onRequestGet(context: RequestContext) {
const attachment = (context.params.id as string[]).join("/");
const unsignedURL = `https://mediaproxy.carcrushers.cc/${attachment}?Expires=${(
Math.round(Date.now() / 1000) + 1800
).toString()}&KeyName=portal-media-linkgen`;
const signingKey = await crypto.subtle.importKey(
"raw",
Uint8Array.from(atob(context.env.URL_SIGNING_KEY), (c) => c.charCodeAt(0)),
{ hash: "SHA-1", name: "HMAC" },
false,
["sign"],
);
const signature = await crypto.subtle.sign(
"HMAC",
signingKey,
new TextEncoder().encode(unsignedURL),
);
const aws = new AwsClient({
accessKeyId: context.env.R2_ACCESS_KEY,
secretAccessKey: context.env.R2_SECRET_KEY,
});

return Response.redirect(
`${unsignedURL}&Signature=${btoa(
String.fromCharCode(...new Uint8Array(signature)),
)
.replaceAll("+", "-")
.replaceAll("/", "_")
.replaceAll("=", "")}`,
(
await aws.sign(
`https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com/${attachment}?X-Amz-Expires=1800`,
{
aws: {
signQuery: true,
},
},
)
).url,
);
}

0 comments on commit 89ce6ec

Please sign in to comment.