Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions web/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
21 changes: 11 additions & 10 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -599,16 +599,17 @@ function Dashboard({ user: initialUser, onLogout }: { user: User; onLogout: () =
Delete
</button>
</div>
<FileManager key={selected.id} torrentId={selected.id} />
{(selected.stats.complete ||
selected.status === 'seeding' ||
selected.completed_at) && (
<div className="detail-actions">
<a className="button" href={api.downloadUrl(selected.id)}>
Download All (ZIP)
</a>
</div>
)}
<FileManager
key={selected.id}
torrentId={selected.id}
zipUrl={
selected.stats.complete ||
selected.status === 'seeding' ||
selected.completed_at
? api.downloadUrl(selected.id)
: undefined
}
/>
</aside>
) : null}
</div>
Expand Down
76 changes: 75 additions & 1 deletion web/src/FileManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> {
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<FileEntry[]>([])
const [writable, setWritable] = useState(false)
const [error, setError] = useState('')
const [busy, setBusy] = useState(false)
const [progress, setProgress] = useState('')
const [selected, setSelected] = useState<Map<string, boolean>>(new Map()) // path -> is_dir

const load = useCallback(
async (path: string) => {
Expand Down Expand Up @@ -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 (
<section className="file-manager">
<h3>Files</h3>

{zipUrl ? (
<div className="fm-downloads">
<a className="button" href={zipUrl}>
Download All (ZIP)
</a>
<button
type="button"
disabled={busy || selected.size === 0}
onClick={() => void downloadSelected()}
>
{progress || `Download Selected${selected.size ? ` (${selected.size})` : ''}`}
</button>
</div>
) : null}

{dir !== '/' ? (
<nav className="fm-crumbs" aria-label="Path">
<button type="button" onClick={() => void load('/')}>
Expand Down Expand Up @@ -84,6 +151,13 @@ export function FileManager({ torrentId }: { torrentId: number }) {
const full = join(dir, e.name)
return (
<li key={e.name}>
<input
type="checkbox"
className="fm-check"
aria-label={`Select ${e.name}`}
checked={selected.has(full)}
onChange={() => toggle(full, e.is_dir)}
/>
{e.is_dir ? (
<button type="button" className="fm-name" onClick={() => void load(full)}>
📁 {e.name}
Expand Down
Loading