Files
LuPhoto/README.md
T
lucioandClaude Opus 5.5 f5152fb709 Anti-scraping: 3x3 grids, decoy pools, robot check
Every pyramid level is cut into at least min_grid x min_grid tiles.
Each photo's tile pool mixes real tiles with hard-linked decoys from
other photos; the grid-to-pool map, byte sizes, seed and scramble seed
are sealed per build and unlocked by a press-and-hold check backed by
a static proof of work. Cell scrambling is on by default, and
robots.txt disallows tiles and pool files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 21:39:30 -08:00

201 lines
13 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# LuPhoto
LuPhoto serves photos.luciochen.com. It is one Go program that reads album folders, cuts every photo into WebP tiles at several resolutions with libvips (at most 512 px, at least a 3 × 3 grid per level), and publishes a static site in which the browser stitches the visible tiles back together on a canvas. A one-time robot check unlocks the tile maps; see [Protection](#protection). Design: [docs/DESIGN.md](docs/DESIGN.md).
## Commands
```
luphoto [-config /etc/luphoto/config.toml] <command>
```
| Command | What it does |
|---|---|
| `build` | Process the albums once, publish a new build, exit. |
| `serve` | Build, watch the albums folder, rebuild after changes, and serve the site over HTTP on `server.listen`. |
| `export` | Build, watch and rebuild; no HTTP. Point Caddy or nginx at `<output>/current`. |
| `run` | `serve` or `export`, whichever `server.mode` says. The systemd unit uses this. |
| `version` | Print the version. |
A missing config file means all defaults. `SIGHUP` re-reads the config and rebuilds (an invalid config is logged and the old one kept; a new `server.listen` restarts the listener). `SIGINT`/`SIGTERM` stop gracefully. Rebuilds never overlap; changes during a build cause one follow-up build.
## Album folders
```
/srv/luphoto/albums/
2026-10-iceland/
album.json
DSC_0001.jpg
PANO_0007.jpg
```
The folder name is the album slug (`/a/2026-10-iceland/`). Only JPEGs listed in `album.json` are processed and shown. An album whose `album.json` is broken is skipped and logged; if the previous build had it, that version stays live.
```json
{
"title": "Iceland, in October",
"subtitle": "冰岛 · 十月",
"date": "2026-10",
"order": 10,
"hidden": false,
"cover": { "photo": "PANO_0007.jpg", "crop": [0, 410, 8256, 2322] },
"collapsed_focus_y": 0.5,
"show_exif": true,
"photos": [
{ "file": "DSC_0001.jpg", "title": "Vík", "caption": "Black sand, *7 a.m.*", "alt": "A black sand beach" },
{ "file": "PANO_0007.jpg", "caption": "Two screens wide", "slides": 2 }
]
}
```
| Field | Type | Default | Meaning |
|---|---|---|---|
| `title` | string | folder name | Album title, sentence case. |
| `subtitle` | string | none | Second line, for example the Chinese title. |
| `date` | `YYYY`, `YYYY-MM` or `YYYY-MM-DD` | none | Shown as the eyebrow; sorts albums without `order`, newest first. |
| `order` | integer | none | Albums with `order` come first, ascending. |
| `hidden` | bool | `false` | Built and reachable by URL, but left off the index. |
| `cover.photo` | file name | first photo | Photo the 32:9 cover is cut from. Must be listed in `photos`. |
| `cover.crop` | `[x, y, w, h]` | none | Cover rectangle in pixels of the upright photo. |
| `cover.focus_y` | 0–1 | 0.5 | Without `crop`: a 32:9 band at full width centred at this height. |
| `collapsed_focus_y` | 0–1 | 0.5 | Which 32:6 band of the cover a closed index strip shows. |
| `show_exif` | bool | `true` | Put camera, lens, focal length, aperture, shutter, ISO and date into the manifest and caption line. GPS is never kept. |
| `photos[].file` | file name | required | A `.jpg`/`.jpeg` in the album folder. Order of the list is the viewing order. |
| `photos[].title` | string | none | Caption title. |
| `photos[].caption` | string | none | Plain text; `*emphasis*` and line breaks only. |
| `photos[].alt` | string | none | Text alternative for screen readers. |
| `photos[].slides` | integer ≥ 1 | 1 | Show a wide photo as N viewport-wide sections with a continuous pan. |
Unknown fields are errors, so typos are caught.
## Config
`/etc/luphoto/config.toml`; every key is optional. `packaging/config.toml` is the production file.
| Key | Default | Meaning |
|---|---|---|
| `server.mode` | `"export"` | What `luphoto run` does: `"serve"` or `"export"`. |
| `server.listen` | `"127.0.0.1:8080"` | Address of the built-in HTTP server. |
| `server.base_url` | `"https://photos.luciochen.com"` | Used for canonical and Open Graph URLs. |
| `paths.albums` | `"/srv/luphoto/albums"` | Album folders. Read only. |
| `paths.output` | root or systemd: `/var/lib/luphoto/site`, else `$XDG_DATA_HOME/luphoto/site` | Builds, tiles, `current`. The site key and per-photo records go to `state/` next to it. |
| `paths.cache` | root or systemd: `/var/cache/luphoto`, else the user cache dir | Temporary vips files. |
| `images.tile_size` | `512` | Largest tile edge in pixels. Each level uses min(`tile_size`, short edge ÷ `min_grid`), at least 1 px. |
| `images.min_grid` | `3` | Minimum tile columns and rows per level (1–8). Levels whose short edge would drop below it are not made. Changing it reprocesses everything. |
| `images.overlap` | `1` | Tile overlap in pixels; 0 on levels whose tile is at most twice as wide, and always 0 while `scramble_cells` is on. |
| `images.format` | `"webp"` | `"webp"` or `"avif"` (AVIF needs a libheif AV1 encoder). |
| `images.quality` | `{ top = 90, mid = 88, low = 88 }` | Top two levels, levels above 1100 px, levels up to 1100 px. |
| `images.min_levels` | `6` | Minimum pyramid depth. |
| `images.seed_px` | `48` | Long edge of the blurred placeholder. |
| `images.tile_names` | `"hashed"` | Only `"hashed"` in v1. |
| `images.scramble_cells` | `true` | Shuffle the 64 px cells inside each tile with a per-photo key; the browser restores them when it decodes a tile. Tiles under two cells wide or high are left alone. Forces tile overlap 0. Changing it reprocesses everything. |
| `loading.budget_ms` | `3000` | Time budget per photo in the browser. |
| `loading.max_dpr` | `2.0` | Highest device pixel ratio the loader honours. |
| `loading.concurrency` | `6` | Parallel tile requests. |
| `build.watch` | `true` | Watch the albums folder in `serve` and `export`. |
| `build.debounce` | `"30s"` | Quiet time after the last change before rebuilding. |
| `build.workers` | `2` | Photos processed in parallel. vips threads per photo = CPUs ÷ workers. |
| `build.keep_builds` | `3` | Builds kept; tile folders and decoys no kept build uses are deleted. |
| `protection.block_context_menu` | `true` | Disable the context menu on photos. |
| `protection.hotlink_allow` | `["photos.luciochen.com"]` | Hosts allowed as `Referer` for `/t/*`; empty Referers and the site's own host are always allowed. |
| `protection.decoys` | `4` | Decoy tiles per real tile in each photo folder (0–16); 0 turns decoys off. |
| `protection.gate` | `true` | Show the press-and-hold robot check before photos unlock. `false` publishes the key in the clear and shows no check. |
| `protection.pow_bits` | `20` | Proof-of-work size for the robot check (10–26); each extra bit doubles the average work. |
The watcher ignores dotfiles (including rsync's `.name.XXXXXX` temp files), `*.part`, `*.tmp` and editor backups. vips runs at `nice +5` so processing does not starve the web server.
## Protection
Everything stays static files, so export mode, Caddy, nginx and Cloudflare caching of `/t/` keep working. What a build adds on top of hashed tile names:
- **Small tiles.** Every level of every photo and cover is cut into at least a 3 × 3 grid (`images.min_grid`), so no single file is a usable picture, not even the small levels.
- **Decoys.** Each photo folder `/t/<hash>/` also holds `protection.decoys` × as many decoy files as it has real tiles: hard links (copies if linking fails) to real tiles of other photos, from other albums first, with tile sizes as close as possible. A decoy name is an HMAC of the photo, level, slot and source tile, so a name never changes its bytes and `/t/` stays immutable. Decoys are chosen after all photos are processed; unchanged photos keep their decoys from build to build.
- **Sealed maps.** The album manifest and index carry only layout (sizes, tile grid, captions, EXIF). Which file sits where lives in one small JSON per photo and cover, `/a/<slug>/p/<hash>.<build>.json`, fetched when that photo is near: per level the shuffled pool of real and decoy ids, plus a `sealed` payload (the blurred seed, the cell-scramble seed, the grid→pool map and tile byte sizes) encrypted with a SHA-256 counter-mode keystream.
- **Robot check.** Each build picks a random salt and a random `n` below 2^`pow_bits`. The key is SHA-256("luphoto-key" | salt | n); only a 16-byte check value is published. On the first visit a small card asks the visitor to press and hold for 0.9 s while a Web Worker finds `n` (at 20 bits about 0.1–0.25 s in desktop V8; phones are several times slower). The pass is kept in `localStorage` for that build. Until it is unlocked, each photo shows a jumble of tiles drawn from its pool.
- **Cell scrambling** (`images.scramble_cells`, on by default), the hotlink rule on `/t/*`, no context menu, and `robots.txt` with `Disallow: /t/` and `Disallow: /a/*/p/`.
Limits: this raises the cost of bulk downloading; it does not prevent it. A determined scraper that drives a real browser, or reimplements the client, can solve the check in a fraction of a second, decrypt the maps and stitch the tiles; and decoys are byte-identical copies of other photos' tiles, so a crawler that downloads every folder can tell which files are shared. Every visitor's browser must be able to draw the photos, so it always has everything needed to do so.
## Install
### Ubuntu 22.04 / 24.04, Debian 12
```sh
sudo apt install ./luphoto_<version>_amd64.deb
sudoedit /etc/luphoto/config.toml
sudo systemctl enable --now luphoto
```
The package depends on `libvips-tools` and creates:
- user and group `luphoto`, and group `luphoto-upload`;
- `/srv/luphoto/albums` (root:luphoto-upload, mode 2775, so members of `luphoto-upload` can upload);
- `/var/lib/luphoto` and `/var/cache/luphoto` (owned by `luphoto`);
- `/etc/luphoto/config.toml` (kept on upgrade), `/usr/lib/systemd/system/luphoto.service`, and examples in `/usr/share/doc/luphoto/`.
The unit runs `luphoto run` as `luphoto` with `ProtectSystem=strict`, the albums read only, and only `/var/lib/luphoto` and `/var/cache/luphoto` writable. `systemctl reload luphoto` sends SIGHUP.
### Other distributions
Unpack `luphoto_<version>_linux_<arch>.tar.gz` and follow its `INSTALL.md`. You need libvips 8.12+ with its command-line tools.
## Uploading
Add yourself to `luphoto-upload` (`sudo usermod -aG luphoto-upload $USER`, then log in again), then:
```sh
rsync -av --chmod=Dg+rwxs,Fg+rw 2026-10-iceland/ server:/srv/luphoto/albums/2026-10-iceland/
```
rsync writes to dotted temp names and renames at the end, which the watcher ignores; the rebuild starts `build.debounce` after the last file lands. SFTP clients that upload to `*.part` work the same way.
## Web server
- **Serve mode**: LuPhoto serves `current` itself with the right headers (immutable caching for `/t/`, `/og/`, `/assets/`; one minute for HTML and JSON; CSP; hotlink 403 on `/t/*`; precompressed gzip; no directory listings; `404.html` for missing paths). Put Caddy (`packaging/Caddyfile.serve`), nginx (`packaging/nginx.serve.conf`) or a Cloudflare Tunnel in front.
- **Export mode**: serve `/var/lib/luphoto/site/current` with `packaging/Caddyfile` or `packaging/nginx.conf`. Each build writes `.gz` files next to HTML, JSON, JS, CSS and SVG for `precompressed gzip` / `gzip_static`. Each build also writes `robots.txt`; serve mode serves the same file.
- **Cloudflare**: `packaging/cloudflared.yml` and `packaging/cloudflare.md` (cache rules).
## Building from source
Development runs in Docker; the Windows host needs no libvips. From Git Bash or any POSIX shell:
```sh
scripts/dev.sh go build -o bin/luphoto ./cmd/luphoto
scripts/dev.sh go vet ./...
scripts/dev.sh
```
`scripts/dev.sh` builds the `luphoto-dev` image (Ubuntu 24.04, Go, libvips-tools) on first use, mounts the repository at `/src` and keeps Go caches in volumes. Extra `docker run` arguments go in `LUPHOTO_DEV_ARGS`, for example `LUPHOTO_DEV_ARGS="-v /path/to/work:/work -p 127.0.0.1:8080:8080"`.
`scripts/package.sh` builds static binaries (`CGO_ENABLED=0`, `-trimpath`, `-ldflags "-s -w -X main.version=…"`) for amd64 and arm64, the `.deb` packages (nfpm, installed into `.tools/` on first use) and the tarballs into `dist/`, all inside the dev container. Set `VERSION` to override `git describe`. The `Makefile` has `web`, `build`, `dist` and `deb` targets for hosts with make.
### Front-end
TypeScript and CSS live in `web/src`. The minified bundles, fonts, favicon and chop in `internal/site/static` are committed, so `go build` never needs Node. After changing the front-end, rebuild on a host with Node 22:
```sh
npm --prefix web ci
npm --prefix web run build
```
This type-checks with `tsc --noEmit` (strict) and bundles with esbuild. Fonts are self-hosted from the `@fontsource` packages (latin and latin-ext only). Each site build writes the files to `/assets/` under content-hashed names.
### Brand
`scripts/sync-brand.sh` copies the tokens, `lucio-morph.js`, the favicon and the chop from `../LUCIO_ip` (or `$LUCIO_IP_DIR`) into `web/brand/`, stripping comments. The copies are committed; rebuild the front-end afterwards.
## Output layout
```
/var/lib/luphoto/
site/
current -> builds/<id>
builds/<id>/index.html, 404.html, robots.txt, index.<id>.json, a/<slug>/index.html, a/<slug>/manifest.<id>.json,
a/<slug>/p/<hash>.<id>.json, assets/, t -> ../../t, og -> ../../og (plus .gz copies)
builds/<id>/.refs every pool id per photo folder, read by the cleanup to find stale decoys (never served)
t/<photo-hash>/<tile-id>.webp real tiles and decoys (hard links), unordered
og/<hash>.jpg
state/
site.key
pyramids/<photo-hash>.json
```