fixed file upload
This commit is contained in:
@@ -4,29 +4,51 @@ export const ALL: APIRoute = async ({ request, params }) => {
|
||||
const API_URL = process.env.PUBLIC_API_URL || 'http://backend:3000';
|
||||
const path = params.path;
|
||||
|
||||
if (!path) {
|
||||
return new Response(JSON.stringify({ error: 'Missing path' }), { status: 400 });
|
||||
}
|
||||
|
||||
const url = new URL(`${API_URL}/api/${path}`);
|
||||
|
||||
// Forward search parameters
|
||||
const requestUrl = new URL(request.url);
|
||||
url.search = requestUrl.search;
|
||||
|
||||
// Filter headers to avoid conflicts with the backend container
|
||||
const headers = new Headers();
|
||||
const forbiddenHeaders = ['host', 'connection', 'origin', 'referer', 'content-length'];
|
||||
|
||||
request.headers.forEach((value, key) => {
|
||||
if (!forbiddenHeaders.includes(key.toLowerCase())) {
|
||||
headers.set(key, value);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body: request.method !== 'GET' && request.method !== 'HEAD' ? await request.arrayBuffer() : undefined,
|
||||
// @ts-ignore
|
||||
headers: headers,
|
||||
// Pass the body stream directly for efficiency
|
||||
body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : undefined,
|
||||
// @ts-ignore - duplex is required for streaming bodies in some fetch implementations
|
||||
duplex: 'half'
|
||||
});
|
||||
|
||||
// Extract headers for the response
|
||||
const responseHeaders = new Headers();
|
||||
response.headers.forEach((value, key) => {
|
||||
responseHeaders.set(key, value);
|
||||
});
|
||||
|
||||
return new Response(response.body, {
|
||||
status: response.status,
|
||||
headers: response.headers
|
||||
headers: responseHeaders
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(`Proxy error for ${url}:`, e);
|
||||
return new Response(JSON.stringify({ error: 'Proxy error' }), {
|
||||
status: 500,
|
||||
console.error(`[Proxy Error] ${request.method} ${url}:`, e);
|
||||
return new Response(JSON.stringify({
|
||||
error: 'Proxy connection failed',
|
||||
details: e instanceof Error ? e.message : String(e)
|
||||
}), {
|
||||
status: 502,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user