Cleanup of old-data fixes; an editor for the Organization page
Nothing before these changes was ever public, so the fixes kept for older data go: the /leadership redirect (MOVED in routes/pages.js), index.html's move of "ngu-" browser keys to "site-", the boot-time sorting of images from before folders (media.js), and emoji path icons (a path with no named icon shows a dot; migration 055 turns the baseline's seeded emoji into their names). Pages → Organization page (migration 055): a singleton with the Mission, Vision & Values blocks, the Leadership blocks (a People block per team, in order) with a heading and blurb, and Volunteering (title, blurb, links). content_blocks gains the two owner kinds; GET /organization-page serves it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1da79c128b
commit
43797f38bc
14 changed files with 308 additions and 127 deletions
|
|
@ -36,8 +36,7 @@
|
|||
by itself (link previews, Drive files' first pages), images/ for
|
||||
the rest. A stored name includes it: 'logos/3f9c….webp', served at
|
||||
/uploads/logos/3f9c….webp. The small copy sits beside it. Clips
|
||||
and files from before folders keep a bare name in the root.
|
||||
sortIntoFolders() moves those older images once, at boot.
|
||||
keep a bare name in the root.
|
||||
|
||||
── Files copied in by hand ──
|
||||
A file put straight into UPLOADS_DIR has no media row, so the
|
||||
|
|
@ -51,7 +50,6 @@
|
|||
|
||||
import { randomBytes } from "node:crypto";
|
||||
|
||||
import { tx } from "./db.js";
|
||||
import { mkdir, readFile, readdir, rename, stat, unlink, writeFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
|
|
@ -373,83 +371,4 @@ export async function registerLooseFiles(db) {
|
|||
|
||||
// Columns that hold a logo or a site icon: an image one of these
|
||||
// names goes in logos/.
|
||||
const LOGO_COLUMNS = new Set(["logo", "org_logo", "event_logo", "favicon", "apple_touch_icon"]);
|
||||
// Tables whose text never names an upload a page uses.
|
||||
const NOT_CONTENT = new Set(["media", "admin_users", "sessions", "feedback", "feedback_files", "sqlite_sequence"]);
|
||||
|
||||
/* Every image still in the root from before folders, moved into the
|
||||
folder its uses call for (logos/ if a logo or icon field names it,
|
||||
thumbnails/ if the site saved it as a preview, else images/), with
|
||||
every record that names it rewritten to the new path. The files
|
||||
move first; the rows change in one transaction, and if that fails
|
||||
the files move back. Returns how many moved; run at boot, and a
|
||||
no-op once nothing is left in the root. */
|
||||
export async function sortIntoFolders(db) {
|
||||
const rows = db
|
||||
.prepare(
|
||||
`SELECT id, filename, original_name FROM media
|
||||
WHERE instr(filename, '/') = 0 AND content_type LIKE 'image/%'`,
|
||||
)
|
||||
.all();
|
||||
if (rows.length === 0) return 0;
|
||||
|
||||
const columns = [];
|
||||
for (const { name: table } of db
|
||||
.prepare(`SELECT name FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`)
|
||||
.all()) {
|
||||
if (NOT_CONTENT.has(table)) continue;
|
||||
for (const col of db.prepare(`SELECT name, type FROM pragma_table_info(?)`).all(table)) {
|
||||
if (col.type.toUpperCase() === "TEXT") columns.push({ table, column: col.name });
|
||||
}
|
||||
}
|
||||
const namesOf = (filename) => [
|
||||
...new Set([`${PUBLIC_PREFIX}${filename}`, `${PUBLIC_PREFIX}${encodeURIComponent(filename)}`, filename]),
|
||||
];
|
||||
const holds = (table, column, values) =>
|
||||
db
|
||||
.prepare(`SELECT 1 FROM "${table}" WHERE "${column}" IN (${values.map(() => "?").join(", ")}) LIMIT 1`)
|
||||
.get(...values);
|
||||
|
||||
const moved = [];
|
||||
for (const row of rows) {
|
||||
const values = namesOf(row.filename);
|
||||
const folder = columns.some(({ table, column }) => LOGO_COLUMNS.has(column) && holds(table, column, values))
|
||||
? "logos"
|
||||
: /^preview |\(preview\)$/.test(row.original_name) || holds("drive_files", "preview", values)
|
||||
? "thumbnails"
|
||||
: "images";
|
||||
const to = `${folder}/${row.filename}`;
|
||||
try {
|
||||
await mkdir(path.join(UPLOADS_DIR, folder), { recursive: true });
|
||||
await rename(path.join(UPLOADS_DIR, row.filename), path.join(UPLOADS_DIR, to));
|
||||
} catch (err) {
|
||||
// Not on disk (deleted by hand): leave its row as it is.
|
||||
if (err.code !== "ENOENT") console.warn(`media: couldn't move ${row.filename}: ${err.message}`);
|
||||
continue;
|
||||
}
|
||||
await rename(path.join(UPLOADS_DIR, thumbName(row.filename)), path.join(UPLOADS_DIR, thumbName(to))).catch(() => {});
|
||||
moved.push({ row, to, values });
|
||||
}
|
||||
|
||||
try {
|
||||
tx(db, () => {
|
||||
for (const { row, to, values } of moved) {
|
||||
db.prepare(`UPDATE media SET filename = ? WHERE id = ?`).run(to, row.id);
|
||||
for (const { table, column } of columns) {
|
||||
db.prepare(`UPDATE "${table}" SET "${column}" = ? WHERE "${column}" IN (${values.map(() => "?").join(", ")})`).run(
|
||||
publicPath(to),
|
||||
...values,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
for (const { row, to } of moved) {
|
||||
await rename(path.join(UPLOADS_DIR, to), path.join(UPLOADS_DIR, row.filename)).catch(() => {});
|
||||
await rename(path.join(UPLOADS_DIR, thumbName(to)), path.join(UPLOADS_DIR, thumbName(row.filename))).catch(() => {});
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
return moved.length;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue