Initial commit: playlink music library hardlink deduplicator

Go backend (chi, SQLite) with an embedded React/TypeScript UI that finds
songs present in both a host and a slave music library and replaces the
slave copy with a hardlink to the host copy.

- Path-first matching with format/title/album-folder fallbacks
- Auto-linking of byte-identical files, re-verified with cmp before linking
- Manual review with ffprobe quality comparison, downgrade guard and
  promote-to-host override
- Build mode to populate an empty slave library from the host
- Dry run by default, atomic stage+rename replacement, quarantine with
  retention, undo, path confinement and authenticated UI
- Dockerfile and compose file for Unraid

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cdnutterandClaude Opus 5.5 committed 2026-09-22 15:38:25 -07:00
commit ac47e61b08
42 files changed
+8892

No files matched your search

+221
View File
@@ -0,0 +1,221 @@
import { useEffect, useState } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { AlertTriangle, ChevronLeft, Folder, FolderOpen } from "lucide-react";
import { api, type Pair, type PairInput } from "../api";
import { useInfo } from "../App";
import { Alert, Button, Field, Modal, Toggle, inputCls } from "../ui";
import { useToast } from "../toast";
type Validation = { ok: boolean; error?: string; warnings: string[] };
export function PairForm({ pair, open, onClose }: { pair?: Pair; open: boolean; onClose: () => void }) {
const info = useInfo();
const qc = useQueryClient();
const toast = useToast();
const blank: PairInput = {
name: "Music",
host_root: "",
slave_root: "",
quarantine_dir: "",
extensions: [],
auto_link_identical: false,
scan_interval_minutes: 0,
retention_days: 30,
settle_minutes: 10,
};
const [f, setF] = useState<PairInput>(pair ?? blank);
const [exts, setExts] = useState((pair?.extensions ?? []).join(", "));
const [picker, setPicker] = useState<null | "host_root" | "slave_root" | "quarantine_dir">(null);
const [validation, setValidation] = useState<Validation | null>(null);
useEffect(() => {
if (open) {
setF(pair ?? blank);
setExts((pair?.extensions ?? info.data?.default_extensions ?? []).join(", "));
setValidation(null);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open, pair?.id]);
// Send only editable fields: the API rejects unknown keys.
const body = (): PairInput => ({
name: f.name,
host_root: f.host_root,
slave_root: f.slave_root,
quarantine_dir: f.quarantine_dir,
auto_link_identical: f.auto_link_identical,
scan_interval_minutes: f.scan_interval_minutes,
retention_days: f.retention_days,
settle_minutes: f.settle_minutes,
extensions: exts.split(",").map((s) => s.trim()).filter(Boolean),
});
const set = <K extends keyof PairInput>(k: K, v: PairInput[K]) => {
setF((p) => ({ ...p, [k]: v }));
setValidation(null);
};
const validate = useMutation({
mutationFn: () => api.post<Validation>("/pairs/validate", body()),
onSuccess: setValidation,
});
const save = useMutation({
mutationFn: () => (pair ? api.put<{ warnings: string[] }>(`/pairs/${pair.id}`, body()) : api.post<{ warnings: string[] }>("/pairs", body())),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["pairs"] });
qc.invalidateQueries({ queryKey: ["pair"] });
toast("ok", pair ? "Library pair updated" : "Library pair created. Run a scan to find matches.");
onClose();
},
});
return (
<Modal open={open} onClose={onClose} title={pair ? "Edit library pair" : "New library pair"} wide>
<div className="space-y-4">
<Field label="Name">
<input className={inputCls} value={f.name} onChange={(e) => set("name", e.target.value)} />
</Field>
<div className="grid gap-4 md:grid-cols-2">
<PathField
label="Host library (keeps the files)"
hint="Example: Albums. Its files are the source of every hardlink."
value={f.host_root}
onChange={(v) => set("host_root", v)}
onBrowse={() => setPicker("host_root")}
/>
<PathField
label="Slave library (gets hardlinks)"
hint="Example: your curated Singles library. Matching files here are replaced with hardlinks."
value={f.slave_root}
onChange={(v) => set("slave_root", v)}
onBrowse={() => setPicker("slave_root")}
/>
</div>
<Alert tone="blue">
Both libraries should use the same <code className="mono">Artist/Album/Title.ext</code> layout. Files are matched by relative path first.
Both must be on the same filesystem. On Unraid, mount one common parent folder into the container instead of two separate volumes.
</Alert>
<PathField
label="Quarantine folder"
hint="Originals replaced by a non-identical file are moved here so you can restore them. Default: <slave>/.playlink-trash"
value={f.quarantine_dir}
placeholder="(default)"
onChange={(v) => set("quarantine_dir", v)}
onBrowse={() => setPicker("quarantine_dir")}
/>
<Field label="File extensions" hint="Comma-separated. Other files (covers, lyrics, playlists) are never touched.">
<input className={inputCls} value={exts} onChange={(e) => setExts(e.target.value)} />
</Field>
<div className="grid gap-4 md:grid-cols-3">
<Field label="Scan interval (minutes)" hint="0 = manual only, otherwise at least 5">
<input className={inputCls} type="number" min={0} value={f.scan_interval_minutes} onChange={(e) => set("scan_interval_minutes", Number(e.target.value))} />
</Field>
<Field label="Quarantine retention (days)" hint="0 = keep until purged manually">
<input className={inputCls} type="number" min={0} value={f.retention_days} onChange={(e) => set("retention_days", Number(e.target.value))} />
</Field>
<Field label="Settle time (minutes)" hint="Auto-link skips files modified more recently than this">
<input className={inputCls} type="number" min={0} value={f.settle_minutes} onChange={(e) => set("settle_minutes", Number(e.target.value))} />
</Field>
</div>
<div className="rounded-lg border border-line bg-panel-2 p-3.5">
<Toggle
checked={f.auto_link_identical}
onChange={(v) => set("auto_link_identical", v)}
label={<span className="font-medium">Automatically link byte-identical files after each scan</span>}
/>
<p className="mt-1.5 pl-11.5 text-xs text-muted">
Only files whose relative paths match and whose bytes are identical (verified with a full compare right before linking).
Anything else always waits for manual review. Dry run still applies.
</p>
</div>
{validation && !validation.ok && <Alert tone="red">{validation.error}</Alert>}
{validation?.ok && validation.warnings.length === 0 && <Alert tone="green">Looks good. Both folders are valid and on the same filesystem.</Alert>}
{validation?.warnings.map((w) => (
<Alert key={w} tone="amber" icon={<AlertTriangle className="size-4 shrink-0 mt-0.5" />}>
{w}
</Alert>
))}
{save.error && <Alert tone="red">{save.error.message}</Alert>}
<div className="flex justify-end gap-2 pt-2">
<Button onClick={onClose} variant="ghost">
Cancel
</Button>
<Button onClick={() => validate.mutate()} loading={validate.isPending}>
Validate
</Button>
<Button variant="primary" onClick={() => save.mutate()} loading={save.isPending}>
{pair ? "Save" : "Create"}
</Button>
</div>
</div>
{picker && (
<FolderPicker
start={f[picker] || ""}
onPick={(p) => {
set(picker, p);
setPicker(null);
}}
onClose={() => setPicker(null)}
/>
)}
</Modal>
);
}
function PathField(props: { label: string; hint: string; value: string; placeholder?: string; onChange: (v: string) => void; onBrowse: () => void }) {
return (
<Field label={props.label} hint={props.hint}>
<div className="flex gap-2">
<input className={inputCls + " mono text-xs"} value={props.value} placeholder={props.placeholder ?? "/data/..."} onChange={(e) => props.onChange(e.target.value)} />
<Button type="button" onClick={props.onBrowse} title="Browse">
<FolderOpen className="size-4" />
</Button>
</div>
</Field>
);
}
function FolderPicker({ start, onPick, onClose }: { start: string; onPick: (p: string) => void; onClose: () => void }) {
const [path, setPath] = useState(start);
const q = useQuery({
queryKey: ["browse", path],
queryFn: () => api.get<{ path: string; parent: string; dirs: string[] }>(`/browse?path=${encodeURIComponent(path)}`),
retry: false,
});
return (
<Modal open onClose={onClose} title="Choose folder">
<div className="mb-3 flex items-center gap-2">
<Button size="sm" disabled={!path} onClick={() => setPath(q.data?.parent ?? "")}>
<ChevronLeft className="size-3.5" />
</Button>
<code className="mono truncate text-xs text-muted">{q.data?.path || "Allowed roots"}</code>
</div>
{q.error && (
<Alert tone="red">
{q.error.message}{" "}
<button className="underline cursor-pointer" onClick={() => setPath("")}>
Back to roots
</button>
</Alert>
)}
<div className="max-h-80 overflow-y-auto rounded-lg border border-line">
{q.data?.dirs.map((d) => (
<button key={d} onClick={() => setPath(d)} className="flex w-full items-center gap-2 border-b border-line px-3 py-2 text-left text-sm last:border-0 hover:bg-panel-2 cursor-pointer">
<Folder className="size-4 text-accent" />
<span className="truncate">{q.data.path ? d.split("/").pop() : d}</span>
</button>
))}
{q.data && q.data.dirs.length === 0 && <div className="p-4 text-center text-sm text-muted">No subfolders</div>}
</div>
<div className="mt-4 flex justify-end gap-2">
<Button variant="ghost" onClick={onClose}>
Cancel
</Button>
<Button variant="primary" disabled={!q.data?.path} onClick={() => onPick(q.data!.path)}>
Use this folder
</Button>
</div>
</Modal>
);
}