Skip to content

feat(web): upload a recipe's title picture from the editor - #533

Merged
dubadub merged 2 commits into
cooklang:mainfrom
LeMyst:feat/title-picture-upload
Sep 25, 2026
Merged

dubadub merged 2 commits into
cooklang:mainfrom
LeMyst:feat/title-picture-upload

Conversation

@LeMyst

@LeMyst LeMyst commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #520.

Someone running cook server on another machine had no way to add or replace a recipe's title picture (the Recipe.jpg next to Recipe.cook) short of a separate file manager. The editor now has a Picture button that opens a dialog: it shows the current title picture and lets you choose or drop a new one, replace it, or remove it (with an inline confirmation).

What happens to an upload

In the browser, the editor scales the photo down before sending it: an <img> decodes it (every browser turns it upright from its Exif orientation, and Safari reads HEIC), a canvas draws it at most 2048 px on the longer edge over white, and a JPEG goes up. A 2.3 MB 12 MP phone photo leaves as ~700 KB after ~0.25 s in Chromium. A JPEG that already fits, or anything the browser cannot decode, is sent as it is.

On the server, whatever arrives — from the editor or any other client:

  • JPEG, PNG and WebP are accepted, up to 10 MB. The format is read from the bytes, not the Content-Type.
  • The picture is turned upright from its Exif orientation, scaled down to at most 2048 px on the longer edge, laid over white where it is transparent, and saved as Recipe.jpg.
  • Always re-encoded, never stored as sent: what lands in the recipe folder is only what the server's encoder wrote — no malformed segment for a later reader to trip on, no data riding after the image, and no Exif (GPS position included). The browser encodes at 0.92, above the server's 85, so the second pass costs little.
  • Always .jpg: it is the first name cooklang-find looks for, so nothing older can hide it, and it is a format cook.md sync carries (it skips .webp). Any older Recipe.jpeg, .png or .webp is removed; step pictures (Recipe.1.jpg) are never touched.
  • On a release build, a 12 MP photo sent unshrunk is processed in ~0.5 s. Decoding runs off the async runtime, one picture at a time, so a small host is not asked for several hundred MB at once.
  • HEIC / AVIF would need a native decoder, so they are recognised by their ftyp box and refused with a 415 whose message explains how to get a JPEG. An iPhone's own browser converts HEIC to JPEG on upload as long as the file picker does not ask for HEIC, which is why accept deliberately leaves image/heic out; Safari on a Mac decodes it in the editor.
  • The 10 MB limit is sized for what still arrives unshrunk: a JPEG that already fits, or a HEIC photo outside Safari, which has to get through to be refused with the explanatory message rather than a bare 413.
  • A recipe whose metadata names a picture (image:) keeps showing that one; the dialog says so and disables upload/remove.

API

New /api/recipe_image/{*path}, documented in api_docs.rs / docs/api.md:

Method Does Errors
GET { path, image, source } — source is file, metadata or null 404 unknown recipe
PUT body = the picture's raw bytes (not multipart); returns the new state 400 empty/undecodable, 404, 413 too large, 415 + code: "heif" | "unsupported"
DELETE removes every Recipe.{jpg,jpeg,png,webp}; returns the new state 404 when there was none
  • Raw bytes rather than multipart, so neither axum's multipart feature nor the test client's is needed.
  • It is not an /api/recipes/image/{*path} sub-route: axum only allows a catch-all last, so that static segment would claim every recipe in a folder named image/ (the way /recipes/raw/ already claims one named raw/).
  • The route carries its own 10 MB DefaultBodyLimit, inside the global 1 MB one, which it overrides.

Also changed

  • /api/static now sends Cache-Control: no-cache. A picture replaced under the same name keeps its URL, and without the header a browser can keep showing the old one from cache; with it, the check is a cheap 304 through the ETag ServeDir already sends. This also fixes pictures replaced by other means (sync, a file manager).
  • New dependency: image 0.25 with only the jpeg, png and webp codecs (pure Rust, no native libs, no rayon), behind the server feature. tower-http gains its set-header feature.
  • Dialog strings translated for all 7 locales; docs/server.md gains a "Title pictures" section.

