Files
2026-08-21 15:29:01 +02:00

223 lines
7.4 KiB
Vue

<template>
<div v-if="finished" class="flex flex-col items-center gap-4 py-8 text-center">
<div class="flex items-center gap-3">
<div
class="flex h-10 w-10 shrink-0 items-center justify-center rounded-full"
:class="hasErrors ? 'bg-red-500' : 'bg-green-500'"
>
<span class="material-symbols-outlined !text-[22px] text-white">{{ hasErrors ? 'close' : 'check' }}</span>
</div>
<p class="text-left text-text">
{{ hasErrors
? `${errorCount} von ${queue.length} Datei${queue.length === 1 ? '' : 'en'} fehlgeschlagen`
: `${successCount} Datei${successCount === 1 ? '' : 'en'} hochgeladen` }}
</p>
</div>
<p v-if="hasErrors" class="max-w-xs text-sm text-muted">
{{ successCount }} erfolgreich, {{ errorCount }} fehlgeschlagen. Nochmal versuchen?
</p>
<div class="mt-1 flex gap-2">
<button
v-if="hasErrors"
type="button"
class="rounded-md border border-border px-3 py-2 text-sm font-medium text-text transition-colors hover:bg-surface-raised"
@click="retryFailed"
>
Erneut versuchen
</button>
<button
type="button"
class="rounded-md bg-accent px-4 py-2 text-sm font-medium text-white transition-opacity hover:opacity-90"
@click="usePopup().close()"
>
Fertig
</button>
</div>
</div>
<div v-else class="space-y-5">
<div>
<FieldLabel>Zielpfad</FieldLabel>
<div class="mt-1 flex items-center gap-2 rounded-md border border-border bg-bg px-3 py-2">
<span class="material-symbols-outlined !text-[18px] shrink-0 text-muted">folder</span>
<div class="flex flex-1 items-center font-mono text-sm">
<span class="shrink-0 text-muted">{{ bucket }}/</span>
<input
v-model="path"
type="text"
placeholder="backups/"
class="min-w-0 flex-1 bg-transparent text-text placeholder:text-muted/60 focus:outline-none"
>
</div>
</div>
</div>
<div
class="flex flex-col items-center justify-center gap-1.5 rounded-lg border-2 border-dashed px-6 py-10 text-center transition-colors"
:class="isDragging ? 'border-accent bg-accent-dim/40' : 'border-border hover:border-muted'"
@dragover.prevent="isDragging = true"
@dragleave.prevent="isDragging = false"
@drop.prevent="handleDrop"
@click="fileInput?.click()"
>
<span class="material-symbols-outlined !text-[32px] text-muted">upload_file</span>
<p class="text-sm text-text">Dateien hierher ziehen</p>
<p class="text-xs text-muted">oder klicken zum Auswählen</p>
<input ref="fileInput" type="file" multiple class="hidden" @change="handleFileSelect">
</div>
<div v-if="queue.length" class="max-h-48 space-y-1.5 overflow-y-auto">
<div
v-for="item in queue"
:key="item.id"
class="flex items-center gap-2.5 rounded-md border border-border px-3 py-2"
>
<span class="material-symbols-outlined !text-[16px] shrink-0" :class="statusIconClass(item.status)">
{{ statusIcon(item.status) }}
</span>
<div class="min-w-0 flex-1">
<p class="truncate font-mono text-xs text-text">{{ normalizedPath }}{{ item.file.name }}</p>
<p class="text-[11px] text-muted">{{ formatBytes(item.file.size) }}</p>
</div>
<button
v-if="item.status === 'pending'"
type="button"
class="shrink-0 text-muted transition-colors hover:text-text"
@click="queue = queue.filter((q) => q.id !== item.id)"
>
<span class="material-symbols-outlined !text-[16px]">close</span>
</button>
</div>
</div>
<div class="flex justify-end gap-2 pt-1">
<button
type="button"
class="rounded-md border border-border px-3 py-2 text-sm font-medium text-text transition-colors hover:bg-surface-raised"
@click="usePopup().close()"
>
Abbrechen
</button>
<button
type="button"
class="flex items-center gap-2 rounded-md bg-accent px-4 py-2 text-sm font-medium text-white transition-opacity hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-50"
:disabled="!pendingCount || uploading"
@click="startUpload"
>
<span class="material-symbols-outlined !text-[18px]" :class="{ 'animate-spin': uploading }">
{{ uploading ? 'progress_activity' : 'upload' }}
</span>
{{ uploading ? `Lädt hoch (${doneCount}/${queue.length})…` : `${pendingCount} Datei${pendingCount === 1 ? '' : 'en'} hochladen` }}
</button>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
import { formatBytes } from '~/utils/format'
type Status = 'pending' | 'uploading' | 'done' | 'error'
interface QueueItem {
id: string
file: File
status: Status
}
const payload = usePopup().require<{ bucket: string }>().payload!
const bucket = payload.bucket
const { uploadObject } = useS3Client()
const path = ref('')
const queue = ref<QueueItem[]>([])
const uploading = ref(false)
const finished = ref(false)
const isDragging = ref(false)
const fileInput = ref<HTMLInputElement | null>(null)
const normalizedPath = computed(() => {
if (!path.value) {
return ''
}
return path.value.endsWith('/') ? path.value : `${path.value}/`
})
const pendingCount = computed(() => queue.value.filter((item) => item.status === 'pending').length)
const doneCount = computed(() => queue.value.filter((item) => item.status === 'done' || item.status === 'error').length)
const successCount = computed(() => queue.value.filter((item) => item.status === 'done').length)
const errorCount = computed(() => queue.value.filter((item) => item.status === 'error').length)
const hasErrors = computed(() => errorCount.value > 0)
function addFiles(fileList: FileList | null) {
if (!fileList) {
return
}
for (const file of Array.from(fileList)) {
queue.value.push({ id: `${file.name}-${file.size}-${Math.random().toString(36).slice(2)}`, file, status: 'pending' })
}
}
function handleDrop(event: DragEvent) {
isDragging.value = false
addFiles(event.dataTransfer?.files ?? null)
}
function handleFileSelect(event: Event) {
const input = event.target as HTMLInputElement
addFiles(input.files)
input.value = ''
}
function statusIcon(status: Status) {
if (status === 'uploading') {
return 'progress_activity'
}
if (status === 'done') {
return 'check_circle'
}
if (status === 'error') {
return 'error'
}
return 'draft'
}
function statusIconClass(status: Status) {
if (status === 'uploading') {
return 'animate-spin text-muted'
}
if (status === 'done') {
return 'text-accent'
}
if (status === 'error') {
return 'text-red-500'
}
return 'text-muted'
}
async function startUpload() {
uploading.value = true
const pending = queue.value.filter((item) => item.status === 'pending')
for (const item of pending) {
item.status = 'uploading'
try {
await uploadObject(bucket, `${normalizedPath.value}${item.file.name}`, item.file)
item.status = 'done'
}
catch {
item.status = 'error'
}
}
uploading.value = false
finished.value = true
usePopup().callback()
}
function retryFailed() {
queue.value = queue.value
.filter((item) => item.status === 'error')
.map((item) => ({ ...item, status: 'pending' as Status }))
finished.value = false
}
</script>