download job: collapse to one phase (no metadata prefetch)
The job's two phases were a misread — the "count" phase is the client-side
playlist expansion (for the inline-vs-job decision, already done in the panel).
The job itself is just one download request per item.
Dropped the in-job metadata pass entirely:
- reclip-client: reclipDownloadOne no longer prefetches /api/info for a title —
ReClip names the file from the video title itself, so it's a single request
per item.
- execute-download: one phase — expand the playlist, then /api/download each url,
skip failures. Progress is a single { done, failed, total, current } counter
(no meta/dl split); ~2× faster and downloads start right after expansion.
- UI (DownloadJobDetail + panel JobView): one "Downloaded" bar instead of two.
Verified: every item is attempted directly (no /api/info gate), skip-on-error
counts correct.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
import { reclipInfo, reclipPlaylist, reclipDownloadOne, type ReclipInfo } from '../../reclip-client';
|
||||
import { reclipPlaylist, reclipDownloadOne } from '../../reclip-client';
|
||||
|
||||
// The download-job executor — pure scripting, no agent. Two phases:
|
||||
// 1. metadata — expand the playlist, fetch each item's info sequentially, keep the ones that resolve
|
||||
// (skip the errors: private/deleted/unavailable).
|
||||
// 2. download — download every survivor in the chosen format (audio/video); skip anything that fails.
|
||||
// Emits a compact `download:progress` snapshot (counters, not per-item events — a playlist can be
|
||||
// thousands of items). Throws on abort or a fatal error (playlist expansion) so the job manager marks it
|
||||
// stopped/failed; per-item errors are counted and skipped, never fatal.
|
||||
// The download-job executor — pure scripting, no agent. One phase: expand the playlist, then one download
|
||||
// request per item in the chosen format (audio/video), skipping anything that fails (private / deleted /
|
||||
// download error). No metadata prefetch — ReClip names the file from the video title itself. Emits a
|
||||
// compact `download:progress` snapshot (counters, not per-item events — a playlist can be thousands of
|
||||
// items). Throws on abort or a fatal error (playlist expansion); per-item errors are counted + skipped.
|
||||
|
||||
type Counts = { done: number; failed: number; total: number };
|
||||
export type DownloadProgress = {
|
||||
phase: 'expanding' | 'metadata' | 'download' | 'done';
|
||||
meta: Counts;
|
||||
dl: Counts;
|
||||
current?: string; // title of the item currently downloading
|
||||
phase: 'expanding' | 'download' | 'done';
|
||||
done: number; // downloaded successfully
|
||||
failed: number; // skipped (unavailable / download error)
|
||||
total: number;
|
||||
current?: string; // url of the item currently downloading
|
||||
};
|
||||
|
||||
export type DownloadEvent = { type: 'download:progress'; progress: DownloadProgress };
|
||||
@@ -36,20 +34,13 @@ export async function executeDownload(params: ExecuteDownloadParams): Promise<vo
|
||||
if (!url || !absDir) throw new Error('download job missing url or target directory');
|
||||
const audioOnly = format !== 'video'; // default to audio
|
||||
|
||||
const progress: DownloadProgress = {
|
||||
phase: 'expanding',
|
||||
meta: { done: 0, failed: 0, total: 0 },
|
||||
dl: { done: 0, failed: 0, total: 0 },
|
||||
};
|
||||
const progress: DownloadProgress = { phase: 'expanding', done: 0, failed: 0, total: 0 };
|
||||
let lastEmit = 0;
|
||||
const emit = (force = false) => {
|
||||
const now = Date.now();
|
||||
if (!force && now - lastEmit < EMIT_THROTTLE_MS) return;
|
||||
lastEmit = now;
|
||||
params.emit({
|
||||
type: 'download:progress',
|
||||
progress: { ...progress, meta: { ...progress.meta }, dl: { ...progress.dl } },
|
||||
});
|
||||
params.emit({ type: 'download:progress', progress: { ...progress } });
|
||||
};
|
||||
const checkAbort = () => {
|
||||
if (params.abortSignal.aborted) throw new Error('aborted');
|
||||
@@ -65,44 +56,20 @@ export async function executeDownload(params: ExecuteDownloadParams): Promise<vo
|
||||
if (pl.urls?.length) urls = pl.urls;
|
||||
}
|
||||
|
||||
// ── Phase 1: metadata (keep survivors) ──
|
||||
progress.phase = 'metadata';
|
||||
progress.meta.total = urls.length;
|
||||
// ── Download: one request per item, skip failures ──
|
||||
progress.phase = 'download';
|
||||
progress.total = urls.length;
|
||||
emit(true);
|
||||
const valid: Array<{ url: string; title: string }> = [];
|
||||
for (const u of urls) {
|
||||
checkAbort();
|
||||
const info = await reclipInfo(u).catch((): ReclipInfo => ({ error: 'fetch failed' }));
|
||||
if (info && !info.error) {
|
||||
valid.push({ url: u, title: info.title ?? '' });
|
||||
progress.meta.done++; // done = kept; failed = skipped (both phases read `(done+failed)/total`)
|
||||
} else {
|
||||
progress.meta.failed++;
|
||||
}
|
||||
emit();
|
||||
}
|
||||
emit(true);
|
||||
|
||||
// ── Phase 2: download survivors ──
|
||||
progress.phase = 'download';
|
||||
progress.dl.total = valid.length;
|
||||
emit(true);
|
||||
for (const item of valid) {
|
||||
checkAbort();
|
||||
progress.current = item.title || item.url;
|
||||
progress.current = u;
|
||||
emit(true);
|
||||
try {
|
||||
await reclipDownloadOne({
|
||||
url: item.url,
|
||||
destDir: absDir,
|
||||
audioOnly,
|
||||
title: item.title,
|
||||
signal: params.abortSignal,
|
||||
});
|
||||
progress.dl.done++;
|
||||
await reclipDownloadOne({ url: u, destDir: absDir, audioOnly, signal: params.abortSignal });
|
||||
progress.done++;
|
||||
} catch {
|
||||
checkAbort(); // an abort surfaces as a throw here — re-check so it stops instead of counting as a skip
|
||||
progress.dl.failed++;
|
||||
progress.failed++;
|
||||
}
|
||||
emit();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user