|
| 1 | +import type { ActionFunctionArgs } from "@remix-run/server-runtime"; |
| 2 | +import { json } from "@remix-run/server-runtime"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { env } from "~/env.server"; |
| 5 | +import { authenticateApiRequest } from "~/services/apiAuth.server"; |
| 6 | +import { logger } from "~/services/logger.server"; |
| 7 | +import { r2 } from "~/v3/r2.server"; |
| 8 | + |
| 9 | +const ParamsSchema = z.object({ |
| 10 | + "*": z.string(), |
| 11 | +}); |
| 12 | + |
| 13 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 14 | + // Ensure this is a POST request |
| 15 | + if (request.method.toUpperCase() !== "PUT") { |
| 16 | + return { status: 405, body: "Method Not Allowed" }; |
| 17 | + } |
| 18 | + |
| 19 | + // Next authenticate the request |
| 20 | + const authenticationResult = await authenticateApiRequest(request); |
| 21 | + |
| 22 | + if (!authenticationResult) { |
| 23 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 24 | + } |
| 25 | + |
| 26 | + const parsedParams = ParamsSchema.parse(params); |
| 27 | + const filename = parsedParams["*"]; |
| 28 | + |
| 29 | + if (!env.OBJECT_STORE_BASE_URL) { |
| 30 | + return json({ error: "Object store base URL is not set" }, { status: 500 }); |
| 31 | + } |
| 32 | + |
| 33 | + if (!r2) { |
| 34 | + return json({ error: "Object store credentials are not set" }, { status: 500 }); |
| 35 | + } |
| 36 | + |
| 37 | + const url = new URL(env.OBJECT_STORE_BASE_URL); |
| 38 | + url.pathname = `/packets/${authenticationResult.environment.project.externalRef}/${authenticationResult.environment.slug}/${filename}`; |
| 39 | + url.searchParams.set("X-Amz-Expires", "300"); // 5 minutes |
| 40 | + |
| 41 | + const signed = await r2.sign( |
| 42 | + new Request(url, { |
| 43 | + method: "PUT", |
| 44 | + }), |
| 45 | + { |
| 46 | + aws: { signQuery: true }, |
| 47 | + } |
| 48 | + ); |
| 49 | + |
| 50 | + logger.debug("Generated presigned URL", { |
| 51 | + url: signed.url, |
| 52 | + headers: Object.fromEntries(signed.headers), |
| 53 | + }); |
| 54 | + |
| 55 | + // Caller can now use this URL to upload to that object. |
| 56 | + return json({ presignedUrl: signed.url }); |
| 57 | +} |
| 58 | + |
| 59 | +export async function loader({ request, params }: ActionFunctionArgs) { |
| 60 | + // Next authenticate the request |
| 61 | + const authenticationResult = await authenticateApiRequest(request); |
| 62 | + |
| 63 | + if (!authenticationResult) { |
| 64 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 65 | + } |
| 66 | + |
| 67 | + const parsedParams = ParamsSchema.parse(params); |
| 68 | + const filename = parsedParams["*"]; |
| 69 | + |
| 70 | + if (!env.OBJECT_STORE_BASE_URL) { |
| 71 | + return json({ error: "Object store base URL is not set" }, { status: 500 }); |
| 72 | + } |
| 73 | + |
| 74 | + if (!r2) { |
| 75 | + return json({ error: "Object store credentials are not set" }, { status: 500 }); |
| 76 | + } |
| 77 | + |
| 78 | + const url = new URL(env.OBJECT_STORE_BASE_URL); |
| 79 | + url.pathname = `/packets/${authenticationResult.environment.project.externalRef}/${authenticationResult.environment.slug}/${filename}`; |
| 80 | + url.searchParams.set("X-Amz-Expires", "300"); // 5 minutes |
| 81 | + |
| 82 | + const signed = await r2.sign( |
| 83 | + new Request(url, { |
| 84 | + method: request.method, |
| 85 | + }), |
| 86 | + { |
| 87 | + aws: { signQuery: true }, |
| 88 | + } |
| 89 | + ); |
| 90 | + |
| 91 | + logger.debug("Generated presigned URL", { |
| 92 | + url: signed.url, |
| 93 | + headers: Object.fromEntries(signed.headers), |
| 94 | + }); |
| 95 | + |
| 96 | + const getUrl = new URL(url.href); |
| 97 | + getUrl.searchParams.delete("X-Amz-Expires"); |
| 98 | + |
| 99 | + // Caller can now use this URL to upload to that object. |
| 100 | + return json({ presignedUrl: signed.url }); |
| 101 | +} |
0 commit comments