T-013: Download-Manager — Queue mit Bytes-Fortschritt-Events, Abbruch, Wiederaufnahme per Manifest nach Neustart, UI: Laden/Abbrechen auf Cards
This commit is contained in:
102
src/App.svelte
102
src/App.svelte
@ -1,5 +1,16 @@
|
||||
<script lang="ts">
|
||||
import { authStatus, startLogin, startDecrypt, getLibrary, type Book } from './lib/audible'
|
||||
import {
|
||||
authStatus,
|
||||
startLogin,
|
||||
startDecrypt,
|
||||
getLibrary,
|
||||
downloadList,
|
||||
downloadEnqueue,
|
||||
downloadCancel,
|
||||
onDownloadUpdate,
|
||||
type Book,
|
||||
type DlItem,
|
||||
} from './lib/audible'
|
||||
|
||||
type View = 'loading' | 'login' | 'library'
|
||||
type SortKey = 'added' | 'title' | 'author' | 'runtime' | 'percent'
|
||||
@ -15,6 +26,8 @@
|
||||
|
||||
let query = $state('')
|
||||
let sortBy = $state<SortKey>('added')
|
||||
let downloads = $state<Record<string, DlItem>>({})
|
||||
let localOverride = $state<Record<string, boolean>>({})
|
||||
|
||||
const filtered = $derived.by(() => {
|
||||
const q = query.trim().toLowerCase()
|
||||
@ -43,6 +56,44 @@
|
||||
return m ? `${h} h ${m} min` : `${h} h`
|
||||
}
|
||||
|
||||
function mb(bytes: number): string {
|
||||
return bytes >= 1e6 ? `${(bytes / 1e6).toFixed(1)} MB` : `${Math.round(bytes / 1e3)} KB`
|
||||
}
|
||||
|
||||
function isLocal(b: Book): boolean {
|
||||
return Boolean(b.local || localOverride[b.asin])
|
||||
}
|
||||
|
||||
function activeDl(b: Book): 'running' | 'queued' | undefined {
|
||||
const d = downloads[b.asin]
|
||||
if (!d) return undefined
|
||||
if (d.status === 'running') return 'running'
|
||||
if (d.status === 'queued') return 'queued'
|
||||
return undefined
|
||||
}
|
||||
|
||||
async function laden(b: Book): Promise<void> {
|
||||
error = ''
|
||||
info = ''
|
||||
try {
|
||||
const it = await downloadEnqueue(b.asin)
|
||||
downloads = { ...downloads, [b.asin]: it }
|
||||
info = `„${b.title}“ in der Download-Queue.`
|
||||
} catch (e) {
|
||||
error = String(e)
|
||||
}
|
||||
}
|
||||
|
||||
async function abbrechen(b: Book): Promise<void> {
|
||||
error = ''
|
||||
try {
|
||||
const it = await downloadCancel(b.asin)
|
||||
downloads = { ...downloads, [b.asin]: it }
|
||||
} catch (e) {
|
||||
error = String(e)
|
||||
}
|
||||
}
|
||||
|
||||
function hideBrokenCover(e: Event): void {
|
||||
;(e.currentTarget as HTMLElement).style.display = 'none'
|
||||
}
|
||||
@ -64,7 +115,7 @@
|
||||
const msg = String(e)
|
||||
if (msg.toLowerCase().includes('encrypted')) {
|
||||
// Auth-Datei verschlüsselt — Entschlüsselung im Terminal anbieten
|
||||
error = 'Auth-Datei ist verschlüsselt. Bitte einmal entschriften:'
|
||||
error = 'Auth-Datei ist verschlüsselt. Bitte einmal entsprichern:'
|
||||
view = 'login'
|
||||
} else {
|
||||
error = msg
|
||||
@ -116,10 +167,25 @@
|
||||
}
|
||||
}
|
||||
|
||||
// Beim Start prüfen: angemeldet → Bibliothek; sonst Login-Screen.
|
||||
// Beim Start: auth prüfen, Download-Events abonnieren, Queue-Stand laden.
|
||||
$effect(() => {
|
||||
checkAuth()
|
||||
return clearPoll
|
||||
const unlisten = onDownloadUpdate((it) => {
|
||||
downloads = { ...downloads, [it.asin]: it }
|
||||
if (it.status === 'done') {
|
||||
localOverride = { ...localOverride, [it.asin]: true }
|
||||
info = `„Download fertig: ${it.asin}“`
|
||||
}
|
||||
})
|
||||
downloadList().then((list) => {
|
||||
const m: Record<string, DlItem> = {}
|
||||
for (const it of list) m[it.asin] = it
|
||||
downloads = m
|
||||
})
|
||||
return () => {
|
||||
clearPoll()
|
||||
unlisten.then((f) => f())
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
@ -143,7 +209,7 @@
|
||||
<button class="ghost" onclick={() => checkAuth()} disabled={checking}>
|
||||
{checking ? 'Prüfe…' : 'Jetzt prüfen'}
|
||||
</button>
|
||||
{#if error === 'Auth-Datei ist verschlüsselt. Bitte einmal entschriften:'}
|
||||
{#if error === 'Auth-Datei ist verschlüsselt. Bitte einmal entsprichern:'}
|
||||
<button class="ghost" onclick={() => startDecrypt().catch((e) => (error = String(e)))}>
|
||||
Entschlüsseln im Terminal…
|
||||
</button>
|
||||
@ -186,6 +252,8 @@
|
||||
{:else}
|
||||
<div class="grid">
|
||||
{#each filtered as b (b.asin)}
|
||||
{@const d = downloads[b.asin]}
|
||||
{@const dlg = activeDl(b)}
|
||||
<article class="card">
|
||||
<div class="cover">
|
||||
<img
|
||||
@ -194,18 +262,38 @@
|
||||
loading="lazy"
|
||||
onerror={hideBrokenCover}
|
||||
/>
|
||||
{#if dlg === 'running'}
|
||||
<div class="overlay"><span class="muted small">{d ? mb(d.bytes) : ''}</span></div>
|
||||
{/if}
|
||||
</div>
|
||||
<div class="meta">
|
||||
<div class="t">{b.title}</div>
|
||||
<div class="muted small">{b.authors}</div>
|
||||
<div class="chips">
|
||||
{#if b.local}<span class="chip ok" title="book.m4b liegt lokal vor">lokal</span>{/if}
|
||||
{#if isLocal(b)}<span class="chip ok" title="book.m4b liegt lokal vor">lokal</span>{/if}
|
||||
{#if b.is_finished}<span class="chip done">fertig</span>{/if}
|
||||
{#if b.runtime_length_min > 0}<span class="chip">{fmtRuntime(b.runtime_length_min)}</span>{/if}
|
||||
{#if d && d.status === 'failed'}<span class="chip err" title={d.msg}>Fehlgeschlagen</span>{/if}
|
||||
</div>
|
||||
{#if b.percent_complete > 0}
|
||||
{#if b.percent_complete > 0 && !dlg}
|
||||
<div class="progress"><div class="fill" style={`width:${b.percent_complete}%`}></div></div>
|
||||
{/if}
|
||||
{#if dlg === 'running'}
|
||||
<div class="progress indeterminate"><div class="fill"></div></div>
|
||||
{/if}
|
||||
<div class="actions">
|
||||
{#if dlg === 'running' || dlg === 'queued'}
|
||||
<button class="ghost" onclick={() => abbrechen(b)}>
|
||||
{dlg === 'queued' ? 'In der Queue — abbrechen?' : 'Abbrechen'}
|
||||
</button>
|
||||
{#if dlg === 'queued'}<span class="muted small">wartet…</span>{/if}
|
||||
{:else if d && d.status === 'failed'}
|
||||
<button class="ghost" onclick={() => laden(b)}>Nochmal laden</button>
|
||||
<span class="muted small" title={d.msg}>{d.msg.slice(0, 60)}</span>
|
||||
{:else if !isLocal(b)}
|
||||
<button class="primary" onclick={() => laden(b)}>Laden</button>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
{/each}
|
||||
|
||||
Reference in New Issue
Block a user