fix(media): enhance media proxy routing with multi-target fallback and add uploads location to nginx
This commit is contained in:
1 parent
fa1a35c835
commit
b68dd29541
14 files changed
+12689
-12044
No files matched your search
@@ -2,6 +2,29 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
function getCandidateUrls(filePath: string): string[] {
|
||||
const set = new Set<string>();
|
||||
|
||||
if (process.env.INTERNAL_API_URL) {
|
||||
const base = process.env.INTERNAL_API_URL.replace(/\/api\/?$/, '');
|
||||
set.add(`${base}/uploads/${filePath}`);
|
||||
}
|
||||
|
||||
if (process.env.NEXT_PUBLIC_API_URL && !process.env.NEXT_PUBLIC_API_URL.startsWith('/')) {
|
||||
const base = process.env.NEXT_PUBLIC_API_URL.replace(/\/api\/?$/, '');
|
||||
set.add(`${base}/uploads/${filePath}`);
|
||||
}
|
||||
|
||||
set.add(`http://backend_prod:3000/uploads/${filePath}`);
|
||||
set.add(`http://canino_backend_prod:3000/uploads/${filePath}`);
|
||||
set.add(`http://backend:3000/uploads/${filePath}`);
|
||||
|
||||
set.add(`http://127.0.0.1:4001/uploads/${filePath}`);
|
||||
set.add(`http://localhost:4001/uploads/${filePath}`);
|
||||
|
||||
return Array.from(set);
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ path: string[] }> }
|
||||
@@ -13,13 +36,7 @@ export async function GET(
|
||||
}
|
||||
|
||||
const filePath = path.map(segment => encodeURIComponent(segment)).join('/');
|
||||
const backendBase = (
|
||||
process.env.INTERNAL_API_URL ||
|
||||
process.env.NEXT_PUBLIC_API_URL ||
|
||||
'http://127.0.0.1:4001'
|
||||
).replace(/\/api\/?$/, '');
|
||||
|
||||
const targetUrl = `${backendBase}/uploads/${filePath}`;
|
||||
const candidates = getCandidateUrls(filePath);
|
||||
|
||||
const forwardedHeaders: HeadersInit = {};
|
||||
const rangeHeader = request.headers.get('range');
|
||||
@@ -29,16 +46,27 @@ export async function GET(
|
||||
const ifModifiedSince = request.headers.get('if-modified-since');
|
||||
if (ifModifiedSince) forwardedHeaders['if-modified-since'] = ifModifiedSince;
|
||||
|
||||
const backendRes = await fetch(targetUrl, {
|
||||
method: 'GET',
|
||||
headers: forwardedHeaders,
|
||||
cache: 'no-store',
|
||||
});
|
||||
let backendRes: Response | null = null;
|
||||
|
||||
if (!backendRes.ok && backendRes.status !== 304 && backendRes.status !== 206) {
|
||||
return new NextResponse(`Media not found or error from upstream (${backendRes.status})`, {
|
||||
status: backendRes.status,
|
||||
});
|
||||
for (const targetUrl of candidates) {
|
||||
try {
|
||||
const res = await fetch(targetUrl, {
|
||||
method: 'GET',
|
||||
headers: forwardedHeaders,
|
||||
cache: 'no-store',
|
||||
});
|
||||
|
||||
if (res.ok || res.status === 304 || res.status === 206) {
|
||||
backendRes = res;
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
// Continue
|
||||
}
|
||||
}
|
||||
|
||||
if (!backendRes) {
|
||||
return new NextResponse('Media file not found upstream', { status: 404 });
|
||||
}
|
||||
|
||||
const responseHeaders = new Headers();
|
||||
@@ -85,14 +113,25 @@ export async function HEAD(
|
||||
}
|
||||
|
||||
const filePath = path.map(segment => encodeURIComponent(segment)).join('/');
|
||||
const backendBase = (
|
||||
process.env.INTERNAL_API_URL ||
|
||||
process.env.NEXT_PUBLIC_API_URL ||
|
||||
'http://127.0.0.1:4001'
|
||||
).replace(/\/api\/?$/, '');
|
||||
const candidates = getCandidateUrls(filePath);
|
||||
|
||||
const targetUrl = `${backendBase}/uploads/${filePath}`;
|
||||
const backendRes = await fetch(targetUrl, { method: 'HEAD', cache: 'no-store' });
|
||||
let backendRes: Response | null = null;
|
||||
|
||||
for (const targetUrl of candidates) {
|
||||
try {
|
||||
const res = await fetch(targetUrl, { method: 'HEAD', cache: 'no-store' });
|
||||
if (res.ok || res.status === 304) {
|
||||
backendRes = res;
|
||||
break;
|
||||
}
|
||||
} catch {
|
||||
// Continue
|
||||
}
|
||||
}
|
||||
|
||||
if (!backendRes) {
|
||||
return new NextResponse(null, { status: 404 });
|
||||
}
|
||||
|
||||
const responseHeaders = new Headers();
|
||||
const contentType = backendRes.headers.get('content-type') || 'application/octet-stream';
|
||||
|
||||
Reference in new issue
Block a user