import type { RequestHandler } from './$types';
import { env } from 'process';

export const GET: RequestHandler = async ({ params, fetch }) => {
	const backendUrl = `${env.API_INTERNAL_URL || 'http://localhost:8080'}/api/documents/${params.id}/file`;

	const response = await fetch(backendUrl);

	if (!response.ok) {
		return new Response(null, { status: response.status });
	}

	return new Response(response.body, {
		headers: {
			'Content-Type': response.headers.get('Content-Type') ?? 'application/octet-stream',
			'Content-Disposition': response.headers.get('Content-Disposition') ?? ''
		}
	});
};