Testing

  • cargo fmt, cargo clippy --workspace --all-targets --all-features -- -D warnings: clean.
  • cargo test --workspace and cargo test --no-default-features: pass.
  • New unit tests in src/server/title_image.rs: over-size scaling, PNG/WebP → JPEG, transparency → white, every upload re-encoded (Exif and trailing data dropped), Exif orientation applied, HEIC/AVIF recognised, unsupported/truncated input.
  • New tests/recipe_image_api_test.rs (spawns cook server through common::with_isolated_config): upload + read back, a JPEG stored re-encoded rather than as sent, replacing older .png/.webp while keeping step pictures, a >1 MB upload accepted (route limit wins), a body over 10 MB refused with 413, unsupported/HEIC/empty bodies refused with nothing written, unknown recipe and .. path refused, delete then 404, metadata-named picture reported as such, no-cache on /api/static.
  • Playwright (tests/e2e/editor.spec.ts): upload through the dialog on a throwaway recipe (preview appears, .jpg on disk, recipe page picks it up), remove it; a HEIC upload showing the help message; and a 3000×1500 PNG that leaves the browser as a 2048×1024 JPEG.
  • Tried end to end in a container image built from this branch.

Not changed here

  • DELETE /api/recipes/{*path} still leaves the recipe's pictures behind.
  • On Windows, image URLs are built with \ between folder and file (pre-existing; browsers cope). Being handled separately.

🤖 Generated with Claude Code

