feat: 图片上传优化及审核状态交互改进
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import sharp from 'sharp';
|
||||
|
||||
const UPLOAD_DIR = path.join(process.cwd(), 'public/uploads');
|
||||
const MAX_SIZE = 5 * 1024 * 1024; // 5MB
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -18,16 +20,54 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ error: '仅支持 JPG、PNG、GIF、WebP 格式' }, { status: 400 });
|
||||
}
|
||||
|
||||
const ext = file.name.split('.').pop() || 'jpg';
|
||||
const ext = 'jpg';
|
||||
const filename = `${Date.now()}-${Math.random().toString(36).slice(2)}.${ext}`;
|
||||
const filepath = path.join(UPLOAD_DIR, filename);
|
||||
|
||||
const buffer = await file.arrayBuffer();
|
||||
await fs.writeFile(filepath, Buffer.from(buffer));
|
||||
let imageBuffer = Buffer.from(buffer);
|
||||
|
||||
const host = request.headers.get('host') || 'localhost:3000';
|
||||
const protocol = request.headers.get('x-forwarded-proto') || 'http';
|
||||
const url = `${protocol}://${host}/uploads/${filename}`;
|
||||
// 获取图片尺寸
|
||||
const image = sharp(imageBuffer);
|
||||
const metadata = await image.metadata();
|
||||
|
||||
// 如果超过5MB,进行压缩
|
||||
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;
|
||||
}
|
||||
|
||||
// 循环压缩直到小于5MB
|
||||
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 {
|
||||
// 转换为jpeg并优化
|
||||
imageBuffer = await sharp(imageBuffer)
|
||||
.jpeg({ quality: 85, progressive: true })
|
||||
.toBuffer() as any;
|
||||
}
|
||||
|
||||
await fs.writeFile(filepath, imageBuffer);
|
||||
|
||||
// 返回相对路径
|
||||
const url = `/uploads/${filename}`;
|
||||
return NextResponse.json({ url });
|
||||
} catch (error) {
|
||||
console.error('Upload error:', error);
|
||||
|
||||
Reference in New Issue
Block a user