🐛 修复上传阶段状态与重试进度
补齐预签名和分片上传的初始化、上传、确认、成功与失败状态回调,使用实际上传对象的进度字节避免 ZIP 选中文件计数失真,并在新提交前清理旧错误状态。 - 空提交校验不再进入 uploading 状态,失败保留文件以便重试 - 完成架构检查、类型检查和生产构建验证
This commit is contained in:
1 parent
84ddf92b20
commit
43ebc8c1b2
4 files changed
+32
-14
No files matched your search
@@ -37,6 +37,7 @@ type PresignedUploadNotifier = (message: string, type: AlertType) => void
|
|||||||
type UsePresignedUploadOptions = {
|
type UsePresignedUploadOptions = {
|
||||||
getMaxFileSize?: () => number
|
getMaxFileSize?: () => number
|
||||||
notify?: PresignedUploadNotifier
|
notify?: PresignedUploadNotifier
|
||||||
|
onStatus?: (status: PresignUploadStatus) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,6 +67,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
|
|||||||
const notify: PresignedUploadNotifier = (message, type) => {
|
const notify: PresignedUploadNotifier = (message, type) => {
|
||||||
options.notify?.(message, type)
|
options.notify?.(message, type)
|
||||||
}
|
}
|
||||||
|
const onStatus = options.onStatus
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件大小验证
|
* 文件大小验证
|
||||||
@@ -110,6 +112,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
|
|||||||
*/
|
*/
|
||||||
const handleUploadError = (error: unknown): void => {
|
const handleUploadError = (error: unknown): void => {
|
||||||
presignStatus.value = PRESIGN_UPLOAD_STATUS.ERROR
|
presignStatus.value = PRESIGN_UPLOAD_STATUS.ERROR
|
||||||
|
onStatus?.(PRESIGN_UPLOAD_STATUS.ERROR)
|
||||||
const status = (error as ErrorWithResponse)?.response?.status
|
const status = (error as ErrorWithResponse)?.response?.status
|
||||||
const fallback =
|
const fallback =
|
||||||
status === 404
|
status === 404
|
||||||
@@ -131,6 +134,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
|
|||||||
): Promise<PresignInitResponse | null> => {
|
): Promise<PresignInitResponse | null> => {
|
||||||
try {
|
try {
|
||||||
presignStatus.value = PRESIGN_UPLOAD_STATUS.INITIALIZING
|
presignStatus.value = PRESIGN_UPLOAD_STATUS.INITIALIZING
|
||||||
|
onStatus?.(PRESIGN_UPLOAD_STATUS.INITIALIZING)
|
||||||
|
|
||||||
const response = await PresignUploadService.initUpload({
|
const response = await PresignUploadService.initUpload({
|
||||||
file_name: file.name,
|
file_name: file.name,
|
||||||
@@ -161,6 +165,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
|
|||||||
): Promise<string | null> => {
|
): Promise<string | null> => {
|
||||||
try {
|
try {
|
||||||
presignStatus.value = PRESIGN_UPLOAD_STATUS.UPLOADING
|
presignStatus.value = PRESIGN_UPLOAD_STATUS.UPLOADING
|
||||||
|
onStatus?.(PRESIGN_UPLOAD_STATUS.UPLOADING)
|
||||||
|
|
||||||
// 上传到 S3
|
// 上传到 S3
|
||||||
const progressHandler = createProgressHandler(options?.onProgress)
|
const progressHandler = createProgressHandler(options?.onProgress)
|
||||||
@@ -176,6 +181,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
|
|||||||
|
|
||||||
// 确认上传
|
// 确认上传
|
||||||
presignStatus.value = PRESIGN_UPLOAD_STATUS.CONFIRMING
|
presignStatus.value = PRESIGN_UPLOAD_STATUS.CONFIRMING
|
||||||
|
onStatus?.(PRESIGN_UPLOAD_STATUS.CONFIRMING)
|
||||||
const confirmResponse = await PresignUploadService.confirmUpload(session.upload_id, {
|
const confirmResponse = await PresignUploadService.confirmUpload(session.upload_id, {
|
||||||
expire_value: options?.expireValue ?? DEFAULT_EXPIRE_VALUE,
|
expire_value: options?.expireValue ?? DEFAULT_EXPIRE_VALUE,
|
||||||
expire_style: options?.expireStyle ?? DEFAULT_EXPIRE_STYLE
|
expire_style: options?.expireStyle ?? DEFAULT_EXPIRE_STYLE
|
||||||
@@ -183,6 +189,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
|
|||||||
|
|
||||||
if (confirmResponse.code === 200 && confirmResponse.detail?.code) {
|
if (confirmResponse.code === 200 && confirmResponse.detail?.code) {
|
||||||
presignStatus.value = PRESIGN_UPLOAD_STATUS.SUCCESS
|
presignStatus.value = PRESIGN_UPLOAD_STATUS.SUCCESS
|
||||||
|
onStatus?.(PRESIGN_UPLOAD_STATUS.SUCCESS)
|
||||||
uploadedCode.value = String(confirmResponse.detail.code)
|
uploadedCode.value = String(confirmResponse.detail.code)
|
||||||
// 确保进度为 100%
|
// 确保进度为 100%
|
||||||
uploadProgress.value = {
|
uploadProgress.value = {
|
||||||
@@ -211,6 +218,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
|
|||||||
): Promise<string | null> => {
|
): Promise<string | null> => {
|
||||||
try {
|
try {
|
||||||
presignStatus.value = PRESIGN_UPLOAD_STATUS.UPLOADING
|
presignStatus.value = PRESIGN_UPLOAD_STATUS.UPLOADING
|
||||||
|
onStatus?.(PRESIGN_UPLOAD_STATUS.UPLOADING)
|
||||||
|
|
||||||
const progressHandler = createProgressHandler(options?.onProgress)
|
const progressHandler = createProgressHandler(options?.onProgress)
|
||||||
const uploadUrl =
|
const uploadUrl =
|
||||||
@@ -224,6 +232,7 @@ export function usePresignedUpload(options: UsePresignedUploadOptions = {}) {
|
|||||||
|
|
||||||
if (response.code === 200 && response.detail?.code) {
|
if (response.code === 200 && response.detail?.code) {
|
||||||
presignStatus.value = PRESIGN_UPLOAD_STATUS.SUCCESS
|
presignStatus.value = PRESIGN_UPLOAD_STATUS.SUCCESS
|
||||||
|
onStatus?.(PRESIGN_UPLOAD_STATUS.SUCCESS)
|
||||||
uploadedCode.value = String(response.detail.code)
|
uploadedCode.value = String(response.detail.code)
|
||||||
// 确保进度为 100%
|
// 确保进度为 100%
|
||||||
uploadProgress.value = {
|
uploadProgress.value = {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export function useSendFlow() {
|
|||||||
const selectedRecord = ref<SentFileRecord | null>(null)
|
const selectedRecord = ref<SentFileRecord | null>(null)
|
||||||
const isSubmitting = ref(false)
|
const isSubmitting = ref(false)
|
||||||
const fileHash = ref('')
|
const fileHash = ref('')
|
||||||
const uploadStatus = ref<'idle' | 'uploading' | 'success' | 'error'>('idle')
|
const uploadStatus = ref<'idle' | 'initializing' | 'uploading' | 'confirming' | 'success' | 'error'>('idle')
|
||||||
const uploadedBytes = ref(0)
|
const uploadedBytes = ref(0)
|
||||||
const totalBytes = ref(0)
|
const totalBytes = ref(0)
|
||||||
const uploadError = ref('')
|
const uploadError = ref('')
|
||||||
@@ -65,10 +65,9 @@ export function useSendFlow() {
|
|||||||
notify: (message, type) => alertStore.showAlert(message, type),
|
notify: (message, type) => alertStore.showAlert(message, type),
|
||||||
translate: t,
|
translate: t,
|
||||||
onProgress: (progress) => {
|
onProgress: (progress) => {
|
||||||
uploadProgress.value = progress
|
uploadProgress.value = progress.percentage
|
||||||
const total = selectedFile.value?.size || selectedFiles.value.reduce((sum, file) => sum + file.size, 0)
|
totalBytes.value = progress.total
|
||||||
totalBytes.value = total
|
uploadedBytes.value = progress.loaded
|
||||||
uploadedBytes.value = Math.round((progress / 100) * total)
|
|
||||||
},
|
},
|
||||||
onStatus: (status) => { uploadStatus.value = status },
|
onStatus: (status) => { uploadStatus.value = status },
|
||||||
onHashCalculated: (hash) => {
|
onHashCalculated: (hash) => {
|
||||||
@@ -215,7 +214,7 @@ export function useSendFlow() {
|
|||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
if (isSubmitting.value) return
|
if (isSubmitting.value) return
|
||||||
isSubmitting.value = true
|
isSubmitting.value = true
|
||||||
uploadStatus.value = 'uploading'
|
uploadError.value = ''
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (sendType.value === 'file' && !selectedFile.value && selectedFiles.value.length === 0) {
|
if (sendType.value === 'file' && !selectedFile.value && selectedFiles.value.length === 0) {
|
||||||
@@ -297,6 +296,10 @@ export function useSendFlow() {
|
|||||||
alertStore.showAlert(getErrorMessage(error, t('send.messages.sendFailed')), 'error')
|
alertStore.showAlert(getErrorMessage(error, t('send.messages.sendFailed')), 'error')
|
||||||
} finally {
|
} finally {
|
||||||
uploadProgress.value = 0
|
uploadProgress.value = 0
|
||||||
|
if (uploadStatus.value !== 'success' && uploadStatus.value !== 'error') {
|
||||||
|
uploadStatus.value = 'idle'
|
||||||
|
}
|
||||||
|
if (uploadStatus.value === 'success') uploadStatus.value = 'idle'
|
||||||
isSubmitting.value = false
|
isSubmitting.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ type UseSendSubmitOptions = {
|
|||||||
getMaxFileSize: () => number
|
getMaxFileSize: () => number
|
||||||
notify: (message: string, type: AlertType) => void
|
notify: (message: string, type: AlertType) => void
|
||||||
translate: Translate
|
translate: Translate
|
||||||
onProgress: (progress: number) => void
|
onProgress: (progress: UploadProgress) => void
|
||||||
onHashCalculated: (hash: string) => void
|
onHashCalculated: (hash: string) => void
|
||||||
onStatus?: (status: 'idle' | 'uploading' | 'success' | 'error') => void
|
onStatus?: (status: 'idle' | 'initializing' | 'uploading' | 'confirming' | 'success' | 'error') => void
|
||||||
}
|
}
|
||||||
|
|
||||||
type SubmitFileOptions = {
|
type SubmitFileOptions = {
|
||||||
@@ -35,7 +35,8 @@ type SubmitTextOptions = {
|
|||||||
export function useSendSubmit(options: UseSendSubmitOptions) {
|
export function useSendSubmit(options: UseSendSubmitOptions) {
|
||||||
const { uploadFile: presignUploadFile, reset: resetPresignUpload } = usePresignedUpload({
|
const { uploadFile: presignUploadFile, reset: resetPresignUpload } = usePresignedUpload({
|
||||||
getMaxFileSize: options.getMaxFileSize,
|
getMaxFileSize: options.getMaxFileSize,
|
||||||
notify: options.notify
|
notify: options.notify,
|
||||||
|
onStatus: options.onStatus
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleChunkUpload = async (
|
const handleChunkUpload = async (
|
||||||
@@ -47,9 +48,8 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
|||||||
expireValue,
|
expireValue,
|
||||||
expireStyle,
|
expireStyle,
|
||||||
onHashCalculated: options.onHashCalculated,
|
onHashCalculated: options.onHashCalculated,
|
||||||
onProgress: (progress: UploadProgress) => {
|
onProgress: options.onProgress,
|
||||||
options.onProgress(progress.percentage)
|
onStatus: options.onStatus,
|
||||||
},
|
|
||||||
messages: {
|
messages: {
|
||||||
initFailed: options.translate('send.messages.initChunkUploadFailed'),
|
initFailed: options.translate('send.messages.initChunkUploadFailed'),
|
||||||
chunkFailed: (index) => options.translate('send.messages.chunkUploadFailed', { index }),
|
chunkFailed: (index) => options.translate('send.messages.chunkUploadFailed', { index }),
|
||||||
@@ -67,7 +67,7 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
|||||||
expireValue,
|
expireValue,
|
||||||
expireStyle: expireStyle as ExpireStyle,
|
expireStyle: expireStyle as ExpireStyle,
|
||||||
onProgress: (progress) => {
|
onProgress: (progress) => {
|
||||||
options.onProgress(progress.percentage)
|
options.onProgress(progress)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -107,7 +107,7 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
|||||||
throw new Error(options.translate('send.messages.selectFile'))
|
throw new Error(options.translate('send.messages.selectFile'))
|
||||||
}
|
}
|
||||||
|
|
||||||
options.onStatus?.('uploading')
|
|
||||||
try {
|
try {
|
||||||
const response = enableChunk
|
const response = enableChunk
|
||||||
? await handleChunkUpload(fileToUpload, expireValue, expireStyle)
|
? await handleChunkUpload(fileToUpload, expireValue, expireStyle)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ type ChunkedUploadOptions = {
|
|||||||
chunkFailed?: (index: number) => string
|
chunkFailed?: (index: number) => string
|
||||||
completeFailed?: string
|
completeFailed?: string
|
||||||
}
|
}
|
||||||
|
onStatus?: (status: 'initializing' | 'uploading' | 'confirming' | 'success' | 'error') => void
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChunkedUploadResult = ChunkUploadInitResponse | FileUploadResponse
|
type ChunkedUploadResult = ChunkUploadInitResponse | FileUploadResponse
|
||||||
@@ -29,6 +30,7 @@ export const uploadChunkedFile = async (
|
|||||||
file: File,
|
file: File,
|
||||||
options: ChunkedUploadOptions
|
options: ChunkedUploadOptions
|
||||||
): Promise<ApiResponse<ChunkedUploadResult>> => {
|
): Promise<ApiResponse<ChunkedUploadResult>> => {
|
||||||
|
options.onStatus?.('initializing')
|
||||||
const fileHash = await calculateFileHash(file)
|
const fileHash = await calculateFileHash(file)
|
||||||
options.onHashCalculated?.(fileHash)
|
options.onHashCalculated?.(fileHash)
|
||||||
|
|
||||||
@@ -55,6 +57,7 @@ export const uploadChunkedFile = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const uploadedChunks = new Set(initDetail.uploaded_chunks || [])
|
const uploadedChunks = new Set(initDetail.uploaded_chunks || [])
|
||||||
|
options.onStatus?.('uploading')
|
||||||
for (let index = 0; index < chunks; index++) {
|
for (let index = 0; index < chunks; index++) {
|
||||||
if (uploadedChunks.has(index)) {
|
if (uploadedChunks.has(index)) {
|
||||||
continue
|
continue
|
||||||
@@ -84,14 +87,17 @@ export const uploadChunkedFile = async (
|
|||||||
uploadedChunks.add(index)
|
uploadedChunks.add(index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options.onStatus?.('confirming')
|
||||||
const completeResponse = await FileService.completeChunkUpload(uploadId, {
|
const completeResponse = await FileService.completeChunkUpload(uploadId, {
|
||||||
expire_value: options.expireValue,
|
expire_value: options.expireValue,
|
||||||
expire_style: options.expireStyle
|
expire_style: options.expireStyle
|
||||||
})
|
})
|
||||||
|
|
||||||
if (completeResponse.code !== 200) {
|
if (completeResponse.code !== 200) {
|
||||||
|
options.onStatus?.('error')
|
||||||
throw new Error(options.messages?.completeFailed || 'Complete chunk upload failed')
|
throw new Error(options.messages?.completeFailed || 'Complete chunk upload failed')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options.onStatus?.('success')
|
||||||
return completeResponse
|
return completeResponse
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user