Skip to content

Slugify uploaded filenames; cap image height too - #10

Merged
punchagan merged 1 commit into
mainfrom
slugify-uploaded-image-filenames
Aug 6, 2026
Merged

punchagan merged 1 commit into
mainfrom
slugify-uploaded-image-filenames

Conversation

@punchagan

@punchagan punchagan commented Aug 6, 2026 •

Copy link
Copy Markdown
Owner

Summary

Two related fixes to image uploads, both traced from a real user's repo:

1. Slugify uploaded filenames

An uploaded "Map of Living Room.PNG" broke an external pre-commit hook doing further image compression — pngquant --ext .png treats "name.PNG" → "name.png" as a different filename (case-sensitive), silently leaving an untracked derivative file behind on every commit that touched it.

  • Extracted _slugify() from the existing edition-slug logic in new_edition() (same lowercase-hyphenated convention) — now shared.
  • upload_image() applies it to the upload's filename stem before saving, falling back to "image" if the name slugifies to nothing (e.g. "!!!.png").

Fixes the class of problem at the source (spaces, mixed case, mixed-case extensions) rather than only patching around the one script that happened to break on it.

2. Cap image height, not just width

While investigating, found compress_image() only ever checked img.width against the max dimension — a tall portrait image (narrow width, huge height) sailed through completely unresized, since its width alone was never over the threshold.

Swapped the width-only check for Image.thumbnail(), which bounds both width and height in one call (only ever shrinks, preserves aspect ratio). Renamed IMAGE_MAX_WIDTH → IMAGE_MAX_DIMENSION to match its new meaning.

Test plan

  • uv run ruff check --unsafe-fixes --fix / uv run ruff format
  • uv run --extra dev pytest (417 passed) — 4 new tests (slugify: spaces+mixed case, special characters, empty-after-slugify fallback; portrait-image height cap), each verified red before the fix
  • Existing upload/new-edition tests unaffected (their filenames/dimensions were already within bounds, so both fixes are no-ops for them)

🤖 Generated with Claude Code

Uploaded filenames were saved as-is (spaces, mixed case, and mixed-
case extensions all preserved). Traced from a real user's repo: an
uploaded "Map of Living Room.PNG" broke an external pre-commit hook
doing further image compression — pngquant's --ext .png treats
"name.PNG" -> "name.png" as a *different* filename (case-sensitive),
silently leaving an untracked derivative file behind on every commit
that touched it.

_slugify() (extracted from the existing edition-slug logic in
new_edition() — same lowercase-hyphenated convention, now shared)
is applied to the upload's filename stem before saving, falling
back to "image" if the name slugifies to nothing (e.g. "!!!.png").
This avoids the whole class of case-sensitivity/whitespace problems
regardless of what any downstream tooling (git hooks, shell scripts,
URL encoding) does with the file, rather than only patching around
the one script that happened to break on it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@punchagan
punchagan merged commit 209c4f1 into main Aug 6, 2026
2 checks passed
@punchagan punchagan changed the title Slugify uploaded image filenames Slugify uploaded filenames; cap image height too Aug 6, 2026
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.

1 participant