| 1 | import { redirect } from "react-router"; |
| 2 | |
| 3 | import type { Route } from "./+types/actions-artifact"; |
| 4 | import { addresses } from "../../lib/addresses.server"; |
| 5 | import { readArtifact } from "../../lib/artifacts.server"; |
| 6 | import { actions } from "../../lib/services.server"; |
| 7 | import { getViewer } from "../../lib/session.server"; |
| 8 | |
| 9 | /** |
| 10 | * One artifact of a run, for anyone who can see the run: a redirect to a |
| 11 | * link the API signed for a few minutes, or, for one an older runner kept |
| 12 | * in KV, its bytes. |
| 13 | */ |
| 14 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 15 | const repo = { namespace: params.owner, name: params.repo }; |
| 16 | const viewer = getViewer(context); |
| 17 | const found = await actions.artifactDownload(repo, viewer, { run: params.id, name: params.name }); |
| 18 | if (found.ok && found.value.blob) { |
| 19 | throw redirect(`${addresses().api}/actions/toolkit/blobs/${found.value.blob}`); |
| 20 | } |
| 21 | const seen = await actions.run(repo, viewer, params.id); |
| 22 | if (!seen.ok) throw new Response("Not found.", { status: 404 }); |
| 23 | const bytes = await readArtifact(params.id, params.name); |
| 24 | if (!bytes) throw new Response("That artifact is gone: it expired or was deleted.", { status: 404 }); |
| 25 | return new Response(bytes.buffer as ArrayBuffer, { |
| 26 | headers: { |
| 27 | "content-type": "application/gzip", |
| 28 | "content-disposition": `attachment; filename="${params.name.replace(/"/g, "")}.tar.gz"`, |
| 29 | }, |
| 30 | }); |
| 31 | } |