fix(mini-utilities): proxy compress-pdf through Next.js API route to bypass CORS
This commit is contained in:
40
src/app/api/compress-pdf/route.ts
Normal file
40
src/app/api/compress-pdf/route.ts
Normal file
@@ -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 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -486,14 +486,14 @@ function PdfReducer() {
|
|||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("fileInput", file);
|
formData.append("fileInput", file);
|
||||||
formData.append("optimizeLevel", optimizeLevel);
|
formData.append("optimizeLevel", optimizeLevel);
|
||||||
const res = await fetch(
|
const res = await fetch("/api/compress-pdf", {
|
||||||
"http://10.10.10.166:8087/api/v1/misc/compress-pdf",
|
method: "POST",
|
||||||
{
|
body: formData,
|
||||||
method: "POST",
|
});
|
||||||
body: formData,
|
if (!res.ok) {
|
||||||
},
|
const data = await res.json().catch(() => ({}));
|
||||||
);
|
throw new Error(data.error ?? `Eroare server: ${res.status}`);
|
||||||
if (!res.ok) throw new Error(`Eroare server: ${res.status}`);
|
}
|
||||||
const blob = await res.blob();
|
const blob = await res.blob();
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
const a = document.createElement("a");
|
const a = document.createElement("a");
|
||||||
@@ -501,9 +501,11 @@ function PdfReducer() {
|
|||||||
a.download = file.name.replace(/\.pdf$/i, "-comprimat.pdf");
|
a.download = file.name.replace(/\.pdf$/i, "-comprimat.pdf");
|
||||||
a.click();
|
a.click();
|
||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
} catch {
|
} catch (err) {
|
||||||
setError(
|
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 {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user