✨ feat(frontend): 支持上传后台校验与断线恢复
This commit is contained in:
1 parent
e8c12af834
commit
1ea2fa3c6e
10 files changed
+108
-12
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { FileService } from './file'
|
||||
import { waitForUploadCompletion, isTransientCompletionError } from './upload-completion'
|
||||
import type { ApiResponse, ChunkUploadInitResponse, FileUploadResponse, UploadProgress } from '@/types'
|
||||
import { calculateFileHash } from '@/utils/file-processing'
|
||||
|
||||
@@ -7,6 +8,7 @@ const CHUNK_SIZE = 5 * 1024 * 1024
|
||||
type ChunkedUploadOptions = {
|
||||
expireValue: number
|
||||
expireStyle: string
|
||||
onFinalizing?: () => void
|
||||
onHashCalculated?: (hash: string) => void
|
||||
onProgress?: (progress: UploadProgress) => void
|
||||
messages?: {
|
||||
@@ -18,6 +20,26 @@ type ChunkedUploadOptions = {
|
||||
|
||||
type ChunkedUploadResult = ChunkUploadInitResponse | FileUploadResponse
|
||||
|
||||
// Keep the completion session across a manual retry after a network failure.
|
||||
// The same File object is retained by the send form; no credentials are persisted.
|
||||
const pendingCompletion = new WeakMap<File, { uploadId: string; expireValue: number; expireStyle: string }>()
|
||||
|
||||
async function finishUpload(file: File, options: ChunkedUploadOptions): Promise<ApiResponse<ChunkedUploadResult>> {
|
||||
const session = pendingCompletion.get(file)!
|
||||
options.onFinalizing?.()
|
||||
try {
|
||||
const response = await waitForUploadCompletion(() => FileService.completeChunkUpload(session.uploadId, {
|
||||
expire_value: session.expireValue, expire_style: session.expireStyle
|
||||
}))
|
||||
if (response.code !== 200) throw new Error(options.messages?.completeFailed || 'Complete chunk upload failed')
|
||||
pendingCompletion.delete(file)
|
||||
return response
|
||||
} catch (error) {
|
||||
if (!isTransientCompletionError(error)) pendingCompletion.delete(file)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const calculateCompletedBytes = (uploadedChunks: Set<number>, chunkSize: number, fileSize: number) =>
|
||||
Array.from(uploadedChunks).reduce((total, index) => {
|
||||
const chunkStart = index * chunkSize
|
||||
@@ -29,6 +51,7 @@ export const uploadChunkedFile = async (
|
||||
file: File,
|
||||
options: ChunkedUploadOptions
|
||||
): Promise<ApiResponse<ChunkedUploadResult>> => {
|
||||
if (pendingCompletion.has(file)) return finishUpload(file, options)
|
||||
const fileHash = await calculateFileHash(file)
|
||||
options.onHashCalculated?.(fileHash)
|
||||
|
||||
@@ -84,14 +107,6 @@ export const uploadChunkedFile = async (
|
||||
uploadedChunks.add(index)
|
||||
}
|
||||
|
||||
const completeResponse = await FileService.completeChunkUpload(uploadId, {
|
||||
expire_value: options.expireValue,
|
||||
expire_style: options.expireStyle
|
||||
})
|
||||
|
||||
if (completeResponse.code !== 200) {
|
||||
throw new Error(options.messages?.completeFailed || 'Complete chunk upload failed')
|
||||
}
|
||||
|
||||
return completeResponse
|
||||
pendingCompletion.set(file, { uploadId, expireValue: options.expireValue, expireStyle: options.expireStyle })
|
||||
return finishUpload(file, options)
|
||||
}
|
||||
Reference in new issue
Block a user