311 lines
9.6 KiB
TypeScript
311 lines
9.6 KiB
TypeScript
import { api } from './api'
|
|
|
|
export type UploadTaskStatus = 'pending' | 'uploading' | 'done' | 'error' | 'skipped'
|
|
|
|
export interface UploadTask {
|
|
fileName: string
|
|
status: UploadTaskStatus
|
|
progress: number
|
|
error?: string
|
|
imageId?: string
|
|
}
|
|
|
|
export interface BatchUploadResult {
|
|
done: { fileName: string; imageId: string }[]
|
|
failed: { fileName: string; error: string }[]
|
|
skipped: string[]
|
|
}
|
|
|
|
interface PresignSlot {
|
|
fileName: string
|
|
putUrl: string
|
|
r2Key: string
|
|
}
|
|
|
|
const CONCURRENCY = 4
|
|
const MAX_RETRIES = 2
|
|
|
|
function putWithProgress(
|
|
url: string,
|
|
file: File,
|
|
contentType: string,
|
|
onProgress: (ratio: number) => void
|
|
): Promise<void> {
|
|
// fetch has no upload progress; XHR is used for this single call only.
|
|
return new Promise((resolve, reject) => {
|
|
const xhr = new XMLHttpRequest()
|
|
xhr.open('PUT', url)
|
|
xhr.setRequestHeader('Content-Type', contentType)
|
|
xhr.upload.onprogress = (e) => {
|
|
if (e.lengthComputable) onProgress(e.loaded / e.total)
|
|
}
|
|
xhr.onload = () =>
|
|
xhr.status >= 200 && xhr.status < 300
|
|
? resolve()
|
|
: reject(new Error(`Tải lên kho lưu trữ thất bại (${xhr.status})`))
|
|
xhr.onerror = () => reject(new Error('Lỗi mạng khi tải lên'))
|
|
xhr.send(file)
|
|
})
|
|
}
|
|
|
|
async function readDimensions(file: File): Promise<{ width: number; height: number }> {
|
|
try {
|
|
const bmp = await createImageBitmap(file)
|
|
const dims = { width: bmp.width, height: bmp.height }
|
|
bmp.close()
|
|
return dims
|
|
} catch {
|
|
return { width: 0, height: 0 }
|
|
}
|
|
}
|
|
|
|
// Recursively collect files from a drop (files and/or folders) preserving names.
|
|
export async function collectDroppedFiles(dt: DataTransfer): Promise<File[]> {
|
|
const items = Array.from(dt.items).filter((i) => i.kind === 'file')
|
|
const entries = items
|
|
.map((i) => (typeof i.webkitGetAsEntry === 'function' ? i.webkitGetAsEntry() : null))
|
|
.filter((e): e is FileSystemEntry => e !== null)
|
|
if (entries.length === 0 || entries.every((e) => e.isFile)) {
|
|
return Array.from(dt.files)
|
|
}
|
|
const out: File[] = []
|
|
const walk = async (entry: FileSystemEntry, prefix: string): Promise<void> => {
|
|
if (entry.isFile) {
|
|
const blob = await new Promise<File>((resolve, reject) =>
|
|
(entry as FileSystemFileEntry).file(resolve, reject)
|
|
)
|
|
out.push(new File([blob], prefix + blob.name, { type: blob.type }))
|
|
return
|
|
}
|
|
if (entry.isDirectory) {
|
|
const reader = (entry as FileSystemDirectoryEntry).createReader()
|
|
for (;;) {
|
|
const batch: FileSystemEntry[] = await new Promise((resolve, reject) =>
|
|
reader.readEntries(resolve, reject)
|
|
)
|
|
if (batch.length === 0) break
|
|
for (const child of batch) await walk(child, `${prefix}${entry.name}/`)
|
|
}
|
|
}
|
|
}
|
|
for (const entry of entries) await walk(entry, '')
|
|
return out
|
|
}
|
|
|
|
// ---- Resumable multipart upload for single big files (100MB..GBs) ----
|
|
// Parts go straight to R2 in parallel with per-part retry; ETags persist in
|
|
// localStorage so a dropped network resumes instead of restarting.
|
|
const MP_PART_SIZE = 8 * 1024 * 1024
|
|
const MP_CONCURRENCY = 4
|
|
const MP_RETRIES = 2
|
|
const MP_RESUME_PREFIX = 'danable:mp:'
|
|
|
|
interface MpResume {
|
|
key: string
|
|
uploadId: string
|
|
etags: Record<number, string>
|
|
totalParts: number
|
|
}
|
|
|
|
function mpFingerprint(file: File): string {
|
|
return `${MP_RESUME_PREFIX}${file.name}-${file.size}-${file.lastModified}`
|
|
}
|
|
|
|
function putPart(
|
|
url: string,
|
|
blob: Blob,
|
|
onProgress: (loaded: number) => void
|
|
): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const xhr = new XMLHttpRequest()
|
|
xhr.open('PUT', url)
|
|
xhr.upload.onprogress = (e) => {
|
|
if (e.lengthComputable) onProgress(e.loaded)
|
|
}
|
|
xhr.onload = () => {
|
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
resolve(xhr.getResponseHeader('ETag') ?? '')
|
|
} else {
|
|
reject(new Error(`Part thất bại (${xhr.status})`))
|
|
}
|
|
}
|
|
xhr.onerror = () => reject(new Error('Lỗi mạng khi tải part'))
|
|
xhr.send(blob)
|
|
})
|
|
}
|
|
|
|
export async function uploadFileMultipart(
|
|
projectId: string,
|
|
file: File,
|
|
onProgress?: (ratio: number, uploadedBytes: number) => void
|
|
): Promise<{ r2Key: string }> {
|
|
const totalParts = Math.max(1, Math.ceil(file.size / MP_PART_SIZE))
|
|
const saved = (() => {
|
|
try {
|
|
const raw = localStorage.getItem(mpFingerprint(file))
|
|
return raw ? (JSON.parse(raw) as MpResume) : null
|
|
} catch {
|
|
return null
|
|
}
|
|
})()
|
|
|
|
let key: string
|
|
let uploadId: string
|
|
const etags: Record<number, string> = { ...(saved?.etags ?? {}) }
|
|
if (saved && saved.totalParts === totalParts) {
|
|
// Resume: reuse key + uploadId, skip parts that already have ETags.
|
|
key = saved.key
|
|
uploadId = saved.uploadId
|
|
} else {
|
|
const { data } = await api.post<{ key: string; uploadId: string }>(
|
|
`/projects/${projectId}/uploads/multipart`,
|
|
{ fileName: file.name, contentType: file.type || 'application/octet-stream' }
|
|
)
|
|
key = data.key
|
|
uploadId = data.uploadId
|
|
}
|
|
const persist = () => {
|
|
try {
|
|
localStorage.setItem(
|
|
mpFingerprint(file),
|
|
JSON.stringify({ key, uploadId, etags, totalParts })
|
|
)
|
|
} catch {
|
|
// Private mode: resume simply unavailable.
|
|
}
|
|
}
|
|
persist()
|
|
|
|
const { data: partUrls } = await api.post<{ urls: Record<number, string> }>(
|
|
`/projects/${projectId}/uploads/multipart/parts`,
|
|
{ key, uploadId, numbers: Array.from({ length: totalParts }, (_, i) => i + 1) }
|
|
)
|
|
|
|
const loaded = new Array<number>(totalParts).fill(0)
|
|
const report = () => {
|
|
const sum = loaded.reduce((a, b) => a + b, 0)
|
|
onProgress?.(sum / Math.max(1, file.size), sum)
|
|
}
|
|
let cursor = 0
|
|
const numbers = Array.from({ length: totalParts }, (_, i) => i + 1)
|
|
const worker = async (): Promise<void> => {
|
|
for (;;) {
|
|
const n = numbers[cursor++]
|
|
if (n === undefined) return
|
|
if (etags[n]) continue
|
|
const start = (n - 1) * MP_PART_SIZE
|
|
const blob = file.slice(start, start + MP_PART_SIZE)
|
|
let done = false
|
|
for (let attempt = 0; attempt <= MP_RETRIES && !done; attempt++) {
|
|
try {
|
|
etags[n] = await putPart(partUrls.urls[n]!, blob, (ld) => {
|
|
loaded[n - 1] = ld
|
|
report()
|
|
})
|
|
loaded[n - 1] = blob.size
|
|
report()
|
|
persist()
|
|
done = true
|
|
} catch (err) {
|
|
if (attempt === MP_RETRIES) throw err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
await Promise.all(Array.from({ length: Math.min(MP_CONCURRENCY, totalParts) }, worker))
|
|
|
|
await api.post(`/projects/${projectId}/uploads/multipart/complete`, {
|
|
key,
|
|
uploadId,
|
|
parts: Object.entries(etags).map(([num, ETag]) => ({ ETag, PartNumber: Number(num) })),
|
|
})
|
|
try {
|
|
localStorage.removeItem(mpFingerprint(file))
|
|
} catch {
|
|
// ignore
|
|
}
|
|
onProgress?.(1, file.size)
|
|
return { r2Key: key }
|
|
}
|
|
|
|
export async function uploadFilesBatch(
|
|
projectId: string,
|
|
files: File[],
|
|
onUpdate?: (tasks: UploadTask[]) => void
|
|
): Promise<BatchUploadResult> {
|
|
const seen = new Set<string>()
|
|
const tasks: UploadTask[] = files.map((f) => {
|
|
if (seen.has(f.name)) {
|
|
return { fileName: f.name, status: 'skipped', progress: 0 }
|
|
}
|
|
seen.add(f.name)
|
|
return { fileName: f.name, status: 'pending', progress: 0 }
|
|
})
|
|
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))
|
|
}
|
|
|
|
const result: BatchUploadResult = { done: [], failed: [], skipped: [] }
|
|
tasks.forEach((t) => {
|
|
if (t.status === 'skipped') result.skipped.push(t.fileName)
|
|
})
|
|
|
|
let cursor = 0
|
|
const queue = pending.slice()
|
|
const worker = async (): Promise<void> => {
|
|
for (;;) {
|
|
const file = queue[cursor++]
|
|
if (!file) return
|
|
const task = tasks.find((t) => t.fileName === file.name)!
|
|
const slot = slots.get(file.name)
|
|
if (!slot) {
|
|
task.status = 'error'
|
|
task.error = 'Không lấy được URL tải lên'
|
|
result.failed.push({ fileName: file.name, error: task.error })
|
|
emit()
|
|
continue
|
|
}
|
|
task.status = 'uploading'
|
|
emit()
|
|
let ok = false
|
|
for (let attempt = 0; attempt <= MAX_RETRIES && !ok; attempt++) {
|
|
try {
|
|
const dims = await readDimensions(file)
|
|
await putWithProgress(slot.putUrl, file, file.type || 'image/jpeg', (r) => {
|
|
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 }
|
|
)
|
|
task.status = 'done'
|
|
task.progress = 1
|
|
task.imageId = confirmed.id
|
|
result.done.push({ fileName: file.name, imageId: confirmed.id })
|
|
ok = true
|
|
} catch (err) {
|
|
task.error = err instanceof Error ? err.message : 'Tải lên thất bại'
|
|
if (attempt === MAX_RETRIES) {
|
|
task.status = 'error'
|
|
result.failed.push({ fileName: file.name, error: task.error })
|
|
}
|
|
} finally {
|
|
emit()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
await Promise.all(Array.from({ length: Math.min(CONCURRENCY, queue.length) }, worker))
|
|
return result
|
|
}
|