2026-05-07 16:37:22 +08:00

79 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
import { writeFile, mkdir } from 'fs/promises'
import path from 'path'
import { existsSync } from 'fs'
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'
export const maxDuration = 10800
// SKU文件上传API
export async function POST(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const session = await getServerSession(authOptions)
if (!session?.user || !(session.user as any).id) {
return NextResponse.json({ error: '请先登录' }, { status: 401 })
}
const materialId = params.id
// 检查素材是否存在且是标准模型类型
const material = await prisma.material.findUnique({
where: { id: materialId },
select: { id: true, materialType: true, userId: true }
})
if (!material) {
return NextResponse.json({ error: '素材不存在' }, { status: 404 })
}
if (material.materialType !== '标准模型') {
return NextResponse.json({ error: '只有标准模型类型才能上传 SKU' }, { status: 400 })
}
const formData = await request.formData()
const skuName = formData.get('skuName') as string
const file = formData.get('file') as File
if (!skuName || !file) {
return NextResponse.json({ error: '缺少必填字段' }, { status: 400 })
}
// 创建上传目录
const uploadDir = path.join(process.cwd(), 'uploads', 'skus', materialId)
if (!existsSync(uploadDir)) {
await mkdir(uploadDir, { recursive: true })
}
// 生成文件名
const timestamp = Date.now()
const ext = path.extname(file.name)
const fileName = `${skuName}_${timestamp}${ext}`
const filePath = path.join(uploadDir, fileName)
// 保存文件
const bytes = await file.arrayBuffer()
const buffer = Buffer.from(bytes)
await writeFile(filePath, buffer)
// 相对路径(用于数据库存储)
const relativePath = path.join('skus', materialId, fileName).replace(/\\/g, '/') // 统一使用正斜杠
// 只返回文件信息,不创建SKU记录(SKU记录由 /api/materials/[id]/skus POST 接口创建)
return NextResponse.json({
fileName: file.name,
filePath: relativePath,
fileSize: file.size
}, { status: 200 })
} catch (error) {
console.error('上传 SKU 失败:', error)
return NextResponse.json({ error: '上传 SKU 失败' }, { status: 500 })
}
}