Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
car-crushers-portal/functions/api/uploads/[[id]].ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
27 lines (26 sloc)
852 Bytes
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
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), | |
); | |
return Response.redirect( | |
`${unsignedURL}&Signature=${btoa( | |
String.fromCharCode(...new Uint8Array(signature)), | |
) | |
.replaceAll("+", "-") | |
.replaceAll("/", "_") | |
.replaceAll("=", "")}`, | |
); | |
} |