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

@ -52,7 +52,7 @@ import { randomBytes } from "node:crypto";
import { access, constants, copyFile, mkdir, readFile, readdir, rm, stat } from "node:fs/promises";
import path from "node:path";
import { UPLOADS_DIR, PUBLIC_PREFIX } from "./media.js";
import { UPLOADS_DIR, publicPath, storedNameFor } from "./media.js";
export const CLIPS_DIR = path.resolve(process.env.CLIPS_DIR ?? "./clips");
const YTDLP = process.env.YTDLP_PATH ?? "yt-dlp";
@ -548,17 +548,24 @@ export function kick(db = dbRef) {
export const fileOf = (job) => (job?.file ? filePath(job.file) : null);
/* A finished clip, or a whole downloaded video, into the media
library: copied into the uploads directory under a random name and
library: copied into the uploads directory, named for `name` (the
Save to library box; blank: its title and span), and
given a media row. A clip's working file is deleted; a video's is
kept for cutting. Returns the media row's id. */
export async function saveToLibrary(db, job, userId) {
export async function saveToLibrary(db, job, userId, name = null) {
const from = fileOf(job);
if (!from) throw new ClipError(410, "That clip's file is gone. Make it again.");
const audio = job.file.endsWith(".mp3");
const ext = audio ? ".mp3" : ".mp4";
const filename = `${randomBytes(12).toString("hex")}${ext}`;
const to = path.join(UPLOADS_DIR, filename);
const params = JSON.parse(job.params ?? "{}");
const stamp = (s) => `${Math.floor(s / 60)}m${String(Math.floor(s % 60)).padStart(2, "0")}s`;
// A whole download is named for itself; a clip for its span.
const label =
name ??
(job.kind === "source"
? `${job.title ?? "Video"}${ext}`
: `${job.title ?? "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)})${ext}`);
// Each step says what it was doing if it fails, so the page can
// tell an admin (this tool is admin-only) what to fix: usually the
@ -576,6 +583,8 @@ export async function saveToLibrary(db, job, userId) {
};
await step(`making the uploads folder ${UPLOADS_DIR}`, () => mkdir(UPLOADS_DIR, { recursive: true }));
const filename = await step("choosing its file name", () => storedNameFor(label, ext));
const to = path.join(UPLOADS_DIR, filename);
await step(`copying the clip into ${UPLOADS_DIR}`, () => copyFile(from, to));
let id;
@ -584,13 +593,6 @@ export async function saveToLibrary(db, job, userId) {
? { width: null, height: null }
: await step("reading the clip's size", () => probe(from));
const { size } = await stat(from);
const params = JSON.parse(job.params ?? "{}");
const stamp = (s) => `${Math.floor(s / 60)}m${String(Math.floor(s % 60)).padStart(2, "0")}s`;
// A whole download is named for itself; a clip for its span.
const label =
job.kind === "source"
? `${job.title ?? "Video"}${ext}`
: `${job.title ?? "Clip"} (${stamp(params.start ?? 0)} to ${stamp(params.end ?? 0)})${ext}`;
id = await step("adding it to the media table", () =>
Number(
@ -616,7 +618,7 @@ export async function saveToLibrary(db, job, userId) {
await rm(from, { force: true });
update(db, job.id, { saved_media_id: id, file: null });
}
return { id, url: `${PUBLIC_PREFIX}${filename}` };
return { id, url: publicPath(filename) };
}
/* After a clip has gone out in full: its file isn't needed. */