NGU-Web/src/lib/media.ts
Zaldimmar dad3803ccc Resolve every record image from the uploads folder
A bare image filename, whatever it belongs to (organization and event
logos, people photos, team and award logos, backgrounds, hero photos,
block images), now resolves to /uploads/<file>, in media.ts and for
link previews in pageMeta.js. The per-kind public/ folders were
guesses that don't exist. Only the site's own chosen assets, the NGU
logos in the nav, footer and admin, stay bundled from
src/assets/logos.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 23:29:54 -05:00

109 lines
4.2 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
MEDIA PATHS
Every image field in the API is a filename or a path, and where a
bare filename lives is decided here, in one file.
One rule: record images are uploads. A value from the media
library is already '/uploads/<file>'; a bare filename is a file
copied into the uploads directory by hand (fall-retreat-logo.svg
was), so it resolves to /uploads/<file> too. That covers logos,
photos, backgrounds, hero slides, block images: everything an
editor picks.
The site's own chosen assets (the NGU logos in the nav, footer
and admin) are the exception: they're bundled from
src/assets/logos and imported where they're used, not resolved
here.
A value that already looks like a path or a URL is returned
untouched, so a hand-written entry can point anywhere.
server/src/pageMeta.js resolves images for link previews by the
same rule; keep the two in step.
═══════════════════════════════════════════════════════════════ */
const ABSOLUTE = /^(https?:|\/|data:)/
/* An uploaded file ('/uploads/…'), a path anywhere on the site, or
a full URL: something to use as it is rather than resolve. */
export const isAbsoluteMedia = (file?: string | null) => Boolean(file && ABSOLUTE.test(file))
function inDir(dir: string) {
return (file?: string | null): string | null => {
if (!file) return null
if (ABSOLUTE.test(file)) return file
return `${dir}/${file}`
}
}
export const UPLOADS = '/uploads'
const inUploads = inDir(UPLOADS)
/* One helper per field, all the same rule, so a caller says what the
image is and a future exception is one line here. */
export const personPhoto = inUploads
export const eventLogo = inUploads
export const eventBackground = inUploads
export const orgLogo = inUploads
export const teamLogo = inUploads
export const awardLogo = inUploads
/* front_page_slides.media, the hero's photos. */
export const heroPhoto = inUploads
/* front_page.video_url when it names a file rather than a link.
Videos can't be uploaded through the media library, so a file is
one copied into the uploads directory by hand. */
export const heroVideo = inUploads
/* content_blocks.media and content_block_items.media. */
export const blockMedia = inUploads
/* featured_cards.image. */
export const featuredImage = inUploads
/* blog_posts.cover_image and guest_photo. */
export const blogImage = inUploads
/* ── By record kind ────────────────────────────────────────────
The timeline sends `logo: { file, kind }` rather than a path,
because v_timeline COALESCEs across five tables and only the
ref_kind says which one the filename came from. Featured cards
send the same shape. Every kind resolves to the uploads directory
today; the map is what lets one kind differ later.
───────────────────────────────────────────────────────────── */
const BY_KIND: Record<string, (file?: string | null) => string | null> = {
event: eventLogo,
organization: orgLogo,
team: teamLogo,
award: awardLogo,
person: personPhoto,
// Featured cards (loadFeaturedCards in shape.js) add two of their own.
'event-background': eventBackground,
featured: featuredImage,
}
export function logoForKind(
kind?: string | null,
file?: string | null,
): string | null {
if (!file) return null
// An unrecognised kind still renders: a filename with no home
// directory is a bug, but a broken <img> says so louder than a
// silently absent one.
return (BY_KIND[kind ?? ''] ?? blockMedia)(file)
}
/* Initials for a photo that is missing or fails to load. Same
two-word rule PeopleTiles uses. */
export function initials(name = ''): string {
return name
.trim()
.split(/\s+/)
.slice(0, 2)
.map((word) => word[0] || '')
.join('')
.toUpperCase()
}