🐛 fix(frontend): 修复上传进度与取件详情弹窗
This commit is contained in:
1 parent
da54bf40c9
commit
988ae2429d
5 files changed
+43
-124
No files matched your search
@@ -3,7 +3,7 @@ import { waitForUploadCompletion, isTransientCompletionError } from './upload-co
|
||||
import type { ApiResponse, ChunkUploadInitResponse, FileUploadResponse, UploadProgress } from '@/types'
|
||||
import { calculateFileHash } from '@/utils/file-processing'
|
||||
|
||||
const CHUNK_SIZE = 5 * 1024 * 1024
|
||||
export const CHUNK_SIZE = 100 * 1024 * 1024
|
||||
|
||||
type ChunkedUploadOptions = {
|
||||
expireValue: number
|
||||
@@ -26,6 +26,7 @@ const pendingCompletion = new WeakMap<File, { uploadId: string; expireValue: num
|
||||
|
||||
async function finishUpload(file: File, options: ChunkedUploadOptions): Promise<ApiResponse<ChunkedUploadResult>> {
|
||||
const session = pendingCompletion.get(file)!
|
||||
options.onProgress?.({loaded:file.size,total:file.size,percentage:99})
|
||||
options.onFinalizing?.()
|
||||
try {
|
||||
const response = await waitForUploadCompletion(() => FileService.completeChunkUpload(session.uploadId, {
|
||||
@@ -55,7 +56,6 @@ export const uploadChunkedFile = async (
|
||||
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,
|
||||
@@ -77,13 +77,16 @@ export const uploadChunkedFile = async (
|
||||
throw new Error(options.messages?.initFailed || 'Init chunk upload failed')
|
||||
}
|
||||
|
||||
const chunkSize = initDetail.chunk_size || CHUNK_SIZE
|
||||
const chunks = Math.ceil(file.size / chunkSize)
|
||||
const uploadedChunks = new Set(initDetail.uploaded_chunks || [])
|
||||
options.onProgress?.({loaded:calculateCompletedBytes(uploadedChunks,chunkSize,file.size),total:file.size,percentage:0})
|
||||
for (let index = 0; index < chunks; index++) {
|
||||
if (uploadedChunks.has(index)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const start = index * CHUNK_SIZE
|
||||
const start = index * chunkSize
|
||||
const end = Math.min(start + CHUNK_SIZE, file.size)
|
||||
const chunk = file.slice(start, end)
|
||||
const chunkResponse = await FileService.uploadChunk(
|
||||
@@ -91,7 +94,7 @@ export const uploadChunkedFile = async (
|
||||
index,
|
||||
new Blob([chunk], { type: file.type }),
|
||||
(progress) => {
|
||||
const completedBytes = calculateCompletedBytes(uploadedChunks, CHUNK_SIZE, file.size)
|
||||
const completedBytes = calculateCompletedBytes(uploadedChunks, chunkSize, file.size)
|
||||
const percentage = Math.round(((completedBytes + progress.loaded) * 100) / file.size)
|
||||
options.onProgress?.({
|
||||
loaded: completedBytes + progress.loaded,
|
||||
|
||||
Reference in new issue
Block a user