NGU-Web/docs/media-uploads.md
Aidan Dona 687ba898f6 Media: name files when saving, and rename them in the library (file and references)
Every upload and save (Upload, Frame at, Video thumbnails, a clip's
Save to library) has a prefilled Name box; the name is also the
readable part of the file's name, plus a short random ending. Fetch
preview names its picture after the page. Rename on the Media page
changes both, moving the file and its small copy and pointing every
record that names it at the new path in one transaction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 15:47:30 -05:00

139 lines
6.2 KiB
Markdown

# Media uploads
Images uploaded in the admin (Media page, or the Upload and Choose
from library buttons on any image field) are stored on disk and listed
in the `media` table (`server/src/migrations/028_media.sql`).
## How files are stored
- An image field stores the file's public path, `/uploads/<file>`. It
still accepts a bare filename in that field's `public/` directory or a
full URL, as before.
- By default a photo is turned upright, shrunk to fit 2400px on its
longest side, and saved as WebP. This also strips its metadata,
camera location included. Limit: 25 MB per file.
- **Keep original** stores the file exactly as uploaded (up to 10 MB),
metadata and all.
- SVGs are always kept as they are.
- A photo field with a shape (people, event photos and card
backgrounds, blog covers, hero photos, link card pictures, block
images and schedule graphics; not logos) opens **Frame the photo**
before uploading a JPEG, PNG or WebP: drag and zoom it inside the
field's shape, or pick another (As uploaded keeps the photo's own).
The framed part is what's uploaded; nothing about the framing is
saved, so framing again means uploading again. Upload as is skips
it. A field's shape is its `crop` option (`src/components/admin/fields.tsx`).
- Accepted types: JPEG, PNG, GIF, WebP, SVG, and WebVTT captions
(`.vtt`, kept as they are, always in `captions/`). The type is read
from the file's contents, not its name: a captions file has to start
with `WEBVTT`.
- Every upload and save asks for a **Name** (prefilled: the file's own
name, "Video frame at 1:23", a clip's title and span). It's the
library's name and the readable part of the file's, plus a short
random ending: `images/spring-retreat-group-3f9c1a.webp`
(`slugify` and `storedNameFor` in `server/src/media.js`). Fetch preview
names its picture after the page's title.
- **Rename** (Media page, editors and up) changes both. The file and its
small copy move to a new name, and every record that names the file is
pointed at it in the same step (the same search the delete check
uses). Anything outside the site still holding the old address (a
sent newsletter, a shared link) stops working. A rename that only
changes capitals or punctuation keeps the file where it is.
- Editors and up can upload. Admins and up can delete. The Media page
shows which tables still use a file before deleting it, because
nothing stops a file a record uses from being deleted.
Limits and sizes are constants at the top of `server/src/media.js`.
## Folders
Images are kept in folders by what they're for (`FOLDERS` in
`server/src/media.js`), and the paths records store include them:
| Folder | What goes in it |
| --- | --- |
| `logos/` | Uploads from logo and icon fields (organization, team, award and event logos, the favicon and home-screen icon): the fields with `folder: "logos"` in `src/lib/adminSchema/` |
| `images/` | Every other upload: photos, backgrounds, covers, block images |
| `thumbnails/` | Pictures the site saves by itself: link previews (Fetch preview) and Drive files' first pages |
| `captions/` | WebVTT files for uploaded videos: the Captions field of video blocks and the hero's Video captions (migration 066). YouTube, Vimeo and Facebook videos show their own |
Each image's `-640.webp` small copy sits beside it. Video and audio
clips (Tools → Video clips) stay in the root. The Media page and every
image picker have a tab per folder; a picker opens on its field's
folder, and uploads there.
nginx needs no change: `location /uploads/` already serves the folders.
## Files copied in by hand
A file put straight into the uploads directory, or into one of its
folders (with `scp`, say), shows up in the library the next time the Media page or an image picker opens:
the server registers any image it finds there without a record, under
the file's own name, kept as it is (not resized). Anything that isn't a
JPEG, PNG, GIF, WebP or SVG is ignored. A record can name such a file by
its bare filename (eg. `fall-retreat-logo.svg`), which resolves to
`/uploads/<file>`, and the delete check counts those uses too. Keep the
files readable by the `ngu-api` user.
## Local development
The API reads `UPLOADS_DIR` (default `./uploads`, so `server/uploads/`,
which git ignores). The API serves `/uploads/` itself, and Vite proxies
`/uploads` to the same target as `/api`.
## Production
1. Create the directory, owned by the service user:
```sh
sudo mkdir -p /var/lib/ngu/uploads
sudo chown <ngu-api user>: /var/lib/ngu/uploads
```
2. Set it in the `ngu-api` unit (`systemctl edit ngu-api`):
```ini
[Service]
Environment=UPLOADS_DIR=/var/lib/ngu/uploads
```
It lives outside `/srv/ngu-api` so an rsync deploy never deletes it.
Back it up alongside the database.
3. In the nginx site, let uploads through and serve them from disk:
```nginx
# In the server block. nginx's default is 1 MB.
client_max_body_size 26m;
location /uploads/ {
alias /var/lib/ngu/uploads/;
expires 30d;
add_header Cache-Control "public, immutable";
add_header X-Content-Type-Options nosniff;
# An uploaded SVG opened on its own can't run script.
add_header Content-Security-Policy "default-src 'none'; style-src 'unsafe-inline'; sandbox";
# add_header here replaces the server-level HSTS; say it again
# (docs/security-headers.md).
add_header Strict-Transport-Security "max-age=31536000" always;
}
```
Every stored name ends in a random part and a new one is never an
existing file's, so `immutable` is safe; a rename makes a new name
rather than changing a file in place.
4. `sharp` is a server dependency now: run `pnpm install` in
`/srv/ngu-api` after the deploy. It ships prebuilt Linux binaries,
so no build tools are needed.
5. Captions need nginx to send `.vtt` files as `text/vtt` (Firefox
won't show them otherwise, and `nosniff` stops guessing). Check:
```sh
grep vtt /etc/nginx/mime.types
```
If nothing comes back, add `text/vtt vtt;` inside the `types { }`
block of `/etc/nginx/mime.types`, then
`sudo nginx -t && sudo systemctl reload nginx`. Not in the site's
own file: a `types` block there replaces the whole list.