import { BACKEND_URL } from '@/_config/backend'; type BackupFileRouteContext = { params: Promise<{ id: string }>; }; const UNSAFE_FILENAME_CHARACTERS = /[^A-Za-z0-9_.-]/g; // Nur der letzte Pfadteil wird als Download-Name verwendet, // unsichere Zeichen werden ersetzt. function toDownloadFilename(file: string): string { const lastPathPart = file.split('/').pop(); return lastPathPart?.replace(UNSAFE_FILENAME_CHARACTERS, '_') ?? 'backup.json'; } export async function GET(request: Request, { params }: BackupFileRouteContext) { const { id } = await params; const file = new URL(request.url).searchParams.get('file') ?? 'manifest.json'; const backupFileUrl = `${BACKEND_URL}/rk2/backups/${encodeURIComponent(id)}/file` + `?file=${encodeURIComponent(file)}`; try { const response = await fetch(backupFileUrl, { cache: 'no-store' }); if (!response.ok) { return new Response('Sicherungsdatei nicht verfügbar', { status: response.status }); } const filename = toDownloadFilename(file); return new Response(response.body, { headers: { 'Content-Type': response.headers.get('Content-Type') ?? 'application/json', 'Content-Disposition': 'attachment; filename="' + filename + '"', 'Cache-Control': 'no-store', }, }); } catch { return new Response('Keine Verbindung zum Backend', { status: 502 }); } }