import { redirect, type LoaderFunctionArgs } from "react-router"; import { assertFileKeyBelongsToTenant } from "@hivegrid/storage"; import { auditPortalDocumentDownload, getPortalReleasedFile, portalNotFound, requirePortalSessionContext } from "~/services/customer-portal/portal-data.server"; import { isFileScanDeliverable } from "~/utils/integrations/fileScanPolicy"; import { createMediaDownloadUrl } from "~/utils/integrations/mediaSigning.server"; import { recordAuthorizedStorageDownload } from "~/lib/metrics/storage-traffic"; export async function loader({ request, params }: LoaderFunctionArgs) { if (!params.releaseId) throw portalNotFound(); const session = await requirePortalSessionContext(request, params.tenant); const { identity } = session; const file = await getPortalReleasedFile(identity, params.releaseId); if (!file || !isFileScanDeliverable(file.scanStatus)) throw portalNotFound(); try { assertFileKeyBelongsToTenant(identity.tenantId, file.storageKey, "download"); } catch { throw portalNotFound(); } const signedUrl = await createMediaDownloadUrl({ key: file.storageKey, filename: file.displayName, ttlSeconds: 60 }); await auditPortalDocumentDownload(identity, params.releaseId, request); recordAuthorizedStorageDownload({ surface: "portal_document", delivery: "signed_redirect", objectSizeBytes: null }); throw redirect(signedUrl, { headers: session.responseHeaders }); }