feat: add API proxy for MinIO files - fixes file access in production
This commit is contained in:
60
src/app/api/files/[...path]/route.ts
Normal file
60
src/app/api/files/[...path]/route.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
// Get internal MinIO URL for server-side fetching
|
||||
function getInternalFileUrl(fileUrl: string): string {
|
||||
try {
|
||||
const url = new URL(fileUrl);
|
||||
const pathParts = url.pathname.split('/').filter(Boolean);
|
||||
if (pathParts.length >= 2) {
|
||||
const bucket = pathParts[0];
|
||||
const fileName = pathParts.slice(1).join('/');
|
||||
const minioEndpoint = process.env.MINIO_ENDPOINT || 'minio';
|
||||
const minioPort = process.env.MINIO_PORT || '9000';
|
||||
return `http://${minioEndpoint}:${minioPort}/${bucket}/${fileName}`;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error parsing file URL:", e);
|
||||
}
|
||||
return fileUrl;
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ path: string[] }> }
|
||||
) {
|
||||
try {
|
||||
const { path } = await params;
|
||||
|
||||
if (!path || path.length < 2) {
|
||||
return new NextResponse("Invalid path", { status: 400 });
|
||||
}
|
||||
|
||||
const bucket = path[0];
|
||||
const fileName = path.slice(1).join('/');
|
||||
|
||||
const minioEndpoint = process.env.MINIO_ENDPOINT || 'minio';
|
||||
const minioPort = process.env.MINIO_PORT || '9000';
|
||||
const internalUrl = `http://${minioEndpoint}:${minioPort}/${bucket}/${fileName}`;
|
||||
|
||||
const fileResponse = await fetch(internalUrl);
|
||||
if (!fileResponse.ok) {
|
||||
console.error("Failed to fetch file from MinIO:", internalUrl, fileResponse.status);
|
||||
return new NextResponse("File not found", { status: 404 });
|
||||
}
|
||||
|
||||
const contentType = fileResponse.headers.get('content-type') || 'application/octet-stream';
|
||||
const fileBuffer = await fileResponse.arrayBuffer();
|
||||
|
||||
return new NextResponse(fileBuffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Cache-Control': 'public, max-age=31536000, immutable',
|
||||
},
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error serving file:", error);
|
||||
return new NextResponse("Internal server error", { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user