import { AwsClient } from "aws4fetch";

export async function onRequestGet(context: RequestContext) {
  const attachment = (context.params.id as string[]).join("/");
  const aws = new AwsClient({
    accessKeyId: context.env.R2_ACCESS_KEY,
    secretAccessKey: context.env.R2_SECRET_KEY,
  });

  return Response.redirect(
    (
      await aws.sign(
        `https://car-crushers.${context.env.R2_ZONE}.r2.cloudflarestorage.com/${attachment}?X-Amz-Expires=1800`,
        {
          aws: {
            signQuery: true,
          },
        },
      )
    ).url,
  );
}
