le-app/app/middleware.ts

63 lines
1.8 KiB
TypeScript
Raw Normal View History

2026-07-10 12:18:27 -04:00
import { NextRequest, NextResponse } from "next/server";
2026-07-10 12:18:27 -04:00
const PUBLIC_UTILITY_APIS = new Set([
"/api/bgbye/process",
"/api/files/list",
"/api/files/raw",
"/api/files/download",
]);
2026-07-10 12:18:27 -04:00
const LEGACY_TOOL_PATHS: Record<string, string> = {
"/laser-toolkit": "laser-toolkit",
"/files": "files",
"/background-remover": "background-remover",
};
2026-07-10 12:18:27 -04:00
/**
* Temporary utility-only site.
*
* Directus-backed pages and APIs remain in source history but cannot be reached
* through this deployment. This middleware deliberately performs no session or
* Directus request.
*/
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
if (pathname === "/") return NextResponse.next();
const legacyTool = LEGACY_TOOL_PATHS[pathname.replace(/\/$/, "")];
if (legacyTool) {
const url = req.nextUrl.clone();
url.pathname = "/";
url.search = "";
url.searchParams.set("tool", legacyTool);
return NextResponse.redirect(url);
}
if (pathname === "/portal/utilities") {
const url = req.nextUrl.clone();
const requested = url.searchParams.get("t");
url.pathname = "/";
url.search = "";
if (requested && ["laser-toolkit", "files", "background-remover"].includes(requested)) {
url.searchParams.set("tool", requested);
}
return NextResponse.redirect(url);
}
if (PUBLIC_UTILITY_APIS.has(pathname)) return NextResponse.next();
if (pathname.startsWith("/api/")) {
return NextResponse.json({ error: "Not available on the temporary utility site" }, { status: 404 });
}
const home = req.nextUrl.clone();
home.pathname = "/";
home.search = "";
return NextResponse.redirect(home);
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml|images|static).*)"],
};