64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { storeFile } from '@/lib/fileStorage'
|
|
import { withCors } from '@/lib/middleware'
|
|
import { createHash } from 'crypto'
|
|
|
|
async function handleVersionUpload(req: NextRequest) {
|
|
try {
|
|
const formData = await req.formData()
|
|
const file = formData.get('file') as File
|
|
const version = formData.get('version') as string
|
|
|
|
if (!file || !version) {
|
|
return NextResponse.json({ error: '缺少文件或版本号' }, { status: 400 })
|
|
}
|
|
|
|
const buffer = Buffer.from(await file.arrayBuffer())
|
|
|
|
// Calculate SHA-256
|
|
const hash = createHash('sha256')
|
|
hash.update(buffer)
|
|
const checksum = hash.digest('hex')
|
|
|
|
// Store file
|
|
const filename = `winupdate-${version}-${Date.now()}.exe`
|
|
const storedFile = await storeFile(buffer, filename, 'application/x-msdownload', 'version')
|
|
|
|
// Update all versions to not be latest
|
|
await prisma.version.updateMany({
|
|
data: { isLatest: false }
|
|
})
|
|
|
|
// Create new version record
|
|
const newVersion = await prisma.version.create({
|
|
data: {
|
|
version,
|
|
fileId: storedFile.id,
|
|
objectName: storedFile.objectName,
|
|
filename: storedFile.filename,
|
|
contentType: storedFile.contentType,
|
|
fileSize: storedFile.size,
|
|
checksum,
|
|
isLatest: true
|
|
}
|
|
})
|
|
|
|
const protocol = req.headers.get('x-forwarded-proto') || 'http'
|
|
const host = req.headers.get('host')
|
|
const downloadUrl = `${protocol}://${host}/api/downloads/${storedFile.id}`
|
|
|
|
return NextResponse.json({
|
|
version,
|
|
download_url: downloadUrl,
|
|
checksum
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('上传版本失败:', error)
|
|
return NextResponse.json({ error: '上传失败' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export const POST = withCors(handleVersionUpload)
|