feat: 支持批量上传关卡
This commit is contained in:
@@ -5,8 +5,7 @@ import { Button } from '@/components/ui/button'
|
||||
import { X, Image as ImageIcon } from 'lucide-react'
|
||||
import Image from 'next/image'
|
||||
import { Spinner } from '@/components/ui/spinner'
|
||||
import COS from 'cos-js-sdk-v5'
|
||||
import { apiFetch } from '@/lib/api'
|
||||
import { uploadToCos } from '@/lib/cos-client'
|
||||
|
||||
interface ImageUploaderProps {
|
||||
value: string
|
||||
@@ -38,52 +37,7 @@ export function ImageUploader({ value, onChange }: ImageUploaderProps) {
|
||||
setIsUploading(true)
|
||||
|
||||
try {
|
||||
// Get temp key
|
||||
const keyRes = await apiFetch('/api/cos/temp-key')
|
||||
if (!keyRes.ok) {
|
||||
throw new Error('获取上传凭证失败')
|
||||
}
|
||||
const keyData = await keyRes.json()
|
||||
|
||||
// Generate unique filename
|
||||
const ext = file.name.split('.').pop() || 'jpg'
|
||||
const timestamp = Date.now()
|
||||
const randomStr = Math.random().toString(36).substring(2, 8)
|
||||
const filename = `mini_game/images/${timestamp}_${randomStr}.${ext}`
|
||||
|
||||
// Initialize COS with temp credentials
|
||||
const cos = new COS({
|
||||
getAuthorization: (_options, callback) => {
|
||||
callback({
|
||||
TmpSecretId: keyData.credentials.tmpSecretId,
|
||||
TmpSecretKey: keyData.credentials.tmpSecretKey,
|
||||
SecurityToken: keyData.credentials.sessionToken,
|
||||
StartTime: keyData.startTime,
|
||||
ExpiredTime: keyData.expiredTime,
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
// Upload file
|
||||
const uploadUrl = await new Promise<string>((resolve, reject) => {
|
||||
cos.putObject(
|
||||
{
|
||||
Bucket: keyData.bucket,
|
||||
Region: keyData.region,
|
||||
Key: filename,
|
||||
Body: file,
|
||||
},
|
||||
(err, data) => {
|
||||
if (err) {
|
||||
reject(new Error(err.message || '上传失败'))
|
||||
return
|
||||
}
|
||||
const url = `https://${keyData.bucket}.cos.${keyData.region}.myqcloud.com/${filename}`
|
||||
resolve(url)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const uploadUrl = await uploadToCos(file, file.name)
|
||||
onChange(uploadUrl)
|
||||
} catch (err) {
|
||||
console.error('Upload error:', err)
|
||||
|
||||
Reference in New Issue
Block a user