LeMyst and others added 2 commits September 24, 2026 21:50
Someone who runs `cook server` on another machine has no way to add or
replace the `Recipe.jpg` a recipe shows except a separate file manager
(cooklang#520). The editor now has a Picture button: a dialog that shows the
current title picture and takes a new one, by file picker or drop, or
removes it.

It talks to a new `/api/recipe_image/{*path}` (GET, PUT, DELETE). The
PUT body is the picture's bytes rather than a multipart form, so neither
axum nor the test client needs another feature. The route is not a
`/api/recipes/image/...` sub-route: axum only allows a catch-all last, so
the static segment would claim every recipe in a folder named `image/`,
the way `/recipes/raw/` already claims one named `raw/`.

An upload is decoded, turned upright from its Exif orientation, scaled to
at most 2048 px on the longer edge, laid over white where it is
transparent, and stored as `Recipe.jpg`; a JPEG that needs none of that
and would not come out smaller is kept as sent. A phone photo of several
megabytes lands at a few hundred kilobytes (12 MP in ~0.5 s, 50 MP in
~1 s on a release build), the listing no longer loads every card's
picture at full size, the photo's GPS location goes with its Exif, and
the result is a name `cooklang-find` looks for first and a format cook.md
sync carries, which `.webp` is not. Any older `Recipe.jpeg`, `.png` or
`.webp` is removed; step pictures are left alone. Decoding runs off the
async runtime, one picture at a time, so a small host is not asked for
several hundred megabytes at once.

The `image` crate is pure Rust and only its JPEG, PNG and WebP codecs are
enabled. HEIC and AVIF would need a native decoder, so they are
recognised by their `ftyp` box and refused with a `415` that says how to
get a JPEG: an iPhone's own browser converts HEIC while the file picker
does not ask for it, which is why `accept` leaves `image/heic` out.

The route carries its own 40 MB body limit inside the global 1 MB one. A
recipe whose metadata names a picture keeps showing that one; the dialog
says so and does not offer to replace it.

`/api/static` now answers with `Cache-Control: no-cache`. A picture
replaced under the same name keeps its URL, and without the header a
browser can go on showing the old one from its cache; with it the check
is a 304 through the ETag ServeDir already sends.

Closes cooklang#520

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the server

The editor now scales a photo down before sending it: an <img> decodes it
(every browser turns it upright from its Exif orientation, and Safari reads
HEIC), a canvas draws it at most 2048 px on the longer edge over white, and
a JPEG goes up. A 2.3 MB 12 MP phone photo leaves as ~700 KB after a quarter
of a second in Chromium. A JPEG that already fits is sent as it is, and so
is anything the browser cannot decode, for the server to accept or explain.
The size check now applies to what is sent, so a photo too big to upload
whole can still shrink below it.

With the browser doing that, the route's body limit drops from 40 MB to
10 MB. What still arrives unshrunk is a JPEG that already fits, or a HEIC
photo outside Safari, which has to fit too so that it is refused with the
message that explains it rather than a bare 413.

The server no longer keeps a JPEG as sent when re-encoding would not shrink
it: every upload is rebuilt from its pixels. What lands in the recipe
folder is then only what this encoder wrote, with no malformed segment for
a later reader to trip on, nothing riding after the image, and no Exif. The
browser encodes at 0.92, above the server's 85, since its copy only has to
survive that second pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst marked this pull request as ready for review September 25, 2026 15:06
@dubadub
dubadub merged commit 9940b27 into cooklang:main Sep 25, 2026
6 checks passed
LeMyst added a commit to LeMyst/cookcli that referenced this pull request Sep 30, 2026
The editor's Picture dialog handled only the title picture (cooklang#533), so a
step's picture still had to be dropped into the recipe folder by hand.

`/api/recipe_image/{*path}` now takes `?section=S&step=N` (section
defaults to 1). PUT stores the upload as `Recipe.S.N.jpg` through the
same pipeline as the title picture (10 MB, Exif orientation, 2048 px,
re-encoded, HEIC explained) and removes the step's older files in both
conventions and every extension (`Recipe.S.N.png`, `Recipe.G.jpg`, ...)
so none can come back when the new one is removed. DELETE removes them
all. A step or section the saved recipe does not have is a 400. The
title picture and other steps are never touched. GET without a step
also lists every section's steps with their text and picture.

The lookup the recipe page uses for a step's picture moves to
`builders::step_image`, so the API reports what the page shows.

UI: a "Picture for" list in the dialog (Title / Section > Step N: text,
marked with a camera once a step has a picture). The dialog saves the
editor's text first, so steps are numbered as the page numbers them.
On the recipe page, editors get a camera button on each step that opens
the editor with that step picked (`#picture=S.N`, a fragment so it
survives the /edit redirect). Strings are translated for every locale.

Closes cooklang#562

Co-Authored-By: Claude Opus 5.5 <[email protected]>
LeMyst added a commit that referenced this pull request Sep 30, 2026
The editor's Picture dialog handled only the title picture (#533), so a
step's picture still had to be dropped into the recipe folder by hand.

`/api/recipe_image/{*path}` now takes `?section=S&step=N` (section
defaults to 1). PUT stores the upload as `Recipe.S.N.jpg` through the
same pipeline as the title picture (10 MB, Exif orientation, 2048 px,
re-encoded, HEIC explained) and removes the step's older files in both
conventions and every extension (`Recipe.S.N.png`, `Recipe.G.jpg`, ...)
so none can come back when the new one is removed. DELETE removes them
all. A step or section the saved recipe does not have is a 400. The
title picture and other steps are never touched. GET without a step
also lists every section's steps with their text and picture.

The lookup the recipe page uses for a step's picture moves to
`builders::step_image`, so the API reports what the page shows.

UI: a "Picture for" list in the dialog (Title / Section > Step N: text,
marked with a camera once a step has a picture). The dialog saves the
editor's text first, so steps are numbered as the page numbers them.
On the recipe page, editors get a camera button on each step that opens
the editor with that step picked (`#picture=S.N`, a fragment so it
survives the /edit redirect). Strings are translated for every locale.

Closes #562

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Upload title picture through web UI/editor

2 participants