diff --git a/web/src/App.css b/web/src/App.css index eefb18b..19346bf 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -850,3 +850,16 @@ button.danger { color: var(--muted); margin: 0 0 0.6rem; } + +.fm-downloads { + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 0.4rem; + margin-bottom: 0.75rem; +} + +.fm-check { + flex: none; + margin: 0; +} diff --git a/web/src/App.tsx b/web/src/App.tsx index 58f5d01..f118e87 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -599,16 +599,17 @@ function Dashboard({ user: initialUser, onLogout }: { user: User; onLogout: () = Delete - - {(selected.stats.complete || - selected.status === 'seeding' || - selected.completed_at) && ( -
- - Download All (ZIP) - -
- )} + ) : null} diff --git a/web/src/FileManager.tsx b/web/src/FileManager.tsx index d2cbd2b..1673135 100644 --- a/web/src/FileManager.tsx +++ b/web/src/FileManager.tsx @@ -10,12 +10,27 @@ function parent(dir: string): string { return i <= 0 ? '/' : dir.slice(0, i) } -export function FileManager({ torrentId }: { torrentId: number }) { +async function collectFiles(torrentId: number, dir: string): Promise { + const res = await api.listFiles(torrentId, dir) + const out: string[] = [] + for (const e of res.entries) { + const full = join(dir, e.name) + if (e.is_dir) out.push(...(await collectFiles(torrentId, full))) + else out.push(full) + } + return out +} + +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)) + +export function FileManager({ torrentId, zipUrl }: { torrentId: number; zipUrl?: string }) { const [dir, setDir] = useState('/') const [entries, setEntries] = useState([]) const [writable, setWritable] = useState(false) const [error, setError] = useState('') const [busy, setBusy] = useState(false) + const [progress, setProgress] = useState('') + const [selected, setSelected] = useState>(new Map()) // path -> is_dir const load = useCallback( async (path: string) => { @@ -48,12 +63,64 @@ export function FileManager({ torrentId }: { torrentId: number }) { } } + function toggle(path: string, isDir: boolean) { + setSelected((cur) => { + const next = new Map(cur) + if (next.has(path)) next.delete(path) + else next.set(path, isDir) + return next + }) + } + + // Starts one browser download per selected file (folders are expanded); + // browsers may ask to allow multiple downloads. + async function downloadSelected() { + setBusy(true) + try { + const paths: string[] = [] + for (const [sel, isDir] of selected) { + if (isDir) paths.push(...(await collectFiles(torrentId, sel))) + else paths.push(sel) + } + for (let i = 0; i < paths.length; i++) { + setProgress(`Downloading ${i + 1}/${paths.length}…`) + const a = document.createElement('a') + a.href = api.fileDownloadUrl(torrentId, paths[i]) + a.download = '' + document.body.appendChild(a) + a.click() + a.remove() + await sleep(400) + } + } catch (err) { + setError(err instanceof Error ? err.message : 'Download failed') + } finally { + setProgress('') + setBusy(false) + } + } + const crumbs = dir === '/' ? [] : dir.slice(1).split('/') return (

Files

+ {zipUrl ? ( +
+ + Download All (ZIP) + + +
+ ) : null} + {dir !== '/' ? (