import { NextRequest, NextResponse } from 'next/server';

const API_URL = process.env.API_URL;

export async function GET(_: NextRequest, { params }: { params: Promise<{ path: string[] }> })
{
	const { path } = await params;
	const filePath = `/uploads/${path.join('/')}`;

	try {
		const res = await fetch(`${API_URL}${filePath}`, {
			cache: 'no-store'
		});

		if (!res.ok) {
			return new NextResponse(null, {
				status: res.status
			});
		}

		const contentType = res.headers.get('content-type') || 'application/octet-stream';
		const buffer = await res.arrayBuffer();

		return new NextResponse(buffer, {
			status: 200,
			headers: {
				'Content-Type': contentType,
				'Cache-Control': 'public, max-age=31536000, immutable'
			}
		});
	} catch {
		return new NextResponse(null, {
			status: 502
		});
	}
}