From f1b8abf414499f231bca32420c8c0735939c5a2c Mon Sep 17 00:00:00 2001 From: Cuishibing <643237029@qq.com> Date: Sun, 26 Apr 2026 21:57:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E6=8E=A5=E5=8F=A3=EF=BC=8C=E4=BF=9D=E6=8A=A4?= =?UTF-8?q?OSS=20API=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/files/[fileKey]/route.ts | 31 +++++++++++++++++++++++++++++++ app/api/upload/route.ts | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/api/files/[fileKey]/route.ts 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);