feat: dateiaustausch im frontend
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>
This commit is contained in:
1 parent
52a8fd019f
commit
4b79f08bdd
18 files changed
+872
-28
No files matched your search
@@ -14,6 +14,7 @@ function clip(overrides: Partial<Clip> = {}): Clip {
|
||||
language: null,
|
||||
pinned: false,
|
||||
project_id: null,
|
||||
file: null,
|
||||
version: 1,
|
||||
created_at: '2026-10-25T10:00:00Z',
|
||||
updated_at: '2026-10-25T10:00:00Z',
|
||||
|
||||
@@ -48,6 +48,7 @@ const clip: Clip = {
|
||||
language: null,
|
||||
pinned: false,
|
||||
project_id: null,
|
||||
file: null,
|
||||
version: 1,
|
||||
created_at: '2026-10-25T10:00:00Z',
|
||||
updated_at: '2026-10-25T10:00:00Z',
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { Clip } from '@/api/types'
|
||||
import { UploadAborted, uploadFile, type UploadHandle } from '@/api/upload'
|
||||
|
||||
import { useClipsStore } from './clips'
|
||||
import { useUploadsStore } from './uploads'
|
||||
|
||||
vi.mock('@/api/upload', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('@/api/upload')>()),
|
||||
uploadFile: vi.fn(),
|
||||
}))
|
||||
|
||||
const upload = vi.mocked(uploadFile)
|
||||
|
||||
function fileClip(id: number, name: string): Clip {
|
||||
return {
|
||||
id,
|
||||
kind: 'file',
|
||||
title: null,
|
||||
content: name,
|
||||
language: null,
|
||||
pinned: false,
|
||||
project_id: null,
|
||||
file: { name, content_type: 'text/plain', size: 3, previewable: false, expires_at: null },
|
||||
version: 1,
|
||||
created_at: '2026-10-25T10:00:00Z',
|
||||
updated_at: '2026-10-25T10:00:00Z',
|
||||
}
|
||||
}
|
||||
|
||||
/** A pending upload that the test settles by hand. */
|
||||
function deferred(): UploadHandle & { resolve: (c: Clip) => void; reject: (e: Error) => void } {
|
||||
let resolve!: (c: Clip) => void
|
||||
let reject!: (e: Error) => void
|
||||
const promise = new Promise<Clip>((res, rej) => {
|
||||
resolve = res
|
||||
reject = rej
|
||||
})
|
||||
return { promise, abort: vi.fn(() => reject(new UploadAborted())), resolve, reject }
|
||||
}
|
||||
|
||||
const flush = () => new Promise((r) => setTimeout(r, 0))
|
||||
const options = { expiresDays: 7 as const, projectId: null }
|
||||
|
||||
describe('uploads store', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
upload.mockReset()
|
||||
const store = useUploadsStore()
|
||||
store.usage = { used_bytes: 0, quota_bytes: 1000, max_file_bytes: 10 }
|
||||
})
|
||||
|
||||
it('rejects empty and oversized files without sending them', async () => {
|
||||
const store = useUploadsStore()
|
||||
await store.add([new File([], 'empty.txt'), new File(['x'.repeat(11)], 'big.bin')], options)
|
||||
|
||||
expect(upload).not.toHaveBeenCalled()
|
||||
expect(store.items.map((u) => [u.name, u.status, u.error])).toEqual([
|
||||
['empty.txt', 'failed', 'Die Datei ist leer.'],
|
||||
['big.bin', 'failed', 'Die Datei ist zu groß (höchstens 10 B).'],
|
||||
])
|
||||
})
|
||||
|
||||
it('uploads one file after another and adds the clips', async () => {
|
||||
const first = deferred()
|
||||
const second = deferred()
|
||||
upload.mockReturnValueOnce(first).mockReturnValueOnce(second)
|
||||
const store = useUploadsStore()
|
||||
|
||||
await store.add([new File(['abc'], 'a.txt'), new File(['def'], 'b.txt')], options)
|
||||
expect(upload).toHaveBeenCalledOnce()
|
||||
expect(store.items.map((u) => u.status)).toEqual(['uploading', 'waiting'])
|
||||
|
||||
first.resolve(fileClip(1, 'a.txt'))
|
||||
await flush()
|
||||
expect(upload).toHaveBeenCalledTimes(2)
|
||||
second.resolve(fileClip(2, 'b.txt'))
|
||||
await flush()
|
||||
|
||||
expect(store.items).toEqual([])
|
||||
expect(
|
||||
useClipsStore()
|
||||
.items.map((c) => c.id)
|
||||
.sort(),
|
||||
).toEqual([1, 2])
|
||||
expect(store.usage?.used_bytes).toBe(6)
|
||||
})
|
||||
|
||||
it('keeps failed uploads until dismissed', async () => {
|
||||
const job = deferred()
|
||||
upload.mockReturnValueOnce(job)
|
||||
const store = useUploadsStore()
|
||||
|
||||
await store.add([new File(['abc'], 'a.txt')], options)
|
||||
job.reject(new Error('Nicht genug Speicherplatz'))
|
||||
await flush()
|
||||
|
||||
expect(store.items[0]).toMatchObject({ status: 'failed', error: 'Nicht genug Speicherplatz' })
|
||||
store.dismiss(store.items[0]!.id)
|
||||
expect(store.items).toEqual([])
|
||||
})
|
||||
|
||||
it('cancels a running upload', async () => {
|
||||
const job = deferred()
|
||||
upload.mockReturnValueOnce(job)
|
||||
const store = useUploadsStore()
|
||||
|
||||
await store.add([new File(['abc'], 'a.txt')], options)
|
||||
store.dismiss(store.items[0]!.id)
|
||||
await flush()
|
||||
|
||||
expect(job.abort).toHaveBeenCalledOnce()
|
||||
expect(store.items).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,134 @@
|
||||
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 }
|
||||
})
|
||||
Reference in new issue
Block a user