diff --git a/app/api/files/[fileKey]/route.ts b/app/api/files/[fileKey]/route.ts new file mode 100644 index 0000000..3fccb58 --- /dev/null +++ b/app/api/files/[fileKey]/route.ts @@ -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 }); + } +} \ No newline at end of file diff --git a/app/api/upload/route.ts b/app/api/upload/route.ts index ea9aab0..1da81a3 100644 --- a/app/api/upload/route.ts +++ b/app/api/upload/route.ts @@ -76,7 +76,7 @@ export async function POST(request: NextRequest) { 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 }); } catch (error) { console.error('Upload error:', error);