umstellung auf claud
This commit is contained in:
1 parent
79c5f0c5cd
commit
5bb4e063da
171 files changed
+5471
-1333
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { BACKEND_URL } from '@/_config/backend';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
@@ -5,33 +6,84 @@ export const dynamic = 'force-dynamic';
|
||||
|
||||
type Context = { params: Promise<{ id: string; path?: string[] }> };
|
||||
|
||||
async function proxy(request: NextRequest, context: Context) {
|
||||
const { id, path = [] } = await context.params;
|
||||
if (!/^[a-f\d]{24}$/i.test(id) || !(
|
||||
path.length === 0 || path.length === 1 && ['file', 'units'].includes(path[0]) ||
|
||||
path.length === 2 && path[0] === 'units' && /^[a-f\d]{24}$/i.test(path[1])
|
||||
)) return Response.json({ message: 'Ungültiger PDF-Pfad' }, { status: 400 });
|
||||
const url = new URL(`http://localhost:3000/anlage/${id}/pdf${path.length ? '/' + path.join('/') : ''}`);
|
||||
url.search = request.nextUrl.search;
|
||||
const headers = new Headers();
|
||||
for (const name of ['content-type', 'range', 'if-range']) {
|
||||
const value = request.headers.get(name);
|
||||
if (value) headers.set(name, value);
|
||||
}
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: request.method, headers, cache: 'no-store',
|
||||
body: request.method === 'GET' ? undefined : await request.arrayBuffer(),
|
||||
});
|
||||
const outgoing = new Headers({ 'Cache-Control': 'no-store' });
|
||||
for (const name of ['content-type', 'content-length', 'content-range', 'accept-ranges']) {
|
||||
const value = response.headers.get(name);
|
||||
if (value) outgoing.set(name, value);
|
||||
const OBJECT_ID_PATTERN = /^[a-f\d]{24}$/i;
|
||||
|
||||
const FORWARDED_REQUEST_HEADERS = [
|
||||
'content-type',
|
||||
'range',
|
||||
'if-range',
|
||||
];
|
||||
|
||||
const FORWARDED_RESPONSE_HEADERS = [
|
||||
'content-type',
|
||||
'content-length',
|
||||
'content-range',
|
||||
'accept-ranges',
|
||||
];
|
||||
|
||||
// Erlaubt sind nur: "", "file", "units" und "units/<id>".
|
||||
function isAllowedPdfPath(path: string[]): boolean {
|
||||
if (path.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (path.length === 1) {
|
||||
return ['file', 'units'].includes(path[0]);
|
||||
}
|
||||
|
||||
if (path.length === 2) {
|
||||
return path[0] === 'units' && OBJECT_ID_PATTERN.test(path[1]);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function copyHeaders(source: Headers, target: Headers, names: string[]): void {
|
||||
for (const name of names) {
|
||||
const value = source.get(name);
|
||||
|
||||
if (value) {
|
||||
target.set(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function proxy(request: NextRequest, context: Context) {
|
||||
const { id, path = [] } = await context.params;
|
||||
|
||||
if (!OBJECT_ID_PATTERN.test(id) || !isAllowedPdfPath(path)) {
|
||||
return Response.json({ message: 'Ungültiger PDF-Pfad' }, { status: 400 });
|
||||
}
|
||||
|
||||
const pathSuffix = path.length ? '/' + path.join('/') : '';
|
||||
const url = new URL(`${BACKEND_URL}/anlage/${id}/pdf${pathSuffix}`);
|
||||
url.search = request.nextUrl.search;
|
||||
|
||||
const headers = new Headers();
|
||||
copyHeaders(request.headers, headers, FORWARDED_REQUEST_HEADERS);
|
||||
|
||||
try {
|
||||
const body = request.method === 'GET'
|
||||
? undefined
|
||||
: await request.arrayBuffer();
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: request.method,
|
||||
headers,
|
||||
cache: 'no-store',
|
||||
body,
|
||||
});
|
||||
|
||||
const outgoing = new Headers({ 'Cache-Control': 'no-store' });
|
||||
copyHeaders(response.headers, outgoing, FORWARDED_RESPONSE_HEADERS);
|
||||
|
||||
return new Response(response.body, {
|
||||
status: response.status,
|
||||
headers: outgoing,
|
||||
});
|
||||
} catch {
|
||||
return Response.json({ message: 'Keine Verbindung zum Backend' }, { status: 502 });
|
||||
}
|
||||
return new Response(response.body, { status: response.status, headers: outgoing });
|
||||
} catch {
|
||||
return Response.json({ message: 'Keine Verbindung zum Backend' }, { status: 502 });
|
||||
}
|
||||
}
|
||||
|
||||
export { proxy as GET, proxy as POST, proxy as PATCH };
|
||||
Reference in new issue
Block a user