补齐预签名和分片上传的初始化、上传、确认、成功与失败状态回调,使用实际上传对象的进度字节避免 ZIP 选中文件计数失真,并在新提交前清理旧错误状态。 - 空提交校验不再进入 uploading 状态,失败保留文件以便重试 - 完成架构检查、类型检查和生产构建验证
104 lines
3.3 KiB
TypeScript
104 lines
3.3 KiB
TypeScript
import { FileService } from './file'
|
|
import type { ApiResponse, ChunkUploadInitResponse, FileUploadResponse, UploadProgress } from '@/types'
|
|
import { calculateFileHash } from '@/utils/file-processing'
|
|
|
|
const CHUNK_SIZE = 5 * 1024 * 1024
|
|
|
|
type ChunkedUploadOptions = {
|
|
expireValue: number
|
|
expireStyle: string
|
|
onHashCalculated?: (hash: string) => void
|
|
onProgress?: (progress: UploadProgress) => void
|
|
messages?: {
|
|
initFailed?: string
|
|
chunkFailed?: (index: number) => string
|
|
completeFailed?: string
|
|
}
|
|
onStatus?: (status: 'initializing' | 'uploading' | 'confirming' | 'success' | 'error') => void
|
|
}
|
|
|
|
type ChunkedUploadResult = ChunkUploadInitResponse | FileUploadResponse
|
|
|
|
const calculateCompletedBytes = (uploadedChunks: Set<number>, chunkSize: number, fileSize: number) =>
|
|
Array.from(uploadedChunks).reduce((total, index) => {
|
|
const chunkStart = index * chunkSize
|
|
const chunkEnd = Math.min((index + 1) * chunkSize, fileSize)
|
|
return total + Math.max(0, chunkEnd - chunkStart)
|
|
}, 0)
|
|
|
|
export const uploadChunkedFile = async (
|
|
file: File,
|
|
options: ChunkedUploadOptions
|
|
): Promise<ApiResponse<ChunkedUploadResult>> => {
|
|
options.onStatus?.('initializing')
|
|
const fileHash = await calculateFileHash(file)
|
|
options.onHashCalculated?.(fileHash)
|
|
|
|
const chunks = Math.ceil(file.size / CHUNK_SIZE)
|
|
const initResponse = await FileService.initChunkUpload({
|
|
file_name: file.name,
|
|
file_size: file.size,
|
|
chunk_size: CHUNK_SIZE,
|
|
file_hash: fileHash
|
|
})
|
|
|
|
if (initResponse.code !== 200) {
|
|
throw new Error(options.messages?.initFailed || 'Init chunk upload failed')
|
|
}
|
|
|
|
if (initResponse.detail?.existed) {
|
|
return initResponse
|
|
}
|
|
|
|
const initDetail = initResponse.detail
|
|
const uploadId = initDetail?.upload_id
|
|
if (!uploadId) {
|
|
throw new Error(options.messages?.initFailed || 'Init chunk upload failed')
|
|
}
|
|
|
|
const uploadedChunks = new Set(initDetail.uploaded_chunks || [])
|
|
options.onStatus?.('uploading')
|
|
for (let index = 0; index < chunks; index++) {
|
|
if (uploadedChunks.has(index)) {
|
|
continue
|
|
}
|
|
|
|
const start = index * CHUNK_SIZE
|
|
const end = Math.min(start + CHUNK_SIZE, file.size)
|
|
const chunk = file.slice(start, end)
|
|
const chunkResponse = await FileService.uploadChunk(
|
|
uploadId,
|
|
index,
|
|
new Blob([chunk], { type: file.type }),
|
|
(progress) => {
|
|
const completedBytes = calculateCompletedBytes(uploadedChunks, CHUNK_SIZE, file.size)
|
|
const percentage = Math.round(((completedBytes + progress.loaded) * 100) / file.size)
|
|
options.onProgress?.({
|
|
loaded: completedBytes + progress.loaded,
|
|
total: file.size,
|
|
percentage: Math.min(percentage, 99)
|
|
})
|
|
}
|
|
)
|
|
|
|
if (chunkResponse.code !== 200) {
|
|
throw new Error(options.messages?.chunkFailed?.(index) || `Chunk upload failed: ${index}`)
|
|
}
|
|
uploadedChunks.add(index)
|
|
}
|
|
|
|
options.onStatus?.('confirming')
|
|
const completeResponse = await FileService.completeChunkUpload(uploadId, {
|
|
expire_value: options.expireValue,
|
|
expire_style: options.expireStyle
|
|
})
|
|
|
|
if (completeResponse.code !== 200) {
|
|
options.onStatus?.('error')
|
|
throw new Error(options.messages?.completeFailed || 'Complete chunk upload failed')
|
|
}
|
|
|
|
options.onStatus?.('success')
|
|
return completeResponse
|
|
}
|