import { NextResponse } from "next/server"; import { errorResponse, requireReady } from "../../../_lib"; import { downloadOutput } from "@/server/spreadsheet-adapter/process-job"; import type { OutputFormat } from "@/domain/spreadsheet-adapter/types"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; export async function GET(request: Request, context: { params: Promise<{ id: string }> }) { try { const { ctx, deps } = await requireReady("download"); const { id } = await context.params; const url = new URL(request.url); const format = url.searchParams.get("format") as OutputFormat | null; const sourceId = url.searchParams.get("sourceId"); if (!sourceId || (format !== "xlsx" && format !== "csv" && format !== "pdf")) { return NextResponse.json({ error: { code: "INVALID_FORMAT", message: "Escolha XLSX, CSV ou PDF." } }, { status: 400 }); } const file = await downloadOutput(deps, ctx, id, sourceId, format); return new NextResponse(new Uint8Array(file.body), { headers: { "Content-Type": file.contentType, "Content-Disposition": `attachment; filename="${file.filename}"`, "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff", }, }); } catch (error) { return errorResponse(error); } }