112 lines
3.4 KiB
TypeScript
112 lines
3.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import sharp from 'sharp';
|
|
|
|
const MAX_SIZE = 5 * 1024 * 1024; // 5MB
|
|
const OSS_URL = process.env.OSS_URL || 'http://localhost:9000';
|
|
const API_KEY = process.env.OSS_API_KEY || '7cf93760ea49b750c96e6078b364e5f0';
|
|
|
|
export async function POST(request: NextRequest) {
|
|
let imageBuffer: Buffer;
|
|
|
|
try {
|
|
const formData = await request.formData();
|
|
const file = formData.get('file') as File | null;
|
|
|
|
if (!file) {
|
|
return NextResponse.json({ error: '请选择图片' }, { status: 400 });
|
|
}
|
|
|
|
const allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
|
|
if (!allowedTypes.includes(file.type)) {
|
|
return NextResponse.json({ error: '仅支持 JPG、PNG、GIF、WebP 格式' }, { status: 400 });
|
|
}
|
|
|
|
const buffer = await file.arrayBuffer();
|
|
imageBuffer = Buffer.from(buffer);
|
|
|
|
const image = sharp(imageBuffer);
|
|
const metadata = await image.metadata();
|
|
|
|
if (imageBuffer.length > MAX_SIZE) {
|
|
let quality = 85;
|
|
|
|
if (metadata.width && metadata.width > 1920) {
|
|
imageBuffer = await image
|
|
.resize(1920, null, { withoutEnlargement: true })
|
|
.toBuffer() as any;
|
|
}
|
|
|
|
while (imageBuffer.length > MAX_SIZE && quality > 30) {
|
|
imageBuffer = await sharp(imageBuffer)
|
|
.jpeg({ quality, progressive: true })
|
|
.toBuffer() as any;
|
|
quality -= 10;
|
|
}
|
|
|
|
if (imageBuffer.length > MAX_SIZE && metadata.width && metadata.width > 800) {
|
|
imageBuffer = await sharp(imageBuffer)
|
|
.resize(800, null, { withoutEnlargement: true })
|
|
.jpeg({ quality: 70, progressive: true })
|
|
.toBuffer() as any;
|
|
}
|
|
} else {
|
|
imageBuffer = await sharp(imageBuffer)
|
|
.jpeg({ quality: 85, progressive: true })
|
|
.toBuffer() as any;
|
|
}
|
|
|
|
const formData2 = new FormData();
|
|
const uint8Array = new Uint8Array(imageBuffer);
|
|
const blob = new Blob([uint8Array], { type: 'image/jpeg' });
|
|
formData2.append('file', blob, 'image.jpg');
|
|
|
|
const uploadRes = await fetch(`${OSS_URL}/api/files`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'x-api-key': API_KEY,
|
|
},
|
|
body: formData2,
|
|
});
|
|
|
|
if (!uploadRes.ok) {
|
|
const err = await uploadRes.text();
|
|
console.error('OSS upload failed:', err);
|
|
return NextResponse.json({ error: '上传失败' }, { status: 500 });
|
|
}
|
|
|
|
const fileData = await uploadRes.json();
|
|
|
|
const url = `/api/files/${fileData.fileKey}`;
|
|
return NextResponse.json({ url, fileKey: fileData.fileKey });
|
|
} catch (error) {
|
|
console.error('Upload error:', error);
|
|
return NextResponse.json({ error: '上传失败' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(request: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(request.url);
|
|
const fileKey = searchParams.get('fileKey');
|
|
|
|
if (!fileKey) {
|
|
return NextResponse.json({ error: '缺少文件标识' }, { status: 400 });
|
|
}
|
|
|
|
const res = await fetch(`${OSS_URL}/api/files/${fileKey}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'x-api-key': API_KEY,
|
|
},
|
|
});
|
|
|
|
if (res.ok) {
|
|
return NextResponse.json({ success: true });
|
|
}
|
|
|
|
return NextResponse.json({ error: '删除失败' }, { status: 500 });
|
|
} catch (error) {
|
|
console.error('Delete error:', error);
|
|
return NextResponse.json({ error: '删除失败' }, { status: 500 });
|
|
}
|
|
} |