Let the clip tool save a clip to a Drive folder

Save to Drive, beside Save to library, puts a finished clip in a folder
under Media (Videos or Audio chosen first to match it) as a Files
entry, shared by link, so pages can link to it (migration 051). The
clip streams from disk through a resumable upload, so a long one
isn't read into memory. A clip can be in both the library and Drive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 23:29:28 -05:00
parent e1542d8d87
commit 42d1536ee6
6 changed files with 219 additions and 17 deletions

View file

@ -38,6 +38,8 @@ type Job = {
has_file: boolean;
saved_media_id: number | null;
saved_url: string | null;
/** In Drive, under Media (Save to Drive): its Files entry. */
saved_drive: { id: number; url: string; folder: string } | null;
downloaded_at: string | null;
};
@ -87,6 +89,80 @@ const primary =
const input =
"rounded-lg border border-muted/25 bg-surface px-3 py-2 text-sm text-body outline-none focus:border-brand focus:ring-2 focus:ring-brand/25";
/* Save to Drive: a folder under Media (Tools → Media → Files), then
the clip goes there as a Files entry. Opens on a folder named for
the clip's kind (Videos, Audio) when there is one. */
function SaveToDrive({ clip, onSaved }: { clip: Job; onSaved: () => void }) {
const [folders, setFolders] = useState<string[] | null>(null);
const [folder, setFolder] = useState("");
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const audio = clip.params?.format === "audio";
async function open() {
setError(null);
try {
const data = await get("/admin/files", { ttl: 0 });
if (!data.ready) return setError("Google Drive isn't set up on the server.");
const list: string[] = data.folders ?? [];
setFolders(list);
const wanted = audio ? /^audio$/i : /^videos?$/i;
setFolder(list.find((f) => wanted.test(f)) ?? "");
} catch (err) {
setError(err instanceof ApiError ? err.message : "Couldn't load the folders.");
}
}
async function save() {
setBusy(true);
setError(null);
try {
await post(`/admin/clips/${clip.id}/drive`, { folder });
onSaved();
} catch (err) {
setError(err instanceof ApiError ? err.message : "Couldn't save it to Drive.");
setBusy(false);
}
}
return (
<>
{folders === null ? (
<button type="button" className={button} onClick={open}>
Save to Drive
</button>
) : (
<span className="flex flex-wrap items-center gap-2">
<label className="sr-only" htmlFor={`drive-folder-${clip.id}`}>
Folder in Drive
</label>
<select
id={`drive-folder-${clip.id}`}
value={folder}
disabled={busy}
onChange={(e) => setFolder(e.target.value)}
className="rounded-lg border border-muted/25 bg-surface px-2 py-1 text-sm text-body"
>
<option value="">Media (not in a folder)</option>
{folders.map((f) => (
<option key={f} value={f}>
Media/{f}
</option>
))}
</select>
<button type="button" className={button} disabled={busy} onClick={save}>
{busy ? "Uploading…" : "Save"}
</button>
<button type="button" className="text-sm" style={{ color: MUTED }} onClick={() => setFolders(null)}>
Cancel
</button>
</span>
)}
{error && <span className="text-sm text-danger">{error}</span>}
</>
);
}
function Card({ title, children }: { title: string; children: ReactNode }) {
return (
<section className="mt-6 rounded-2xl border border-brand/20 bg-surface p-5">
@ -484,7 +560,15 @@ export default function AdminClips() {
In the media library
</Link>
)}
{clip.status === "done" && !clip.has_file && !clip.saved_url && (
{clip.status === "done" && !clip.saved_drive && (clip.has_file || clip.saved_url) && (
<SaveToDrive clip={clip} onSaved={load} />
)}
{clip.saved_drive && (
<a href={clip.saved_drive.url} target="_blank" rel="noreferrer" className="text-sm underline" style={{ color: accentText(BRAND) }}>
In Drive: Media{clip.saved_drive.folder ? `/${clip.saved_drive.folder}` : ""}
</a>
)}
{clip.status === "done" && !clip.has_file && !clip.saved_url && !clip.saved_drive && (
<span className="text-sm" style={{ color: MUTED }}>
Downloaded, and deleted from the server
</span>
@ -505,7 +589,7 @@ export default function AdminClips() {
))}
</ul>
<p className="mt-3 text-xs" style={{ color: MUTED }}>
A clip is deleted from the server once it's downloaded, unless it's saved to the library first.
A clip is deleted from the server once it's downloaded, unless it's saved to the library first. Save to Drive puts a copy in a folder under Media in the shared Drive, as a Files entry pages can link to.
Everything here is cleared a day after it's made.
</p>
</Card>