Move trailer files to new bucket
This commit is contained in:
parent
06d6c572e3
commit
73e4d5e823
@ -1,6 +1,7 @@
|
|||||||
import { Container, Flex, Heading } from "@chakra-ui/react";
|
import { Container, Flex, Heading } from "@chakra-ui/react";
|
||||||
import { type LinksFunction } from "@remix-run/cloudflare";
|
import { type LinksFunction } from "@remix-run/cloudflare";
|
||||||
import { useLoaderData } from "@remix-run/react";
|
import { useLoaderData } from "@remix-run/react";
|
||||||
|
import { AwsClient } from "aws4fetch";
|
||||||
import stylesheet from "../styles/_index.css";
|
import stylesheet from "../styles/_index.css";
|
||||||
|
|
||||||
export const links: LinksFunction = () => {
|
export const links: LinksFunction = () => {
|
||||||
@ -14,47 +15,30 @@ export async function loader({
|
|||||||
}): Promise<string[]> {
|
}): Promise<string[]> {
|
||||||
if (context.request.headers.get("Save-Data") === "on") return [];
|
if (context.request.headers.get("Save-Data") === "on") return [];
|
||||||
|
|
||||||
const signingKey = await crypto.subtle.importKey(
|
const s3base = `https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com`;
|
||||||
"raw",
|
|
||||||
// @ts-expect-error
|
|
||||||
Uint8Array.from(atob(context.env.URL_SIGNING_KEY), (m) => m.codePointAt(0)),
|
|
||||||
{ hash: "SHA-1", name: "HMAC" },
|
|
||||||
false,
|
|
||||||
["sign"],
|
|
||||||
);
|
|
||||||
|
|
||||||
const expiration = Math.floor(Date.now() / 1000) + 600;
|
|
||||||
const baseURLs = [
|
const baseURLs = [
|
||||||
`https://mediaproxy.carcrushers.cc/trailer.webm?Expires=${expiration}&KeyName=portal-media-linkgen`,
|
`https://${s3base}/trailer.webm?X-Amz-Expires=600`,
|
||||||
`https://mediaproxy.carcrushers.cc/trailer.mp4?Expires=${expiration}&KeyName=portal-media-linkgen`,
|
`https://${s3base}/trailer.mp4?X-Amz-Expires=600`,
|
||||||
];
|
];
|
||||||
|
|
||||||
const signaturePromises = [];
|
const aws = new AwsClient({
|
||||||
|
accessKeyId: context.env.R2_ACCESS_KEY,
|
||||||
|
secretAccessKey: context.env.R2_SECRET_KEY,
|
||||||
|
});
|
||||||
|
|
||||||
for (const baseURL of baseURLs)
|
|
||||||
signaturePromises.push(
|
|
||||||
crypto.subtle.sign("HMAC", signingKey, new TextEncoder().encode(baseURL)),
|
|
||||||
);
|
|
||||||
|
|
||||||
const signatures = Array.from(
|
|
||||||
(await Promise.allSettled(signaturePromises)).values(),
|
|
||||||
);
|
|
||||||
const urls = [];
|
const urls = [];
|
||||||
|
|
||||||
for (let i = 0; i < baseURLs.length; i++) {
|
for (let i = 0; i < baseURLs.length; i++) {
|
||||||
const sig = signatures[i];
|
urls.push(
|
||||||
|
(
|
||||||
if (sig.status === "rejected")
|
await aws.sign(baseURLs[i], {
|
||||||
throw new Response(`Failed to create signature for ${baseURLs[i]}`, {
|
aws: {
|
||||||
status: 500,
|
signQuery: true,
|
||||||
});
|
},
|
||||||
|
})
|
||||||
const urlSig = btoa(String.fromCharCode(...new Uint8Array(sig.value)))
|
).url,
|
||||||
.replaceAll("+", "-")
|
);
|
||||||
.replaceAll("/", "_")
|
|
||||||
.replaceAll("=", "");
|
|
||||||
|
|
||||||
urls.push(`${baseURLs[i]}&Signature=${urlSig}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return urls;
|
return urls;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user