Keep uploaded images in folders by what they're for

logos/ for logo and icon fields, thumbnails/ for previews the site
saves (link previews, Drive first pages), images/ for everything else;
each small copy beside its image. Images from before folders are
sorted once at boot by their uses, and every record naming them is
rewritten in one transaction after the files move.

The Media page and image pickers get a tab per folder; a picker opens
on its field's folder and uploads there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 22:29:22 -05:00
parent 4b249c9e52
commit 9854da965e
14 changed files with 283 additions and 51 deletions

View file

@ -2,13 +2,15 @@
MEDIA ROUTES: mounted under /api/admin
GET /media the library, newest first, ?q=name,
?type=image|video|audio.
?type=image|video|audio,
?folder=logos|images|thumbnails.
Picks up images copied into the
uploads folder by hand first
(registerLooseFiles, at most every
SCAN_EVERY_MS)
POST /media upload one image (multipart: file,
keep_original)
keep_original, folder: FOLDERS in
media.js, default images)
GET /media/:id/uses which records name this file
DELETE /media/:id remove the row and the file
POST /link-preview a page's title, description and
@ -29,9 +31,11 @@ import { bodyLimit } from "hono/body-limit";
import { requireAuth, requireRole } from "../auth.js";
import {
FOLDERS,
MAX_UPLOAD_BYTES,
MediaError,
PUBLIC_PREFIX,
publicPath,
registerLooseFiles,
remove,
store,
@ -59,7 +63,8 @@ function shape(row) {
id: row.id,
// A hand-copied file keeps its own name, which may need escaping
// in a URL (a space, say); uploads' random names never do.
url: `${PUBLIC_PREFIX}${encodeURIComponent(row.filename)}`,
url: publicPath(row.filename),
folder: row.filename.includes("/") ? row.filename.split("/")[0] : null,
original_name: row.original_name,
content_type: row.content_type,
bytes: row.bytes,
@ -77,8 +82,8 @@ const SELECT = `SELECT m.*, u.name AS uploaded_by_name, u.email AS uploaded_by_e
/* Writes the file, then its row; a failed insert takes the file
back out so nothing is left on disk that the library can't see. */
export async function addToLibrary(db, buf, { originalName, keepOriginal, userId }) {
const stored = await store(buf, { keepOriginal });
export async function addToLibrary(db, buf, { originalName, keepOriginal, userId, folder }) {
const stored = await store(buf, { keepOriginal, folder });
try {
const { lastInsertRowid } = db
.prepare(
@ -113,7 +118,8 @@ media.get("/media", async (c) => {
// ?type=image (what image fields pick from), video or audio
// (clips saved from Tools → Video clips); none lists everything.
const type = ["image", "video", "audio"].includes(c.req.query("type")) ? c.req.query("type") : null;
const where = [q && "m.original_name LIKE ?", type && "m.content_type LIKE ?"].filter(Boolean);
const folder = FOLDERS.includes(c.req.query("folder")) ? c.req.query("folder") : null;
const where = [q && "m.original_name LIKE ?", type && "m.content_type LIKE ?", folder && "m.filename LIKE ?"].filter(Boolean);
const rows = c
.get("db")
.prepare(
@ -122,7 +128,7 @@ media.get("/media", async (c) => {
ORDER BY m.created_at DESC, m.id DESC
LIMIT ${MAX_LIST}`,
)
.all(...(q ? [`%${q}%`] : []), ...(type ? [`${type}/%`] : []));
.all(...(q ? [`%${q}%`] : []), ...(type ? [`${type}/%`] : []), ...(folder ? [`${folder}/%`] : []));
return c.json({ media: rows.map(shape) }, 200, NO_STORE);
});
@ -151,6 +157,7 @@ media.post(
const row = await addToLibrary(db, Buffer.from(await file.arrayBuffer()), {
originalName: file.name,
keepOriginal: body.keep_original === "1",
folder: body.folder,
userId: user.id,
});
@ -179,6 +186,7 @@ media.post("/link-preview", requireRole("editor"), async (c) => {
try {
const row = await addToLibrary(c.get("db"), await fetchImage(page.image), {
originalName: `preview ${new URL(page.image).hostname}`,
folder: "thumbnails",
keepOriginal: false,
userId: user.id,
});
@ -206,6 +214,7 @@ function usesOf(db, filename) {
return recordsNaming(db, [
`${PUBLIC_PREFIX}${filename}`,
`${PUBLIC_PREFIX}${encodeURIComponent(filename)}`,
publicPath(filename),
filename,
]);
}