✨ feat(frontend): 新增上传中断并联动历史回收状态
This commit is contained in:
1 parent
a962f7aec7
commit
e7918e9317
20 files changed
+275
-71
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { FileService, uploadChunkedFile } from '@/services'
|
||||
import { FileService, PresignUploadService, uploadChunkedFile, UploadCancellation, UploadInterrupted, isUploadInterrupted } from '@/services'
|
||||
import type { AlertType, ApiResponse, ExpireStyle, UploadProgress } from '@/types'
|
||||
import { calculateFileHash, packFilesAsZip } from '@/utils/file-processing'
|
||||
import { usePresignedUpload } from './usePresignedUpload'
|
||||
@@ -33,6 +33,10 @@ type SubmitTextOptions = {
|
||||
}
|
||||
|
||||
export function useSendSubmit(options: UseSendSubmitOptions) {
|
||||
let active: UploadCancellation | undefined
|
||||
const requestCancel = () => active?.request()
|
||||
const needsCleanup = () => Boolean(active?.signal.aborted && active.needsCleanup)
|
||||
const retryCleanup = async () => active?.clean()
|
||||
const { uploadFile: presignUploadFile, reset: resetPresignUpload } = usePresignedUpload({
|
||||
getMaxFileSize: options.getMaxFileSize,
|
||||
notify: options.notify
|
||||
@@ -46,6 +50,7 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
||||
return uploadChunkedFile(file, {
|
||||
expireValue,
|
||||
expireStyle,
|
||||
cancellation: active,
|
||||
onHashCalculated: options.onHashCalculated,
|
||||
onFinalizing: options.onFinalizing,
|
||||
onProgress: (progress: UploadProgress) => {
|
||||
@@ -67,6 +72,8 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
||||
const code = await presignUploadFile(file, {
|
||||
expireValue,
|
||||
expireStyle: expireStyle as ExpireStyle,
|
||||
signal: active?.signal,
|
||||
onSession: (uploadId) => active?.register(() => PresignUploadService.cancelUpload(uploadId)),
|
||||
onProgress: (progress) => {
|
||||
options.onProgress(progress)
|
||||
}
|
||||
@@ -93,30 +100,45 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
||||
enableChunk,
|
||||
validateFileSize
|
||||
}: SubmitFileOptions): Promise<ApiResponse | null> => {
|
||||
if (needsCleanup()) throw new Error('请先重试中断并清理上次上传')
|
||||
const operation = new UploadCancellation()
|
||||
active = operation
|
||||
let fileToUpload = selectedFile
|
||||
try {
|
||||
|
||||
if (selectedFiles.length > 0) {
|
||||
options.notify('正在打包文件...', 'success')
|
||||
fileToUpload = await packFilesAsZip(selectedFiles)
|
||||
if (!validateFileSize(fileToUpload)) {
|
||||
return null
|
||||
if (selectedFiles.length > 0) {
|
||||
options.notify('正在打包文件...', 'success')
|
||||
fileToUpload = await packFilesAsZip(selectedFiles)
|
||||
if (!validateFileSize(fileToUpload)) {
|
||||
return null
|
||||
}
|
||||
options.onHashCalculated(await calculateFileHash(fileToUpload))
|
||||
}
|
||||
options.onHashCalculated(await calculateFileHash(fileToUpload))
|
||||
}
|
||||
|
||||
if (!fileToUpload) {
|
||||
throw new Error(options.translate('send.messages.selectFile'))
|
||||
}
|
||||
if (!fileToUpload) {
|
||||
throw new Error(options.translate('send.messages.selectFile'))
|
||||
}
|
||||
|
||||
return enableChunk
|
||||
? handleChunkUpload(fileToUpload, expireValue, expireStyle)
|
||||
: handlePresignedUpload(fileToUpload, expireValue, expireStyle)
|
||||
operation.check()
|
||||
options.onProgress({ loaded:0,total:fileToUpload.size,percentage:0 })
|
||||
const result = await (enableChunk
|
||||
? handleChunkUpload(fileToUpload, expireValue, expireStyle)
|
||||
: handlePresignedUpload(fileToUpload, expireValue, expireStyle))
|
||||
operation.clear()
|
||||
return result
|
||||
} catch (error) {
|
||||
if (!operation.signal.aborted || isUploadInterrupted(error)) throw error
|
||||
const cleanup = await operation.clean()
|
||||
if (cleanup?.detail?.completed && cleanup.detail.code) return cleanup
|
||||
throw new UploadInterrupted(Boolean(cleanup?.detail?.cleanup_pending))
|
||||
}
|
||||
}
|
||||
|
||||
const submitText = ({ text, expireValue, expireStyle }: SubmitTextOptions) =>
|
||||
FileService.uploadText(text, expireValue, expireStyle)
|
||||
|
||||
return {
|
||||
requestCancel, needsCleanup, retryCleanup, isUploadInterrupted,
|
||||
resetPresignUpload,
|
||||
submitFile,
|
||||
submitText
|
||||
|
||||
Reference in new issue
Block a user