feat: persist upload batches in IndexedDB

This commit is contained in:
hailt 2026-11-12 22:40:28 +07:00
parent dba9e7ec3b
commit 4e15605ffd
2 changed files with 95 additions and 14 deletions

View file

@ -1,4 +1,5 @@
import { api } from './api'
import { deleteUpload, prepareUploadStorage, saveUpload } from './uploadStore'
export type UploadTaskStatus = 'pending' | 'uploading' | 'done' | 'error' | 'skipped'
@ -17,6 +18,7 @@ export interface BatchUploadResult {
}
interface PresignSlot {
id: string
fileName: string
putUrl: string
r2Key: string
@ -243,18 +245,35 @@ export async function uploadFilesBatch(
})
const emit = () => onUpdate?.(tasks.map((t) => ({ ...t })))
emit()
// One presign call per 100 files instead of one per file.
const slots = new Map<string, PresignSlot>()
const pending = files.filter((f) => tasks.find((t) => t.fileName === f.name)?.status === 'pending')
for (let i = 0; i < pending.length; i += 100) {
const chunk = pending.slice(i, i + 100)
const { data } = await api.post<PresignSlot[]>(`/projects/${projectId}/images/batch-presign`,
chunk.map((f) => ({ fileName: f.name, contentType: f.type || 'image/jpeg' }))
)
data.forEach((s) => slots.set(s.fileName, s))
if (pending.length === 0) {
return { done: [], failed: [], skipped: tasks.map((t) => t.fileName) }
}
await prepareUploadStorage(pending.reduce((sum, file) => sum + file.size, 0))
const { data: created } = await api.post<{
job: { id: string }
items: PresignSlot[]
}>(`/projects/${projectId}/upload-jobs`, {
kind: 'images',
files: pending.map((file) => ({
fileName: file.name,
contentType: file.type || 'image/jpeg',
size: file.size,
})),
})
const jobId = created.job.id
await saveUpload({
id: jobId,
projectId,
jobId,
files: pending,
createdAt: Date.now(),
})
const slots = new Map<string, PresignSlot>()
created.items.forEach((s) => slots.set(s.fileName, s))
const result: BatchUploadResult = { done: [], failed: [], skipped: [] }
tasks.forEach((t) => {
if (t.status === 'skipped') result.skipped.push(t.fileName)
@ -285,14 +304,14 @@ export async function uploadFilesBatch(
task.progress = r
emit()
})
const { data: confirmed } = await api.post<{ id: string }>(
`/projects/${projectId}/images`,
{ fileName: file.name, r2Key: slot.r2Key, width: dims.width, height: dims.height }
const { data: confirmed } = await api.post<{ processedFiles: number; imageId: string }>(
`/projects/${projectId}/upload-jobs/${jobId}/items/${slot.id}/complete`,
{ width: dims.width, height: dims.height }
)
task.status = 'done'
task.progress = 1
task.imageId = confirmed.id
result.done.push({ fileName: file.name, imageId: confirmed.id })
task.imageId = confirmed.imageId
result.done.push({ fileName: file.name, imageId: confirmed.imageId })
ok = true
} catch (err) {
task.error = err instanceof Error ? err.message : 'Tải lên thất bại'
@ -307,5 +326,6 @@ export async function uploadFilesBatch(
}
}
await Promise.all(Array.from({ length: Math.min(CONCURRENCY, queue.length) }, worker))
if (result.failed.length === 0) await deleteUpload(jobId)
return result
}