feat: persist upload batches in IndexedDB
This commit is contained in:
parent
dba9e7ec3b
commit
4e15605ffd
2 changed files with 95 additions and 14 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import { api } from './api'
|
import { api } from './api'
|
||||||
|
import { deleteUpload, prepareUploadStorage, saveUpload } from './uploadStore'
|
||||||
|
|
||||||
export type UploadTaskStatus = 'pending' | 'uploading' | 'done' | 'error' | 'skipped'
|
export type UploadTaskStatus = 'pending' | 'uploading' | 'done' | 'error' | 'skipped'
|
||||||
|
|
||||||
|
|
@ -17,6 +18,7 @@ export interface BatchUploadResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PresignSlot {
|
interface PresignSlot {
|
||||||
|
id: string
|
||||||
fileName: string
|
fileName: string
|
||||||
putUrl: string
|
putUrl: string
|
||||||
r2Key: string
|
r2Key: string
|
||||||
|
|
@ -243,18 +245,35 @@ export async function uploadFilesBatch(
|
||||||
})
|
})
|
||||||
const emit = () => onUpdate?.(tasks.map((t) => ({ ...t })))
|
const emit = () => onUpdate?.(tasks.map((t) => ({ ...t })))
|
||||||
emit()
|
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')
|
const pending = files.filter((f) => tasks.find((t) => t.fileName === f.name)?.status === 'pending')
|
||||||
for (let i = 0; i < pending.length; i += 100) {
|
if (pending.length === 0) {
|
||||||
const chunk = pending.slice(i, i + 100)
|
return { done: [], failed: [], skipped: tasks.map((t) => t.fileName) }
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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: [] }
|
const result: BatchUploadResult = { done: [], failed: [], skipped: [] }
|
||||||
tasks.forEach((t) => {
|
tasks.forEach((t) => {
|
||||||
if (t.status === 'skipped') result.skipped.push(t.fileName)
|
if (t.status === 'skipped') result.skipped.push(t.fileName)
|
||||||
|
|
@ -285,14 +304,14 @@ export async function uploadFilesBatch(
|
||||||
task.progress = r
|
task.progress = r
|
||||||
emit()
|
emit()
|
||||||
})
|
})
|
||||||
const { data: confirmed } = await api.post<{ id: string }>(
|
const { data: confirmed } = await api.post<{ processedFiles: number; imageId: string }>(
|
||||||
`/projects/${projectId}/images`,
|
`/projects/${projectId}/upload-jobs/${jobId}/items/${slot.id}/complete`,
|
||||||
{ fileName: file.name, r2Key: slot.r2Key, width: dims.width, height: dims.height }
|
{ width: dims.width, height: dims.height }
|
||||||
)
|
)
|
||||||
task.status = 'done'
|
task.status = 'done'
|
||||||
task.progress = 1
|
task.progress = 1
|
||||||
task.imageId = confirmed.id
|
task.imageId = confirmed.imageId
|
||||||
result.done.push({ fileName: file.name, imageId: confirmed.id })
|
result.done.push({ fileName: file.name, imageId: confirmed.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'
|
||||||
|
|
@ -307,5 +326,6 @@ export async function uploadFilesBatch(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await Promise.all(Array.from({ length: Math.min(CONCURRENCY, queue.length) }, worker))
|
await Promise.all(Array.from({ length: Math.min(CONCURRENCY, queue.length) }, worker))
|
||||||
|
if (result.failed.length === 0) await deleteUpload(jobId)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
||||||
61
src/services/uploadStore.ts
Normal file
61
src/services/uploadStore.ts
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
export interface StoredUpload {
|
||||||
|
id: string
|
||||||
|
projectId: string
|
||||||
|
jobId: string
|
||||||
|
files: File[]
|
||||||
|
createdAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const DB_NAME = 'danable-upload-queue'
|
||||||
|
const DB_VERSION = 1
|
||||||
|
const STORE = 'jobs'
|
||||||
|
|
||||||
|
function openDb(): Promise<IDBDatabase> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const req = indexedDB.open(DB_NAME, DB_VERSION)
|
||||||
|
req.onupgradeneeded = () => req.result.createObjectStore(STORE, { keyPath: 'id' })
|
||||||
|
req.onsuccess = () => resolve(req.result)
|
||||||
|
req.onerror = () => reject(req.error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function prepareUploadStorage(bytes: number): Promise<void> {
|
||||||
|
if (!('indexedDB' in window)) throw new Error('Trình duyệt không hỗ trợ lưu hàng đợi upload')
|
||||||
|
const estimate = await navigator.storage?.estimate?.()
|
||||||
|
const available = (estimate?.quota ?? 0) - (estimate?.usage ?? 0)
|
||||||
|
if (available > 0 && bytes > available * 0.8) {
|
||||||
|
throw new Error('Không đủ dung lượng trình duyệt để resume upload sau reload')
|
||||||
|
}
|
||||||
|
await navigator.storage?.persist?.()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveUpload(job: StoredUpload): Promise<void> {
|
||||||
|
const db = await openDb()
|
||||||
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
const req = db.transaction(STORE, 'readwrite').objectStore(STORE).put(job)
|
||||||
|
req.onsuccess = () => resolve()
|
||||||
|
req.onerror = () => reject(req.error)
|
||||||
|
})
|
||||||
|
db.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteUpload(id: string): Promise<void> {
|
||||||
|
const db = await openDb()
|
||||||
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
const req = db.transaction(STORE, 'readwrite').objectStore(STORE).delete(id)
|
||||||
|
req.onsuccess = () => resolve()
|
||||||
|
req.onerror = () => reject(req.error)
|
||||||
|
})
|
||||||
|
db.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listUploads(): Promise<StoredUpload[]> {
|
||||||
|
const db = await openDb()
|
||||||
|
const jobs = await new Promise<StoredUpload[]>((resolve, reject) => {
|
||||||
|
const req = db.transaction(STORE, 'readonly').objectStore(STORE).getAll()
|
||||||
|
req.onsuccess = () => resolve(req.result as StoredUpload[])
|
||||||
|
req.onerror = () => reject(req.error)
|
||||||
|
})
|
||||||
|
db.close()
|
||||||
|
return jobs
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue