WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

91 lines
2.3 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 { uploadToS3 } from '@/lib/s3'
// POST - 上传LOGO(仅管理员)
export async function POST(request: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session || session.user.role !== 'ADMIN') {
return NextResponse.json(
{ error: '无权限' },
{ status: 403 }
)
}
// 将NextRequest转换为Node.js IncomingMessage
const formData = await request.formData()
const logoFile = formData.get('logo') as File
const logoType = formData.get('type') as string // 'header' or 'login'
if (!logoFile) {
return NextResponse.json(
{ error: '请选择LOGO文件' },
{ status: 400 }
)
}
if (!logoType || !['header', 'login'].includes(logoType)) {
return NextResponse.json(
{ error: '请指定LOGO类型(header或login)' },
{ status: 400 }
)
}
// 验证文件类型
if (!logoFile.type.startsWith('image/')) {
return NextResponse.json(
{ error: '只支持图片格式' },
{ status: 400 }
)
}
// 转换File为Buffer
const bytes = await logoFile.arrayBuffer()
const buffer = Buffer.from(bytes)
// 生成文件名
const timestamp = Date.now()
const ext = logoFile.name.split('.').pop()
const filename = `${logoType}-logo-${timestamp}.${ext}`
// 上传到S3
const s3Url = await uploadToS3(buffer, filename, logoFile.type)
// 更新系统设置
let settings = await prisma.systemSettings.findFirst()
const fieldName = logoType === 'header' ? 'headerLogoUrl' : 'loginLogoUrl'
if (!settings) {
settings = await prisma.systemSettings.create({
data: {
[fieldName]: s3Url
}
})
} else {
settings = await prisma.systemSettings.update({
where: { id: settings.id },
data: {
[fieldName]: s3Url
}
})
}
return NextResponse.json({
success: true,
logoUrl: s3Url,
type: logoType
})
} catch (error) {
console.error('Upload logo error:', error)
return NextResponse.json(
{ error: '上传LOGO失败' },
{ status: 500 }
)
}
}