fix(media): enhance media proxy routing with multi-target fallback and add uploads location to nginx
Deploy Canina / deploy (push) Successful in 1m28s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 6s

This commit is contained in:
parsa aghaei committed 2026-08-29 13:25:48 +03:30
1 parent fa1a35c835
commit b68dd29541
14 files changed
+12689 -12044

No files matched your search

@@ -2,6 +2,31 @@ 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}`);
}
// Docker internal service aliases
set.add(`http://backend_prod:3000/uploads/${filePath}`);
set.add(`http://canino_backend_prod:3000/uploads/${filePath}`);
set.add(`http://backend:3000/uploads/${filePath}`);
// Local development ports
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,72 +38,59 @@ 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 candidates = getCandidateUrls(filePath);
const targetUrl = `${backendBase}/uploads/${filePath}`;
// Forward streaming/caching headers from client to backend
// Forward streaming/caching headers from client
const forwardedHeaders: HeadersInit = {};
const rangeHeader = request.headers.get('range');
if (rangeHeader) {
forwardedHeaders['range'] = rangeHeader;
}
if (rangeHeader) forwardedHeaders['range'] = rangeHeader;
const ifNoneMatch = request.headers.get('if-none-match');
if (ifNoneMatch) {
forwardedHeaders['if-none-match'] = ifNoneMatch;
}
if (ifNoneMatch) forwardedHeaders['if-none-match'] = ifNoneMatch;
const ifModifiedSince = request.headers.get('if-modified-since');
if (ifModifiedSince) {
forwardedHeaders['if-modified-since'] = ifModifiedSince;
if (ifModifiedSince) forwardedHeaders['if-modified-since'] = ifModifiedSince;
let backendRes: Response | null = null;
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 trying next candidate
}
}
const backendRes = await fetch(targetUrl, {
method: 'GET',
headers: forwardedHeaders,
cache: 'no-store', // Always stream directly or use Next.js response headers
});
if (!backendRes.ok && backendRes.status !== 304 && backendRes.status !== 206) {
return new NextResponse(`Media not found or error from upstream (${backendRes.status})`, {
status: backendRes.status,
});
if (!backendRes) {
return new NextResponse('Media file not found upstream', { status: 404 });
}
// Build response headers with long-term caching and media streaming support
const responseHeaders = new Headers();
// Forward essential media headers
const contentType = backendRes.headers.get('content-type') || 'application/octet-stream';
responseHeaders.set('Content-Type', contentType);
const contentLength = backendRes.headers.get('content-length');
if (contentLength) {
responseHeaders.set('Content-Length', contentLength);
}
if (contentLength) responseHeaders.set('Content-Length', contentLength);
const contentRange = backendRes.headers.get('content-range');
if (contentRange) {
responseHeaders.set('Content-Range', contentRange);
}
if (contentRange) responseHeaders.set('Content-Range', contentRange);
const acceptRanges = backendRes.headers.get('accept-ranges') || 'bytes';
responseHeaders.set('Accept-Ranges', acceptRanges);
const etag = backendRes.headers.get('etag');
if (etag) {
responseHeaders.set('ETag', etag);
}
if (etag) responseHeaders.set('ETag', etag);
const lastModified = backendRes.headers.get('last-modified');
if (lastModified) {
responseHeaders.set('Last-Modified', lastModified);
}
if (lastModified) responseHeaders.set('Last-Modified', lastModified);
// Set aggressive caching for static media (1 year with immutable)
responseHeaders.set('Cache-Control', 'public, max-age=31536000, s-maxage=31536000, immutable');
responseHeaders.set('X-Content-Type-Options', 'nosniff');
@@ -104,14 +116,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';