- 创建统一的下载错误处理模块 (lib/download-errors.ts) - 实现13种错误类型分类和中文错误消息 - 后端统一使用 createDownloadError 创建标准化错误响应 - 前端统一使用 parseDownloadError 解析错误并显示 - 修复正式系统数据库Schema,添加产品全案相关字段和表 - 执行数据库迁移:添加 task_no, task_status, task_fetched_at 字段 - 创建 material_assets, api_keys, api_call_logs 表
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/lib/auth';
|
|
import { writeFile, mkdir } from 'fs/promises';
|
|
import path from 'path';
|
|
|
|
// POST - 上传 PSD 文件到本地
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session) {
|
|
return NextResponse.json({ error: '请先登录' }, { status: 401 });
|
|
}
|
|
|
|
const formData = await request.formData();
|
|
const file = formData.get('file') as File;
|
|
const materialId = formData.get('materialId') as string;
|
|
|
|
if (!file || !materialId) {
|
|
return NextResponse.json({ error: '缺少文件或素材ID' }, { status: 400 });
|
|
}
|
|
|
|
// 生成本地存储路径
|
|
const timestamp = Date.now();
|
|
const ext = file.name.split('.').pop();
|
|
const fileName = `psd-${timestamp}.${ext}`;
|
|
const uploadDir = path.join(process.cwd(), 'uploads', 'product-package', materialId, 'psd');
|
|
const filePath = path.join(uploadDir, fileName);
|
|
|
|
// 确保目录存在
|
|
await mkdir(uploadDir, { recursive: true });
|
|
|
|
// 保存文件
|
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
await writeFile(filePath, buffer);
|
|
|
|
// 返回相对路径
|
|
const relativePath = path.join('uploads', 'product-package', materialId, 'psd', fileName);
|
|
|
|
return NextResponse.json({ path: relativePath });
|
|
} catch (error) {
|
|
console.error('[PSD Upload] Error:', error);
|
|
return NextResponse.json(
|
|
{ error: '上传失败: ' + (error instanceof Error ? error.message : '未知错误') },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|