Add a Featured Content section to the front page
Front page → Featured cards (migration 030) holds cards of three kinds: a record on the site, whose title, picture and link fill in from the record; a link, with a Fetch preview button that reads the page's title, description and preview image into the media library; and a video, drawn as a thumbnail that loads the player when pressed. A 'featured' section draws them. The cards are shared: featured_cards is owned polymorphically and FeaturedCards.tsx draws them, ready for the Community blog. Deleting a record removes its cards; an unpublished record's card is hidden. The preview fetch only goes to public http(s) addresses on standard ports, pinned to the vetted IP, with each redirect checked again. Along the way: repeater fields can show by another field's value (a hidden one is cleared), a CHECK named as a sentence is shown as the error, and the timeline's record list is now record_refs for both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
960c854611
commit
d4f65d93b4
17 changed files with 1140 additions and 50 deletions
|
|
@ -33,11 +33,16 @@
|
|||
event" (National + Retreat skips a sooner regional class). Null
|
||||
means any. They don't apply to a pinned event: the admin chose
|
||||
that one by name.
|
||||
|
||||
── Featured ──
|
||||
The featured band's cards, record cards filled in from their
|
||||
records (loadFeaturedCards in shape.js, which the Community
|
||||
blog will share).
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { Hono } from "hono";
|
||||
|
||||
import { asBool, shapeSeries } from "../shape.js";
|
||||
import { asBool, loadFeaturedCards, shapeSeries } from "../shape.js";
|
||||
|
||||
const home = new Hono();
|
||||
|
||||
|
|
@ -200,6 +205,7 @@ home.get("/front-page", (c) => {
|
|||
sections,
|
||||
stats,
|
||||
paths,
|
||||
featured: loadFeaturedCards(db, "front_page", PAGE_ID),
|
||||
countdown: shapeCountdown(next),
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@
|
|||
keep_original)
|
||||
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
|
||||
preview image, the image saved into
|
||||
the library ({ url } in, JSON body)
|
||||
|
||||
Reading needs a session, uploading needs editor, deleting needs
|
||||
admin: the same ladder the entity routes use, so an editor can
|
||||
|
|
@ -21,6 +24,7 @@ import { bodyLimit } from "hono/body-limit";
|
|||
|
||||
import { requireAuth, requireRole } from "../auth.js";
|
||||
import { MAX_UPLOAD_BYTES, MediaError, PUBLIC_PREFIX, remove, store } from "../media.js";
|
||||
import { fetchImage, previewLink } from "../linkPreview.js";
|
||||
|
||||
const media = new Hono();
|
||||
|
||||
|
|
@ -52,6 +56,34 @@ const SELECT = `SELECT m.*, u.name AS uploaded_by_name, u.email AS uploaded_by_e
|
|||
FROM media m
|
||||
LEFT JOIN admin_users u ON u.id = m.uploaded_by`;
|
||||
|
||||
/* 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. */
|
||||
async function addToLibrary(db, buf, { originalName, keepOriginal, userId }) {
|
||||
const stored = await store(buf, { keepOriginal });
|
||||
try {
|
||||
const { lastInsertRowid } = db
|
||||
.prepare(
|
||||
`INSERT INTO media
|
||||
(filename, original_name, content_type, bytes, width, height, processed, uploaded_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
)
|
||||
.run(
|
||||
stored.filename,
|
||||
(originalName || "upload").slice(0, 200),
|
||||
stored.content_type,
|
||||
stored.bytes,
|
||||
stored.width,
|
||||
stored.height,
|
||||
stored.processed,
|
||||
userId,
|
||||
);
|
||||
return db.prepare(`${SELECT} WHERE m.id = ?`).get(lastInsertRowid);
|
||||
} catch (err) {
|
||||
await remove(stored.filename);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
media.get("/media", (c) => {
|
||||
const q = (c.req.query("q") ?? "").trim().slice(0, 100);
|
||||
const rows = c
|
||||
|
|
@ -88,39 +120,54 @@ media.post(
|
|||
const file = body.file;
|
||||
if (!(file instanceof File)) return c.json({ error: "No file was sent." }, 400);
|
||||
|
||||
const stored = await store(Buffer.from(await file.arrayBuffer()), {
|
||||
const row = await addToLibrary(db, Buffer.from(await file.arrayBuffer()), {
|
||||
originalName: file.name,
|
||||
keepOriginal: body.keep_original === "1",
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
let row;
|
||||
try {
|
||||
const { lastInsertRowid } = db
|
||||
.prepare(
|
||||
`INSERT INTO media
|
||||
(filename, original_name, content_type, bytes, width, height, processed, uploaded_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
)
|
||||
.run(
|
||||
stored.filename,
|
||||
(file.name || "upload").slice(0, 200),
|
||||
stored.content_type,
|
||||
stored.bytes,
|
||||
stored.width,
|
||||
stored.height,
|
||||
stored.processed,
|
||||
user.id,
|
||||
);
|
||||
row = db.prepare(`${SELECT} WHERE m.id = ?`).get(lastInsertRowid);
|
||||
} catch (err) {
|
||||
await remove(stored.filename);
|
||||
throw err;
|
||||
}
|
||||
|
||||
console.log(`media ${stored.filename} uploaded by ${user.email}`);
|
||||
console.log(`media ${row.filename} uploaded by ${user.email}`);
|
||||
return c.json({ media: shape(row) }, 201, NO_STORE);
|
||||
},
|
||||
);
|
||||
|
||||
/* The page's text comes back whatever happens to the image: a site
|
||||
whose preview image is missing or refused still has a title. */
|
||||
media.post("/link-preview", requireRole("editor"), async (c) => {
|
||||
const user = c.get("user");
|
||||
let url;
|
||||
try {
|
||||
({ url } = await c.req.json());
|
||||
} catch {
|
||||
return c.json({ error: "Expected a JSON body." }, 400);
|
||||
}
|
||||
if (typeof url !== "string" || !url.trim()) return c.json({ error: "No link given." }, 400);
|
||||
|
||||
const page = await previewLink(url.trim());
|
||||
|
||||
let image = null;
|
||||
let imageError = null;
|
||||
if (page.image) {
|
||||
try {
|
||||
const row = await addToLibrary(c.get("db"), await fetchImage(page.image), {
|
||||
originalName: `preview ${new URL(page.image).hostname}`,
|
||||
keepOriginal: false,
|
||||
userId: user.id,
|
||||
});
|
||||
image = shape(row);
|
||||
} catch (err) {
|
||||
imageError = err instanceof MediaError ? err.message : "The preview image couldn't be saved.";
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`link preview ${url.trim().slice(0, 200)} by ${user.email}`);
|
||||
return c.json(
|
||||
{ preview: { title: page.title, description: page.description, image, image_error: imageError } },
|
||||
200,
|
||||
NO_STORE,
|
||||
);
|
||||
});
|
||||
|
||||
/* Every TEXT column of every content table, searched for the exact
|
||||
path. Read from the live schema rather than a list of image
|
||||
columns, so a new image field is covered the day it's added. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue