Repository navigation
feat(web): upload a recipe's title picture from the editor - #533
Merged
Merged
Conversation
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
marked this pull request as ready for review
September 25, 2026 15:06
This was referenced Sep 28, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #520.
Someone running
cook serveron another machine had no way to add or replace a recipe's title picture (theRecipe.jpgnext toRecipe.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:
Content-Type.Recipe.jpg..jpg: it is the first namecooklang-findlooks for, so nothing older can hide it, and it is a format cook.md sync carries (it skips.webp). Any olderRecipe.jpeg,.pngor.webpis removed; step pictures (Recipe.1.jpg) are never touched.ftypbox and refused with a415whose 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 whyacceptdeliberately leavesimage/heicout; Safari on a Mac decodes it in the editor.413.image:) keeps showing that one; the dialog says so and disables upload/remove.API
New
/api/recipe_image/{*path}, documented inapi_docs.rs/docs/api.md:GET{ path, image, source }—sourceisfile,metadataornull404unknown recipePUT400empty/undecodable,404,413too large,415+code: "heif" | "unsupported"DELETERecipe.{jpg,jpeg,png,webp}; returns the new state404when there was nonemultipartfeature nor the test client's is needed./api/recipes/image/{*path}sub-route: axum only allows a catch-all last, so that static segment would claim every recipe in a folder namedimage/(the way/recipes/raw/already claims one namedraw/).DefaultBodyLimit, inside the global 1 MB one, which it overrides.Also changed
/api/staticnow sendsCache-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 cheap304through the ETagServeDiralready sends. This also fixes pictures replaced by other means (sync, a file manager).image0.25 with only thejpeg,pngandwebpcodecs (pure Rust, no native libs, norayon), behind theserverfeature.tower-httpgains itsset-headerfeature.docs/server.mdgains a "Title pictures" section.Testing
cargo fmt,cargo clippy --workspace --all-targets --all-features -- -D warnings: clean.cargo test --workspaceandcargo test --no-default-features: pass.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.tests/recipe_image_api_test.rs(spawnscook serverthroughcommon::with_isolated_config): upload + read back, a JPEG stored re-encoded rather than as sent, replacing older.png/.webpwhile keeping step pictures, a >1 MB upload accepted (route limit wins), a body over 10 MB refused with413, unsupported/HEIC/empty bodies refused with nothing written, unknown recipe and..path refused, delete then404, metadata-named picture reported as such,no-cacheon/api/static.tests/e2e/editor.spec.ts): upload through the dialog on a throwaway recipe (preview appears,.jpgon 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.Not changed here
DELETE /api/recipes/{*path}still leaves the recipe's pictures behind.\between folder and file (pre-existing; browsers cope). Being handled separately.🤖 Generated with Claude Code