feat(dashboard): download .gguf models from URL with live progress
## What
Adds a 'Download from URL' section to the Models tab. Paste a HuggingFace
URL (or any direct .gguf URL) and the dashboard fetches it into
gpu/llama_models/ with live progress, speed, ETA, and cancel support.
## Backend
### `dashboard/src/lib/downloads.ts` (new)
In-memory download tracker. Single Node process, so no DB needed.
- `startModelDownload(url, filenameOverride?)` — validates URL (http/https
only), sanitizes filename (.gguf only, no path traversal), spawns curl:
curl -L -f -S --progress-bar -o <dest>.part <url>
curl follows redirects (HuggingFace, Cloudflare R2), fails on HTTP 4xx/5xx,
writes to <dest>.part for atomic rename on success.
- `subscribeToJob(id, cb)` — pub/sub for progress updates
- `cancelDownload(id)` — sends SIGTERM to curl, deletes .part file
- `listJobs()` / `getJob(id)` — read access
- `publicJob(job)` — strips internal fields (_child, _subscribers) before
returning to API caller
Progress tracking:
- Parses curl --progress-bar stderr for percent (looks like '#### 54.3%')
- Periodically stats the .part file for actual bytes downloaded (more
reliable than parsing curl output)
- Does HEAD request via curl -I to get Content-Length for total bytes
History: keeps last 50 finished jobs to avoid memory bloat.
### API routes (all under /api/models/download/)
- `POST /api/models/download` — body: {url, filename?}, returns job
- `GET /api/models/download/status` — returns all jobs (active + recent)
- `GET /api/models/download/[id]/stream` — SSE push of progress events:
event: progress
data: {id, status, percent, bytesDownloaded, bytesTotal, ...}
Plus 'end' event when job reaches terminal state.
- `POST /api/models/download/[id]/cancel` — SIGTERMs curl, deletes .part
### Dockerfile
Added `curl` to the apk add list — needed for the actual download.
`wget` was already present (kept for compatibility with the rest of the
codebase that uses it for health checks).
## Frontend
### ModelsTab additions
- URL input + Download button — Enter to submit
- 'Active downloads' list with progress bars, bytes downloaded / total,
speed (e.g. '1.5 MB/s'), elapsed time, Cancel button
- 'Recent' list (last 5 finished) — shows final size + duration
### DownloadJobRow component
Reusable row for both active and finished jobs. Two modes:
- Full (default): badge + filename + progress bar + stats + source URL link
- Compact: smaller, just badge + filename + final size/duration
### Main component state
- `downloadJobs: DownloadJob[]` — full job list
- `downloadUrl: string` — URL input state
- `downloadsActiveRef` — guards SSE subscription lifecycle
- `downloadSSERef` — Map<jobId, EventSource> for cleanup
On mount, fetches existing download jobs (in case page was refreshed
mid-download). For each active job, opens an EventSource subscription.
On 'end' event, closes the SSE and refreshes the models list (file
should now be on disk).
### Handlers
- `handleDownload()` — POST /api/models/download, then start SSE
- `handleCancelDownload(id)` — POST /api/models/download/[id]/cancel,
closes SSE, refreshes job list
- `refreshDownloadJobs()` — GET /api/models/download/status, manages
SSE subscriptions based on which jobs are active
## Verification
- `npm run build` — OK, 25 routes (4 new download routes)
- `bash dashboard/validate-build.sh` — all 8 COPY sources ✓, standalone ✓
## After deploy
```sh
git pull && sh build.sh
docker compose up -d --force-recreate dashboard
```
Open Models tab, paste a URL like
'https://huggingface.co/bartowski/Qwen2.5-7B-Instruct-Q8_0.gguf' (or any
direct .gguf link), click Download. Watch progress live, cancel if needed.
File appears in the models list automatically when download completes.
## Files changed
- dashboard/src/lib/downloads.ts — new (download tracker + curl wrapper)
- dashboard/src/app/api/models/download/route.ts — new (POST start)
- dashboard/src/app/api/models/download/status/route.ts — new (GET list)
- dashboard/src/app/api/models/download/[id]/stream/route.ts — new (SSE)
- dashboard/src/app/api/models/download/[id]/cancel/route.ts — new (POST cancel)
- dashboard/Dockerfile — added curl to apk add
- dashboard/src/app/page.tsx — ModelsTab gets Download section,
DownloadJobRow component, DownloadJob type, SSE-based handlers
This commit is contained in:
30
dashboard/src/app/api/models/download/status/route.ts
Normal file
30
dashboard/src/app/api/models/download/status/route.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { listJobs, publicJob } from "@/lib/downloads";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
export const runtime = "nodejs";
|
||||
|
||||
/**
|
||||
* GET /api/models/download/status
|
||||
*
|
||||
* Returns all download jobs (active + recent history). Useful for the
|
||||
* frontend to render a list of in-progress downloads.
|
||||
*
|
||||
* For SSE push on a single job, see /api/models/download/[id]/stream.
|
||||
*
|
||||
* Returns: { jobs: [...], activeCount: N }
|
||||
*/
|
||||
export async function GET(_req: NextRequest) {
|
||||
const jobs = listJobs().map(publicJob);
|
||||
// Sort: active first (by startedAt asc — oldest first), then finished (by finishedAt desc)
|
||||
const active = jobs
|
||||
.filter((j) => j.status === "downloading" || j.status === "queued")
|
||||
.sort((a, b) => a.startedAt - b.startedAt);
|
||||
const finished = jobs
|
||||
.filter((j) => j.status !== "downloading" && j.status !== "queued")
|
||||
.sort((a, b) => (b.finishedAt || 0) - (a.finishedAt || 0));
|
||||
return NextResponse.json({
|
||||
jobs: [...active, ...finished],
|
||||
activeCount: active.length,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user