The download/edit child (yt-dlp + ffmpeg) shares one LAN box with the API, Postgres, and Plex playback. A CPU re-encode pegged every core for ~30s and starved web requests + playback until it finished. Full-scope S4 mitigations: - Child self-de-prioritizes at startup (inherited by every ffmpeg/yt-dlp descendant): CPU nice increment + best-effort idle ionice class + an OMP_NUM_THREADS budget, all config-gated and independently disable-able. - Cap the browser-safe re-encode's encoder threads (`-threads`) so a transcode reserves a core for foreground work instead of claiming all of them. - Throttle the retention GC's whole-tree orphan sweep (yield IO after each batch of files) so its rglob can't monopolize disk IO in the API process. - Idle-back-off the Download Center (2s→15s when nothing queued/running) and UsageBar (2s→15s when running_jobs is 0) polls; both snap back to 2s on the next fetch after an enqueue (invalidateDownloads refetches immediately). Tests: encode_thread_cap (pin/reserve-core/edge), -threads flag presence, _deprioritize (nice/ionice/OMP branches), GC sweep throttle batching.
314 lines
12 KiB
TypeScript
314 lines
12 KiB
TypeScript
import { useEffect, useState, type ReactNode } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { CalendarClock, Copy, Link2, Scissors } from "lucide-react";
|
|
import clsx from "clsx";
|
|
import { qk } from "../lib/queryKeys";
|
|
import { useLiveQuery } from "../lib/useLiveQuery";
|
|
import { api, type DownloadJob } from "../lib/api";
|
|
import { notify } from "../lib/notifications";
|
|
import { copyText } from "../lib/clipboard";
|
|
import { formatBytes, formatDuration, formatEta, formatSpeed, relativeTime } from "../lib/format";
|
|
|
|
// Download-row presentation + the small shared primitives (status pill, icon button, usage bar),
|
|
// split out of DownloadCenter (all purely prop-driven, no shared state). Rendered by the queue,
|
|
// library, shared, and admin tabs.
|
|
|
|
const STATUS_CLS: Record<string, string> = {
|
|
queued: "text-muted",
|
|
running: "text-accent",
|
|
paused: "text-warn",
|
|
done: "text-success",
|
|
error: "text-danger",
|
|
canceled: "text-muted",
|
|
};
|
|
|
|
// Byte-progress phases (show a % bar) vs ffmpeg post-steps (no %, indeterminate pulse).
|
|
// "editing" (the phase-2 editor) reports a real % from ffmpeg's out_time, so it gets a bar.
|
|
const DOWNLOAD_PHASES = ["video", "audio", "editing"];
|
|
const PHASE_KEYS = [
|
|
"video",
|
|
"audio",
|
|
"editing",
|
|
"merging",
|
|
"audio_extract",
|
|
"thumbnail",
|
|
"sponsorblock",
|
|
"metadata",
|
|
"processing",
|
|
];
|
|
|
|
export function StatusPill({ job }: { job: DownloadJob }) {
|
|
const { t } = useTranslation();
|
|
// While downloading, show the concrete phase (video / audio / merging) so the user isn't
|
|
// confused by the bar resetting between the separate video and audio streams.
|
|
if (job.status === "running" && job.phase && PHASE_KEYS.includes(job.phase)) {
|
|
return (
|
|
<span className="text-xs font-medium text-accent">{t(`downloads.phase.${job.phase}`)}</span>
|
|
);
|
|
}
|
|
const key = job.expired ? "expired" : job.status;
|
|
return (
|
|
<span className={clsx("text-xs font-medium", STATUS_CLS[job.status] ?? "text-muted")}>
|
|
{t(`downloads.status.${key}`)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
function Thumb({ job }: { job: DownloadJob }) {
|
|
// Prefer OUR self-hosted poster: it's always present on disk, never expires, and is same-origin, so
|
|
// a source-CDN signature expiry (Facebook's fbcdn URLs expire within days) or an ad-blocker/Shields
|
|
// rule on the source domain can't break it. The source's remote thumbnail_url is only a fallback for
|
|
// the rare poster-less asset (a thumbnail-less source whose ffmpeg frame-cut also failed).
|
|
const remote = job.asset?.thumbnail_url ?? null;
|
|
// `job.poster_url` only SIGNALS a poster exists; the actual <img> src must carry ?account= (see
|
|
// api.posterImageUrl). A plain <img> can't send the X-Siftlode-Account header, so without it the
|
|
// poster resolves to the session-default account and 404s for a per-tab account's download — which,
|
|
// now that the poster is the primary image, reads as a blank Library thumbnail.
|
|
const poster = job.poster_url ? api.posterImageUrl(job.id) : null;
|
|
const primary = poster || remote;
|
|
// On load failure (a genuinely missing poster file) fall back to the remote source thumbnail.
|
|
const [src, setSrc] = useState(primary);
|
|
useEffect(() => setSrc(primary), [primary]);
|
|
return (
|
|
<div className="w-28 aspect-video shrink-0 rounded-lg overflow-hidden bg-surface border border-border">
|
|
{src ? (
|
|
<img
|
|
src={src}
|
|
alt=""
|
|
loading="lazy"
|
|
className="w-full h-full object-cover"
|
|
onError={() => {
|
|
if (remote && src !== remote) setSrc(remote);
|
|
}}
|
|
/>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function jobTitle(job: DownloadJob): string {
|
|
return job.display_name || job.asset?.title || job.source_ref;
|
|
}
|
|
|
|
// The channel to show under the title: a user override wins over the auto-extracted asset value.
|
|
// `url` is null for sources without a channel link (e.g. reddit) — then it renders as plain text.
|
|
function jobChannel(job: DownloadJob): { name: string; url: string | null } {
|
|
return {
|
|
name: job.display_uploader || job.asset?.uploader || job.source_ref,
|
|
url: job.display_uploader_url || job.asset?.uploader_url || null,
|
|
};
|
|
}
|
|
|
|
export function IconBtn({
|
|
onClick,
|
|
title,
|
|
danger,
|
|
children,
|
|
}: {
|
|
onClick: () => void;
|
|
title: string;
|
|
danger?: boolean;
|
|
children: ReactNode;
|
|
}) {
|
|
return (
|
|
<button
|
|
onClick={onClick}
|
|
title={title}
|
|
className={clsx(
|
|
"p-1.5 rounded-md hover:bg-surface text-muted transition",
|
|
danger ? "hover:text-danger" : "hover:text-fg",
|
|
)}
|
|
>
|
|
{children}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
// Compact display of a URL: drop the protocol + trailing slash so "youtube.com/watch?v=…" fits.
|
|
function displayUrl(url: string): string {
|
|
try {
|
|
const u = new URL(url);
|
|
return (u.host + u.pathname + u.search).replace(/\/$/, "");
|
|
} catch {
|
|
return url;
|
|
}
|
|
}
|
|
|
|
// "Downloaded from" reference: opens the original page in a new tab, or copies the link.
|
|
function SourceRef({ url }: { url: string }) {
|
|
const { t } = useTranslation();
|
|
const copy = async () => {
|
|
if (await copyText(url)) {
|
|
notify({ level: "success", message: t("downloads.source.copied") });
|
|
} else {
|
|
notify({ level: "error", message: t("downloads.source.copyFailed") });
|
|
}
|
|
};
|
|
return (
|
|
<div className="flex items-center gap-1 mt-1 text-xs text-muted min-w-0">
|
|
<Link2 className="w-3 h-3 shrink-0" />
|
|
<a
|
|
href={url}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
title={`${t("downloads.source.open")} — ${url}`}
|
|
className="truncate hover:text-accent hover:underline"
|
|
>
|
|
{displayUrl(url)}
|
|
</a>
|
|
<button
|
|
onClick={copy}
|
|
aria-label={t("downloads.source.copy")}
|
|
title={t("downloads.source.copy")}
|
|
className="shrink-0 p-0.5 rounded-sm hover:bg-surface hover:text-fg transition"
|
|
>
|
|
<Copy className="w-3 h-3" />
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// Channel under the title — a clickable link when a channel URL is known, plain text otherwise.
|
|
function ChannelLine({ job }: { job: DownloadJob }) {
|
|
const { name, url } = jobChannel(job);
|
|
if (url) {
|
|
return (
|
|
<div className="text-xs mt-0.5 truncate">
|
|
<a
|
|
href={url}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
title={url}
|
|
className="text-muted hover:text-accent hover:underline"
|
|
>
|
|
{name}
|
|
</a>
|
|
</div>
|
|
);
|
|
}
|
|
return <div className="text-xs text-muted truncate mt-0.5">{name}</div>;
|
|
}
|
|
|
|
export function DownloadRow({
|
|
job,
|
|
onShowError,
|
|
children,
|
|
}: {
|
|
job: DownloadJob;
|
|
onShowError?: (job: DownloadJob) => void;
|
|
children?: ReactNode;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const running = job.status === "running";
|
|
return (
|
|
<div className="flex gap-3 p-2.5 rounded-xl glass-card glass-hover transition">
|
|
<Thumb job={job} />
|
|
<div className="min-w-0 flex-1">
|
|
<div className="font-medium leading-snug line-clamp-1 flex items-center gap-1.5">
|
|
{job.job_kind === "edit" && (
|
|
<span className="inline-flex items-center gap-1 shrink-0 px-1.5 py-0.5 rounded-sm text-3xs font-semibold uppercase tracking-wide bg-accent/15 text-accent">
|
|
<Scissors className="w-3 h-3" /> {t("downloads.clipBadge")}
|
|
</span>
|
|
)}
|
|
<span className="truncate">{jobTitle(job)}</span>
|
|
</div>
|
|
<ChannelLine job={job} />
|
|
<div className="flex items-center gap-2 mt-1 text-xs">
|
|
<StatusPill job={job} />
|
|
{job.asset?.size_bytes ? (
|
|
<span className="text-muted">· {formatBytes(job.asset.size_bytes)}</span>
|
|
) : null}
|
|
{job.asset?.duration_s ? (
|
|
<span className="text-muted">· {formatDuration(job.asset.duration_s)}</span>
|
|
) : null}
|
|
{job.status === "done" && job.created_at ? (
|
|
<span
|
|
className="text-muted inline-flex items-center gap-1"
|
|
title={new Date(job.created_at).toLocaleString()}
|
|
>
|
|
· <CalendarClock className="w-3 h-3" /> {relativeTime(job.created_at)}
|
|
</span>
|
|
) : null}
|
|
{job.status === "error" ? (
|
|
<button
|
|
onClick={() => onShowError?.(job)}
|
|
className="text-danger hover:opacity-80 inline-flex items-center gap-1 truncate"
|
|
title={t("downloads.errors.fail.title")}
|
|
>
|
|
· {t(`downloads.errors.fail.cat.${job.error_code || "unknown"}.short`)}
|
|
<span className="text-muted underline">{t("downloads.errors.fail.detailsLink")}</span>
|
|
</button>
|
|
) : null}
|
|
</div>
|
|
{job.source_url && <SourceRef url={job.source_url} />}
|
|
{job.extra_links?.map((url) => (
|
|
<SourceRef key={url} url={url} />
|
|
))}
|
|
{running && (
|
|
<div className="mt-1.5">
|
|
{job.phase && !DOWNLOAD_PHASES.includes(job.phase) ? (
|
|
// ffmpeg post-steps aren't byte-progress — show an indeterminate pulse, no %.
|
|
<div className="h-1.5 rounded-full bg-surface overflow-hidden">
|
|
<div className="h-full w-full bg-accent/60 animate-pulse" />
|
|
</div>
|
|
) : (
|
|
<>
|
|
<div className="h-1.5 rounded-full bg-surface overflow-hidden">
|
|
<div
|
|
className="h-full bg-accent transition-all"
|
|
style={{ width: `${job.progress}%` }}
|
|
/>
|
|
</div>
|
|
<div className="text-2xs text-muted mt-0.5 flex gap-2">
|
|
<span>{job.progress}%</span>
|
|
{job.speed_bps ? <span>{formatSpeed(job.speed_bps)}</span> : null}
|
|
{job.eta_s ? <span>{formatEta(job.eta_s)}</span> : null}
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div className="flex items-start gap-0.5 shrink-0">{children}</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function UsageBar() {
|
|
const { t } = useTranslation();
|
|
// Poll live (like the library list) so the footprint updates the moment the worker finishes an
|
|
// edit/download — enqueue only creates a queued job (0 bytes), the size lands asynchronously, so
|
|
// a one-shot fetch at enqueue time would show a stale 0 B until a manual reload. The footprint
|
|
// only moves while a job is in flight, so poll fast (2s) then and idle-back-off to 15s otherwise
|
|
// (running_jobs counts queued+running — it flips back to 2s on the first poll after a new enqueue).
|
|
const usage = useLiveQuery(qk.downloadUsage(), api.downloadUsage, {
|
|
intervalMs: (u) => (u?.running_jobs ? 2000 : 15000),
|
|
});
|
|
const u = usage.data;
|
|
if (!u) return null;
|
|
const pct =
|
|
u.unlimited || !u.max_bytes ? 0 : Math.min(100, (u.footprint_bytes / u.max_bytes) * 100);
|
|
return (
|
|
<div className="rounded-xl glass-card p-3 mb-4">
|
|
<div className="flex justify-between text-sm mb-1.5">
|
|
<span className="font-medium">{t("downloads.usage.title")}</span>
|
|
<span className="text-muted">
|
|
{formatBytes(u.footprint_bytes)}
|
|
{u.unlimited ? "" : ` / ${formatBytes(u.max_bytes)}`} ·{" "}
|
|
{u.unlimited
|
|
? t("downloads.usage.unlimited")
|
|
: t("downloads.usage.items", { used: u.active_jobs, max: u.max_jobs })}
|
|
</span>
|
|
</div>
|
|
{!u.unlimited && (
|
|
<div className="h-2 rounded-full bg-surface overflow-hidden">
|
|
<div
|
|
className={clsx("h-full transition-all", pct > 90 ? "bg-danger" : "bg-accent")}
|
|
style={{ width: `${pct}%` }}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|