diff --git a/fresh.gen.ts b/fresh.gen.ts index 1f123c6..712833e 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -4,6 +4,7 @@ import * as $_apps_layout from "./routes/(apps)/_layout.tsx"; import * as $_apps_mobility_api_download from "./routes/(apps)/mobility/api/download.ts"; +import * as $_apps_mobility_api_download_id_ from "./routes/(apps)/mobility/api/download/[id].ts"; import * as $_apps_mobility_api_insert_mobility from "./routes/(apps)/mobility/api/insert-mobility.ts"; import * as $_apps_mobility_index from "./routes/(apps)/mobility/index.tsx"; import * as $_apps_mobility_partials_admin_edit_mobility from "./routes/(apps)/mobility/partials/(admin)/edit_mobility.tsx"; @@ -41,6 +42,8 @@ const manifest = { routes: { "./routes/(apps)/_layout.tsx": $_apps_layout, "./routes/(apps)/mobility/api/download.ts": $_apps_mobility_api_download, + "./routes/(apps)/mobility/api/download/[id].ts": + $_apps_mobility_api_download_id_, "./routes/(apps)/mobility/api/insert-mobility.ts": $_apps_mobility_api_insert_mobility, "./routes/(apps)/mobility/index.tsx": $_apps_mobility_index, diff --git a/routes/(apps)/mobility/api/download/[id].ts b/routes/(apps)/mobility/api/download/[id].ts new file mode 100644 index 0000000..885ee71 --- /dev/null +++ b/routes/(apps)/mobility/api/download/[id].ts @@ -0,0 +1,36 @@ +import { Handlers } from "$fresh/server.ts"; +import connect from "$root/databases/connect.ts"; + +export const handler: Handlers = { + async GET(_request, ctx) { + try { + const { id } = ctx.params; + + if (!id) { + return new Response("Invalid request: Missing ID", { status: 400 }); + } + + using connection = connect("mobility"); + + const query = connection.database.prepare( + "SELECT attestationFile FROM mobility WHERE id = ?", + ); + const result = query.get(id); + + if (!result || !result.attestationFile) { + return new Response("No file found for the given ID", { status: 404 }); + } + + return new Response(result.attestationFile, { + status: 200, + headers: { + "Content-Type": "application/pdf", + "Content-Disposition": `attachment; filename="attestation_${id}.pdf"`, + }, + }); + } catch (error) { + console.error("Error fetching file:", error); + return new Response("Failed to fetch file", { status: 500 }); + } + }, +};