NGU-Web/src/lib/media.ts
Zaldimmar e4a9c96f7e Finish the audit leftovers
- One mediaUrl in media.ts instead of 12 identical aliases and a
  kind map; orgPath builds on hrefs.ts's orgHref
- One Logo component (hides itself or shows a fallback when the file
  is missing) for event cards, org lists and chapter marks
- useHistory is a thin wrapper over useRecord
- adminSchema.ts split into adminSchema/, one file per entity plus
  shared.ts; the registry is unchanged (checked by loading both)
- JS smooth scrolls respect reduced motion (lib/motion.ts)
- Content-Security-Policy in Report-Only, with /api/csp-report logging
  what it would block

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:49:07 -05:00

50 lines
2.1 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:)/
const UPLOADS = '/uploads'
/* Any record image or media field (photos, logos, backgrounds, hero
slides and video files, block and card images, blog covers), and
the timeline's and featured cards' `{ file, kind }` logos by their
file. One rule for all of them; if a kind ever needs its own
directory, that's the time to add a parameter. */
export function mediaUrl(file?: string | null): string | null {
if (!file) return null
if (ABSOLUTE.test(file)) return file
return `${UPLOADS}/${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()
}