Media: name files when saving, and rename them in the library (file and references)

Every upload and save (Upload, Frame at, Video thumbnails, a clip's
Save to library) has a prefilled Name box; the name is also the
readable part of the file's name, plus a short random ending. Fetch
preview names its picture after the page. Rename on the Media page
changes both, moving the file and its small copy and pointing every
record that names it at the new path in one transaction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aidan Dona 2026-10-08 15:47:30 -05:00
parent 14a22ca44d
commit 687ba898f6
9 changed files with 475 additions and 74 deletions

View file

@ -17,7 +17,7 @@
a page (an image field stores the path, not a foreign key).
═══════════════════════════════════════════════════════════════ */
import { useEffect, useState } from "react";
import { useEffect, useState, type FormEvent } from "react";
import { Link } from "react-router-dom";
import { ApiError, del, get, patch, post } from "../../lib/api.ts";
@ -38,6 +38,7 @@ import {
FolderTabs,
formatBytes,
MediaGrid,
NameBox,
UPLOAD_HELP,
UploadButton,
useMediaLibrary,
@ -131,6 +132,73 @@ function DeleteControl({
);
}
/* Rename: the library's name and the file's own. Opening it asks what
uses the file first, since the server repoints those records at the
new address and anything outside the site (a sent newsletter, a
shared link) keeps the old one, which stops working. */
function RenameControl({ item, onRenamed }: { item: MediaItem; onRenamed: (item: MediaItem) => void }) {
const [name, setName] = useState<string | null>(null);
const [uses, setUses] = useState<Use[] | null>(null);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
async function open() {
setName(item.original_name);
setError(null);
setUses(null);
try {
setUses((await get(`/admin/media/${item.id}/uses`, { ttl: 0 })).uses);
} catch {
setUses([]);
}
}
async function save(e: FormEvent) {
e.preventDefault();
if (!name?.trim()) return;
setBusy(true);
setError(null);
try {
const { media } = await patch(`/admin/media/${item.id}`, { name });
onRenamed(media);
setName(null);
} catch (err) {
setError(err instanceof ApiError ? err.message : "Couldn't rename that.");
} finally {
setBusy(false);
}
}
if (name === null) {
return (
<button type="button" onClick={open} className="text-xs text-link hover:underline">
Rename
</button>
);
}
const count = uses?.reduce((sum, use) => sum + use.count, 0) ?? 0;
return (
<form onSubmit={save} className="w-full space-y-2 bg-tint p-2 text-xs text-body">
<NameBox value={name} onChange={setName} />
<p className="text-muted">
{uses === null
? "Checking what uses it…"
: `Also renames the file${count > 0 ? `, and updates the ${count} record${count === 1 ? "" : "s"} using it` : ""}. Links to the old address outside the site (eg. a sent newsletter) will stop working.`}
</p>
<div className="flex gap-3">
<button type="submit" disabled={busy || !name.trim()} className="font-medium text-link hover:underline disabled:opacity-50">
{busy ? "Renaming…" : "Save name"}
</button>
<button type="button" onClick={() => setName(null)} className="text-muted hover:underline">
Cancel
</button>
</div>
{error && <p className="text-danger">{error}</p>}
</form>
);
}
function CopyPath({ url, label = "Copy path" }: { url: string; label?: string }) {
const [copied, setCopied] = useState(false);
return (
@ -573,6 +641,14 @@ export default function AdminMedia() {
>
Open
</a>
{roleCanWrite(user) && (
<RenameControl
item={item}
onRenamed={(renamed) =>
setItems((current) => (current ?? []).map((m) => (m.id === renamed.id ? renamed : m)))
}
/>
)}
{canDelete(user) && (
<div className="w-full">
<DeleteControl