Website 1.7.0: move and zoom photos in their frame; delete unused photos

- A section's photo (hero, welcome, invitation, image-text) shows in the
  shape of its frame for the current template and layout, with a Move and
  zoom dialog: drag or arrow keys to choose what stays in view, zoom to
  300%. Saved with the section as data.framing ({ x, y, zoom } by media
  id), a draft until published. The site draws it with object-position
  and scale() around that point; Bold split/collage frames now clip, and
  a zoomed photo stays inside Heritage's mat. Framing for photos a section
  no longer shows is dropped on save.
- Media library: each photo says where it's used; one used nowhere can be
  deleted with its files (DELETE /admin/media/:id, 409 IN_USE otherwise,
  audited as media_deleted).

Checked: Website backend tests (135) and typecheck, frontend typecheck,
lint and production build, and an e2e test of both flows in Chromium.
Lighthouse not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 23:34:10 +02:00
co-authored by Claude Opus 5.5
parent 25100bd749
commit a9215ac4e4
22 changed files with 494 additions and 50 deletions
+57
View File
@@ -147,6 +147,63 @@ export async function processImage(body: Buffer, kind: Kind, basePath: string) {
return { main, width, height, sizes, files };
}
/**
* Where each photo is used, so one in use is never deleted (it would leave a hole on the site):
* the draft pages and their sections, ministries, leaders, series, events, the site icon, the
* live site (the latest published version) and the church's logo. Returns a lookup by media id.
*/
export async function mediaUsage(ctx: Ctx): Promise<(m: { id: string; filename: string }) => string[]> {
const [pages, sections, ministries, leaders, series, events, settings, latest, church] = await Promise.all([
ctx.db.page.findMany({ where: { archivedAt: null } }),
ctx.db.section.findMany(),
ctx.db.ministry.findMany({ where: { archivedAt: null } }),
ctx.db.leader.findMany({ where: { archivedAt: null } }),
ctx.db.messageSeries.findMany(),
ctx.db.simpleEvent.findMany(),
ctx.db.appSetting.findMany(),
ctx.db.siteVersion.findFirst({ orderBy: { number: 'desc' }, select: { snapshot: true } }),
import('../church/profile.js').then((p) => p.churchProfile(ctx)).catch(() => null),
]);
const titles = new Map(pages.map((p) => [p.id, p.title]));
const places: { label: string; text: string }[] = [
...pages.map((p) => ({ label: `${p.title} page`, text: String(p.imageId ?? '') })),
...sections.filter((s) => titles.has(s.pageId)).map((s) => ({ label: `${titles.get(s.pageId)} page`, text: JSON.stringify(s.data ?? null) })),
...ministries.map((m) => ({ label: `Ministry: ${m.name}`, text: String(m.imageId ?? '') })),
...leaders.map((l) => ({ label: `Leader: ${l.name}`, text: String(l.photoId ?? '') })),
...series.map((s) => ({ label: `Series: ${s.title}`, text: String(s.imageId ?? '') })),
...events.map((e) => ({ label: `Event: ${e.title}`, text: String(e.imageId ?? '') })),
...settings.filter((s) => !s.key.startsWith('hub_')).map((s) => ({ label: 'Site settings', text: s.value })),
{ label: 'The live site', text: JSON.stringify(latest?.snapshot ?? null) },
{ label: 'Church logo', text: JSON.stringify(church?.branding ?? null) },
];
return (m) => {
const url = mediaUrl(m.filename);
// A page's sections each name it: list each place once.
return [...new Set(places.filter((p) => p.text.includes(m.id) || p.text.includes(url)).map((p) => p.label))];
};
}
/** Deletes a photo and its files, unless it's used somewhere (then says where). */
export async function deleteMedia(ctx: Ctx, id: string, by: { id: string; role: string }) {
const row = await ctx.db.mediaAsset.findUnique({ where: { id } });
if (!row) throw new ApiError(404, 'NOT_FOUND', 'That image doesn\'t exist.');
const usedIn = (await mediaUsage(ctx))(row);
if (usedIn.length) {
throw new ApiError(409, 'IN_USE', `That photo is still used (${usedIn.join(', ')}). Take it out there first; if it's on the live site, publish after.`);
}
await ctx.db.mediaAsset.delete({ where: { id } });
const dir = uploadDir(ctx);
const sub = path.dirname(row.filename);
const sizes = Array.isArray(row.sizes) ? (row.sizes as Sizes) : [];
const files = [row.filename, ...sizes.flatMap((s) => [path.join(sub, s.webp), path.join(sub, s.jpg)])];
await Promise.all(files.map((f) => {
const full = path.resolve(dir, f);
// Never outside the uploads folder, whatever the row says.
return full.startsWith(dir + path.sep) ? fs.rm(full, { force: true }).catch(() => undefined) : undefined;
}));
await audit(ctx, { actorId: by.id, actorRole: by.role, action: 'media_deleted', targetType: 'media', targetId: id, metadata: { alt: row.alt, bytes: row.bytes } });
}
type StorageAsk = { at: string; steps: number; limitAtRequest: number };
/** The open "Get more storage" request, if any: open until the limit goes up. */