feat: resume uploads through SSE worker events
This commit is contained in:
parent
032a2a2898
commit
38f9b93076
1 changed files with 39 additions and 3 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
import { api } from './api'
|
import { api } from './api'
|
||||||
import { deleteUpload, prepareUploadStorage, saveUpload, type StoredUpload } from './uploadStore'
|
import { deleteUpload, prepareUploadStorage, saveUpload, type StoredUpload } from './uploadStore'
|
||||||
|
import { API_BASE_URL, STORAGE_KEYS } from '@/app/constants'
|
||||||
|
import { getStorageItem } from '@/utils/storage'
|
||||||
|
|
||||||
export type UploadTaskStatus = 'pending' | 'uploading' | 'done' | 'error' | 'skipped'
|
export type UploadTaskStatus = 'pending' | 'uploading' | 'done' | 'error' | 'skipped'
|
||||||
|
|
||||||
|
|
@ -102,6 +104,38 @@ const MP_CONCURRENCY = 4
|
||||||
const MP_RETRIES = 2
|
const MP_RETRIES = 2
|
||||||
const MP_RESUME_PREFIX = 'danable:mp:'
|
const MP_RESUME_PREFIX = 'danable:mp:'
|
||||||
|
|
||||||
|
async function waitForProcessedItem(projectId: string, jobId: string, itemId: string): Promise<string> {
|
||||||
|
const token = getStorageItem<string | null>(STORAGE_KEYS.ACCESS_TOKEN, null)
|
||||||
|
const response = await fetch(`${API_BASE_URL}/projects/${projectId}/upload-jobs/${jobId}/events`, {
|
||||||
|
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||||
|
})
|
||||||
|
if (!response.ok || !response.body) throw new Error(`Không kết nối được SSE (${response.status})`)
|
||||||
|
const reader = response.body.getReader()
|
||||||
|
const decoder = new TextDecoder()
|
||||||
|
let buffer = ''
|
||||||
|
try {
|
||||||
|
for (;;) {
|
||||||
|
const chunk = await reader.read()
|
||||||
|
if (chunk.done) break
|
||||||
|
buffer += decoder.decode(chunk.value, { stream: true })
|
||||||
|
const events = buffer.split('\n\n')
|
||||||
|
buffer = events.pop() ?? ''
|
||||||
|
for (const event of events) {
|
||||||
|
if (!event.includes('data:')) continue
|
||||||
|
const { data } = await api.get<{ items: Array<{ id: string; status: string; imageId?: string; error?: string }> }>(
|
||||||
|
`/projects/${projectId}/upload-jobs/${jobId}`
|
||||||
|
)
|
||||||
|
const item = data.items.find((candidate) => candidate.id === itemId)
|
||||||
|
if (item?.status === 'done' && item.imageId) return item.imageId
|
||||||
|
if (item?.status === 'error') throw new Error(item.error || 'Xử lý ảnh thất bại')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await reader.cancel()
|
||||||
|
}
|
||||||
|
throw new Error('SSE upload worker đã đóng trước khi xử lý xong')
|
||||||
|
}
|
||||||
|
|
||||||
interface MpResume {
|
interface MpResume {
|
||||||
key: string
|
key: string
|
||||||
uploadId: string
|
uploadId: string
|
||||||
|
|
@ -304,14 +338,15 @@ export async function uploadFilesBatch(
|
||||||
task.progress = r
|
task.progress = r
|
||||||
emit()
|
emit()
|
||||||
})
|
})
|
||||||
const { data: confirmed } = await api.post<{ processedFiles: number; imageId: string }>(
|
await api.post<{ processedFiles: number }>(
|
||||||
`/projects/${projectId}/upload-jobs/${jobId}/items/${slot.id}/complete`,
|
`/projects/${projectId}/upload-jobs/${jobId}/items/${slot.id}/complete`,
|
||||||
{ width: dims.width, height: dims.height }
|
{ width: dims.width, height: dims.height }
|
||||||
)
|
)
|
||||||
|
const imageId = await waitForProcessedItem(projectId, jobId, slot.id)
|
||||||
task.status = 'done'
|
task.status = 'done'
|
||||||
task.progress = 1
|
task.progress = 1
|
||||||
task.imageId = confirmed.imageId
|
task.imageId = imageId
|
||||||
result.done.push({ fileName: file.name, imageId: confirmed.imageId })
|
result.done.push({ fileName: file.name, imageId })
|
||||||
ok = true
|
ok = true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
task.error = err instanceof Error ? err.message : 'Tải lên thất bại'
|
task.error = err instanceof Error ? err.message : 'Tải lên thất bại'
|
||||||
|
|
@ -357,6 +392,7 @@ export async function resumeStoredUpload(
|
||||||
`/projects/${stored.projectId}/upload-jobs/${stored.jobId}/items/${item.id}/complete`,
|
`/projects/${stored.projectId}/upload-jobs/${stored.jobId}/items/${item.id}/complete`,
|
||||||
{ width: dims.width, height: dims.height }
|
{ width: dims.width, height: dims.height }
|
||||||
)
|
)
|
||||||
|
await waitForProcessedItem(stored.projectId, stored.jobId, item.id)
|
||||||
done += 1
|
done += 1
|
||||||
onProgress?.(done, job.totalFiles)
|
onProgress?.(done, job.totalFiles)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue