diff --git a/src/app/api/compress-pdf/route.ts b/src/app/api/compress-pdf/route.ts new file mode 100644 index 0000000..967268c --- /dev/null +++ b/src/app/api/compress-pdf/route.ts @@ -0,0 +1,40 @@ +import { NextRequest, NextResponse } from "next/server"; + +const STIRLING_PDF_URL = + process.env.STIRLING_PDF_URL ?? "http://10.10.10.166:8087"; + +export async function POST(req: NextRequest) { + try { + const formData = await req.formData(); + + const res = await fetch(`${STIRLING_PDF_URL}/api/v1/misc/compress-pdf`, { + method: "POST", + body: formData, + }); + + if (!res.ok) { + const text = await res.text().catch(() => res.statusText); + return NextResponse.json( + { error: `Stirling PDF error: ${res.status} — ${text}` }, + { status: res.status }, + ); + } + + const blob = await res.blob(); + const buffer = Buffer.from(await blob.arrayBuffer()); + + return new NextResponse(buffer, { + status: 200, + headers: { + "Content-Type": "application/pdf", + "Content-Disposition": 'attachment; filename="compressed.pdf"', + }, + }); + } catch (err) { + const message = err instanceof Error ? err.message : "Unknown error"; + return NextResponse.json( + { error: `Nu s-a putut contacta Stirling PDF: ${message}` }, + { status: 502 }, + ); + } +} diff --git a/src/modules/mini-utilities/components/mini-utilities-module.tsx b/src/modules/mini-utilities/components/mini-utilities-module.tsx index 6941be5..201f952 100644 --- a/src/modules/mini-utilities/components/mini-utilities-module.tsx +++ b/src/modules/mini-utilities/components/mini-utilities-module.tsx @@ -486,14 +486,14 @@ function PdfReducer() { const formData = new FormData(); formData.append("fileInput", file); formData.append("optimizeLevel", optimizeLevel); - const res = await fetch( - "http://10.10.10.166:8087/api/v1/misc/compress-pdf", - { - method: "POST", - body: formData, - }, - ); - if (!res.ok) throw new Error(`Eroare server: ${res.status}`); + const res = await fetch("/api/compress-pdf", { + method: "POST", + body: formData, + }); + if (!res.ok) { + const data = await res.json().catch(() => ({})); + throw new Error(data.error ?? `Eroare server: ${res.status}`); + } const blob = await res.blob(); const url = URL.createObjectURL(blob); const a = document.createElement("a"); @@ -501,9 +501,11 @@ function PdfReducer() { a.download = file.name.replace(/\.pdf$/i, "-comprimat.pdf"); a.click(); URL.revokeObjectURL(url); - } catch { + } catch (err) { setError( - "Nu s-a putut conecta la Stirling PDF. Folosește linkul de mai jos pentru a deschide aplicația manual.", + err instanceof Error + ? err.message + : "Nu s-a putut contacta Stirling PDF.", ); } finally { setLoading(false);