Hochladen per Dateiauswahl, Drag & Drop und Einfügen mit Fortschritt und Abbrechen, Aufbewahrung wählbar. Datei-Karten mit Bildvorschau, Größe, Ablauf und Download; Speicheranzeige im Konto. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
3.9 KiB
TypeScript
135 lines
3.9 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { ref } from 'vue'
|
|
|
|
import { api } from '@/api/client'
|
|
import type { ExpiryDays, FileUsage } from '@/api/types'
|
|
import { UploadAborted, uploadFile } from '@/api/upload'
|
|
import { useClipsStore } from '@/stores/clips'
|
|
import { useProjectsStore } from '@/stores/projects'
|
|
import { formatBytes } from '@/utils/format'
|
|
|
|
export interface Upload {
|
|
id: number
|
|
name: string
|
|
size: number
|
|
loaded: number
|
|
status: 'waiting' | 'uploading' | 'failed'
|
|
error: string | null
|
|
}
|
|
|
|
export const DEFAULT_EXPIRY: ExpiryDays = 7
|
|
|
|
/**
|
|
* Upload queue: files go up one after another, so a phone on a slow line
|
|
* does not split its bandwidth. Finished uploads leave the list; failed ones stay
|
|
* until dismissed.
|
|
*/
|
|
export const useUploadsStore = defineStore('uploads', () => {
|
|
const items = ref<Upload[]>([])
|
|
const usage = ref<FileUsage | null>(null)
|
|
let nextId = 1
|
|
let running = false
|
|
const pending = new Map<
|
|
number,
|
|
{ file: File; expiresDays: ExpiryDays; projectId: number | null }
|
|
>()
|
|
const aborts = new Map<number, () => void>()
|
|
|
|
async function loadUsage(): Promise<FileUsage> {
|
|
usage.value = await api.get<FileUsage>('/files/usage')
|
|
return usage.value
|
|
}
|
|
|
|
function patch(id: number, changes: Partial<Upload>): void {
|
|
const index = items.value.findIndex((u) => u.id === id)
|
|
if (index !== -1) items.value[index] = { ...items.value[index]!, ...changes }
|
|
}
|
|
|
|
function dismiss(id: number): void {
|
|
aborts.get(id)?.()
|
|
pending.delete(id)
|
|
items.value = items.value.filter((u) => u.id !== id)
|
|
}
|
|
|
|
/** Queue files; obviously too large ones fail right away without being sent. */
|
|
async function add(
|
|
files: Iterable<File>,
|
|
options: { expiresDays: ExpiryDays; projectId: number | null },
|
|
): Promise<void> {
|
|
const limits = usage.value ?? (await loadUsage().catch(() => null))
|
|
for (const file of files) {
|
|
const id = nextId++
|
|
const upload: Upload = {
|
|
id,
|
|
name: file.name || 'datei',
|
|
size: file.size,
|
|
loaded: 0,
|
|
status: 'waiting',
|
|
error: null,
|
|
}
|
|
if (file.size === 0) {
|
|
upload.status = 'failed'
|
|
upload.error = 'Die Datei ist leer.'
|
|
} else if (limits && file.size > limits.max_file_bytes) {
|
|
upload.status = 'failed'
|
|
upload.error = `Die Datei ist zu groß (höchstens ${formatBytes(limits.max_file_bytes)}).`
|
|
} else {
|
|
pending.set(id, { file, ...options })
|
|
}
|
|
items.value.push(upload)
|
|
}
|
|
void run()
|
|
}
|
|
|
|
async function run(): Promise<void> {
|
|
if (running) return
|
|
running = true
|
|
try {
|
|
for (let next = pending.entries().next(); !next.done; next = pending.entries().next()) {
|
|
const [id, job] = next.value
|
|
pending.delete(id)
|
|
await send(id, job.file, job.expiresDays, job.projectId)
|
|
}
|
|
} finally {
|
|
running = false
|
|
}
|
|
}
|
|
|
|
async function send(
|
|
id: number,
|
|
file: File,
|
|
expiresDays: ExpiryDays,
|
|
projectId: number | null,
|
|
): Promise<void> {
|
|
patch(id, { status: 'uploading' })
|
|
const handle = uploadFile(file, {
|
|
expiresDays,
|
|
projectId,
|
|
onProgress: (loaded) => patch(id, { loaded }),
|
|
})
|
|
aborts.set(id, handle.abort)
|
|
try {
|
|
const clip = await handle.promise
|
|
useClipsStore().upsert(clip)
|
|
useProjectsStore().scheduleRefresh()
|
|
items.value = items.value.filter((u) => u.id !== id)
|
|
if (usage.value)
|
|
usage.value = { ...usage.value, used_bytes: usage.value.used_bytes + file.size }
|
|
} catch (e) {
|
|
if (e instanceof UploadAborted) return
|
|
patch(id, { status: 'failed', error: e instanceof Error ? e.message : String(e) })
|
|
} finally {
|
|
aborts.delete(id)
|
|
}
|
|
}
|
|
|
|
function reset(): void {
|
|
for (const abort of aborts.values()) abort()
|
|
pending.clear()
|
|
items.value = []
|
|
usage.value = null
|
|
}
|
|
|
|
return { items, usage, loadUsage, add, dismiss, reset }
|
|
})
|