Skip to content
29 linesCodeBlameRaw
1import { data } from "react-router";
2
3import type { Route } from "./+types/export";
4import { folios } from "../../../lib/services.server";
5import { requireUser, roleIn } from "../../../lib/session.server";
6
7/**
8 * An artifact as a file: `?folio=<id>` downloads a doc as `<title>.md`
9 * (`&format=json` for its agent form). `&inline=1` answers the text
10 * itself, for "Copy as Markdown". Only what the viewer can read.
11 */
12export async function loader({ params, context, request }: Route.LoaderArgs) {
13 const viewer = requireUser(context, request);
14 if (!roleIn(viewer, params.owner)) throw data(null, { status: 404 });
15 const url = new URL(request.url);
16 const id = url.searchParams.get("folio");
17 if (!id) throw data("Which artifact?", { status: 400 });
18 const format = url.searchParams.get("format") === "json" ? "json" : null;
19 const found = await folios.export(params.owner.toLowerCase(), viewer, id, format);
20 if (!found.ok) throw data(found.error.message, { status: found.error.code === "not_found" ? 404 : found.error.code === "forbidden" ? 403 : 422 });
21 const inline = url.searchParams.get("inline") === "1";
22 return new Response(found.value.body, {
23 headers: {
24 "content-type": found.value.content_type,
25 ...(inline ? {} : { "content-disposition": `attachment; filename*=UTF-8''${encodeURIComponent(found.value.filename)}` }),
26 "cache-control": "no-store",
27 },
28 });
29}