feat: 添加图片代理接口,保护OSS API key
This commit is contained in:
31
app/api/files/[fileKey]/route.ts
Normal file
31
app/api/files/[fileKey]/route.ts
Normal 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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user