實現得獎資訊與資料庫整合

This commit is contained in:
2025-09-26 01:42:52 +08:00
parent 9bed168238
commit 0675fe63b0
8 changed files with 1531 additions and 409 deletions

View File

@@ -0,0 +1,74 @@
import { NextRequest, NextResponse } from 'next/server'
import { writeFile, mkdir } from 'fs/promises'
import { join } from 'path'
import { existsSync } from 'fs'
export async function POST(request: NextRequest) {
try {
const data = await request.formData()
const file: File | null = data.get('file') as unknown as File
const awardId = data.get('awardId') as string
if (!file) {
return NextResponse.json({ error: '沒有選擇文件' }, { status: 400 })
}
// 驗證文件類型
const allowedTypes = [
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-powerpoint',
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
]
if (!allowedTypes.includes(file.type)) {
return NextResponse.json({
error: '不支持的文件格式,請上傳 PDF、DOC、DOCX、PPT、PPTX 格式的文件'
}, { status: 400 })
}
// 驗證文件大小 (10MB)
const maxSize = 10 * 1024 * 1024
if (file.size > maxSize) {
return NextResponse.json({
error: '文件大小超過 10MB 限制'
}, { status: 400 })
}
// 創建上傳目錄
const uploadDir = join(process.cwd(), 'public', 'uploads', 'documents')
if (!existsSync(uploadDir)) {
await mkdir(uploadDir, { recursive: true })
}
// 生成唯一文件名
const timestamp = Date.now()
const fileExtension = file.name.split('.').pop()
const fileName = `${awardId}_${timestamp}.${fileExtension}`
const filePath = join(uploadDir, fileName)
// 保存文件
const bytes = await file.arrayBuffer()
const buffer = Buffer.from(bytes)
await writeFile(filePath, buffer)
// 返回文件信息
const fileUrl = `/uploads/documents/${fileName}`
return NextResponse.json({
success: true,
url: fileUrl,
fileName: file.name,
size: file.size,
type: file.type
})
} catch (error) {
console.error('文件上傳錯誤:', error)
return NextResponse.json(
{ error: '文件上傳失敗' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,74 @@
import { NextRequest, NextResponse } from 'next/server'
import { writeFile, mkdir } from 'fs/promises'
import { join } from 'path'
import { existsSync } from 'fs'
export async function POST(request: NextRequest) {
try {
const data = await request.formData()
const file: File | null = data.get('file') as unknown as File
const awardId = data.get('awardId') as string
if (!file) {
return NextResponse.json({ error: '沒有選擇照片' }, { status: 400 })
}
// 驗證文件類型
const allowedTypes = [
'image/jpeg',
'image/jpg',
'image/png',
'image/gif',
'image/webp'
]
if (!allowedTypes.includes(file.type)) {
return NextResponse.json({
error: '不支持的照片格式,請上傳 JPG、PNG、GIF、WEBP 格式的照片'
}, { status: 400 })
}
// 驗證文件大小 (5MB)
const maxSize = 5 * 1024 * 1024
if (file.size > maxSize) {
return NextResponse.json({
error: '照片大小超過 5MB 限制'
}, { status: 400 })
}
// 創建上傳目錄
const uploadDir = join(process.cwd(), 'public', 'uploads', 'photos')
if (!existsSync(uploadDir)) {
await mkdir(uploadDir, { recursive: true })
}
// 生成唯一文件名
const timestamp = Date.now()
const fileExtension = file.name.split('.').pop()
const fileName = `${awardId}_${timestamp}.${fileExtension}`
const filePath = join(uploadDir, fileName)
// 保存文件
const bytes = await file.arrayBuffer()
const buffer = Buffer.from(bytes)
await writeFile(filePath, buffer)
// 返回文件信息
const fileUrl = `/uploads/photos/${fileName}`
return NextResponse.json({
success: true,
url: fileUrl,
fileName: file.name,
size: file.size,
type: file.type
})
} catch (error) {
console.error('照片上傳錯誤:', error)
return NextResponse.json(
{ error: '照片上傳失敗' },
{ status: 500 }
)
}
}