feat: 添加图片代理接口,保护OSS API key

This commit is contained in:
Cuishibing
2026-04-26 21:57:37 +08:00
parent f847e1c6c6
commit f1b8abf414
2 changed files with 32 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
import { NextRequest, NextResponse } from 'next/server';
const OSS_URL = process.env.OSS_URL || 'http://localhost:9000';
const API_KEY = process.env.OSS_API_KEY || '7cf93760ea49b750c96e6078b364e5f0';
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ fileKey: string }> }
) {
try {
const { fileKey } = await params;
const res = await fetch(`${OSS_URL}/api/files/${fileKey}/preview?key=${API_KEY}`);
if (!res.ok) {
return new NextResponse('File not found', { status: 404 });
}
const imageBuffer = await res.arrayBuffer();
return new NextResponse(imageBuffer, {
headers: {
'Content-Type': 'image/jpeg',
'Cache-Control': 'public, max-age=31536000',
},
});
} catch (error) {
console.error('Proxy error:', error);
return new NextResponse('Error', { status: 500 });
}
}

View File

@@ -76,7 +76,7 @@ export async function POST(request: NextRequest) {
const fileData = await uploadRes.json(); const fileData = await uploadRes.json();
const url = `${OSS_URL}/api/files/${fileData.fileKey}/preview?key=${API_KEY}`; const url = `/api/files/${fileData.fileKey}`;
return NextResponse.json({ url, fileKey: fileData.fileKey }); return NextResponse.json({ url, fileKey: fileData.fileKey });
} catch (error) { } catch (error) {
console.error('Upload error:', error); console.error('Upload error:', error);