This commit is contained in:
2025-12-06 02:31:33 +01:00
parent 2fe3f683d6
commit b76a113c4d
4 changed files with 23 additions and 45 deletions
+7 -20
View File
@@ -1,11 +1,4 @@
import { type NextRequest, NextResponse } from "next/server";
import { Agent, fetch } from "undici";
const insecureAgent = new Agent({
connect: {
rejectUnauthorized: false,
},
});
export async function GET(req: NextRequest) {
try {
@@ -40,7 +33,6 @@ export async function GET(req: NextRequest) {
const externalResponse = await fetch(link, {
method: "GET",
dispatcher: insecureAgent,
});
if (!externalResponse.ok) {
@@ -50,25 +42,20 @@ export async function GET(req: NextRequest) {
);
}
const body = await externalResponse.arrayBuffer();
const headers = new Headers(externalResponse.headers);
headers.set("Cache-Control", "s-maxage=31536000, stale-while-revalidate");
const contentType = externalResponse.headers.get("content-type");
const headers = new Headers({
"Cache-Control": "s-maxage=31536000, stale-while-revalidate",
});
if (contentType) {
headers.set("Content-Type", contentType);
if (contentType === "application/pdf") {
const filename = url.pathname.split("/").pop() ?? "document.pdf";
headers.set("Content-Disposition", `inline; filename="${filename}"`);
}
if (contentType === "application/pdf") {
const filename = url.pathname.split("/").pop() ?? "document.pdf";
headers.set("Content-Disposition", `inline; filename="${filename}"`);
}
return new Response(body, {
return new NextResponse(externalResponse.body, {
status: 200,
headers,
});
} catch (e: unknown) {
console.error("Proxy Error:", e);
const errorMessage =
+15 -20
View File
@@ -1,11 +1,4 @@
import { type NextRequest, NextResponse } from "next/server";
import { Agent, fetch } from "undici";
const insecureAgent = new Agent({
connect: {
rejectUnauthorized: false,
},
});
const ALLOWED_HOSTS = [
"localhost:3000",
@@ -16,7 +9,7 @@ const ALLOWED_HOSTS = [
export async function GET(req: NextRequest) {
try {
const { searchParams } = req.nextUrl;
const link = searchParams.get("link");
let link = searchParams.get("link");
if (!link) {
return NextResponse.json({
@@ -41,26 +34,28 @@ export async function GET(req: NextRequest) {
);
}
if (url.pathname === "/api/proxy" && url.searchParams.has("link")) {
const realTarget = url.searchParams.get("link");
if (realTarget) {
try {
const realUrl = new URL(realTarget);
link = realTarget;
url = realUrl;
} catch {}
}
}
if (!ALLOWED_HOSTS.includes(url.host)) {
return NextResponse.json({ error: "Érvénytelen link" }, { status: 400 });
}
const response = await fetch(link, {
method: "HEAD",
dispatcher: insecureAgent,
});
return NextResponse.json({ status: response.status }, { status: 200 });
} catch (e: unknown) {
console.error("Validation Error:", e);
const errorResponse = {
error: "Internal Server Error",
message: e instanceof Error ? e.message : "An unexpected error occurred",
...(process.env.NODE_ENV === "development" &&
e instanceof Error && { stack: e.stack }),
};
return NextResponse.json(errorResponse, { status: 500 });
} catch (error) {
console.error("Validation Error:", error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}