Skip to content

Read image dimensions before decoding uploads for thumbnails - #327

Open
mraible wants to merge 16 commits into
roller-6.1.xfrom
bound-image-decoding-6.1.x
Open

mraible wants to merge 16 commits into
roller-6.1.xfrom
bound-image-decoding-6.1.x

Conversation

@mraible

@mraible mraible commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

The 6.1.x version of #315. Thumbnail creation in JPAMediaFileManagerImpl now reads an image's width and height from its header with an ImageReader before decoding it, and decodes only what the thumbnail needs. The changes to JPAMediaFileManagerImpl are the same as on master; roller.properties gets only uploads.thumbnail.maxPixels, and MediaResourceServlet is unchanged.

  • Images with more pixels than the new uploads.thumbnail.maxPixels setting (default 50,000,000) are stored without a thumbnail, and the skip is logged. The upload still succeeds and keeps the image's real width and height.
  • Other images are decoded with subsampling sized to about twice the thumbnail in both directions. An image so much wider than tall (or taller than wide) that its thumbnail would be under a pixel across isn't decoded; such images never got a thumbnail.
  • Only JPEG, PNG, GIF and BMP content gets a thumbnail, whatever the file name or content type says. The TIFF reader reads a whole strip or tile at a time even when subsampling, so TIFF images are now stored without one, and their previews load the original.
  • A thumbnail request (?t=true) for an image over the limit gets the original, as it does for any image without a thumbnail, so published entries that embed the thumbnail address keep showing the image. The media list and chooser show the file type icon for such an image directly, and choosing one in the entry editor inserts the image itself.
  • Running out of memory while decoding leaves the image without a thumbnail instead of failing the upload after the file was saved.
  • When new content gets no thumbnail, including a replacement that isn't an image, the old content's thumbnail and size are removed. Content whose header can be read but whose pixels can't is stored without a size. A mistyped uploads.thumbnail.maxPixels (such as 50_000_000, a value over the int range, empty or 0) logs a warning and uses the default.

Tests: MediaFileThumbnailTest (DB-backed) covers an image over a configured limit, a normal image, a header-only 50000x50000 PNG, a very wide image, a TIFF image (also under a .png name), an image that can't be decoded, and replacements by an over-limit image, unreadable content and a non-image. ThumbnailSubsamplingTest covers the subsampling step and the thumbnail size after a size change. MediaResourceThumbnailTest covers thumbnail requests, including the fallback to the original for an image over the limit. MediaFileTest still passes.

CHANGES lists the limit and the TIFF change under "Limits on uploads and fetched feeds".

Thumbnail creation now reads the image's width and height from its header with an ImageReader before decoding it. Images with more pixels than the new uploads.thumbnail.maxPixels setting (default 50,000,000) are stored without a thumbnail, and the skip is logged. Other images are decoded with subsampling sized to the thumbnail. The upload's content stream is now closed, and content that no image reader recognizes gets no thumbnail whatever content type the client sent.

@snoopdave snoopdave left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of a46508e2b: The fix hunks match #315; the remaining diff differences are branch context. The new limit covers the production image decode path, but three behaviors around skipped thumbnails need correction. This was a read-only review; no build was run.

The width and height read from an image's header are now saved before the pixel limit is checked, so an image too large for a thumbnail still shows its real size. When a file's new content gets no thumbnail, for any reason, the thumbnail left from its previous content is removed, so a replaced image no longer shows the old image's thumbnail.

MediaResourceServlet answers a thumbnail request for an image over uploads.thumbnail.maxPixels with the small file icon instead of the full image. Other images without a thumbnail are still served in full at the thumbnail address. The media library and chooser show their file icon for such images without requesting a thumbnail, and inserting one into an entry uses the image's own address.
The placeholder for an image too large for a thumbnail now gets its content type from MediaTypePolicy.applyResponseHeaders, like every other response from MediaResourceServlet.
@mraible
mraible requested a review from snoopdave November 18, 2026 04:16
…ecoding-6.1.x

# Conflicts:
#	app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/MediaResourceServlet.java
…ss hasThumbnail from the chooser

A value of uploads.thumbnail.maxPixels that is not a positive int (for example 50_000_000, 3000000000, an empty value or 0) used to throw NumberFormatException out of the media servlet and the media library pages, and silently stopped thumbnails being made on upload. The limit is now read in one place, MediaFile.thumbnailMaxPixels(), which falls back to the default and logs a warning once for each bad value.

When a file's content is replaced with something no image reader recognizes, or whose header can't be read, the stored width and height are reset to -1 (unknown), so the previous image's size no longer marks the new content as over the limit.

The media chooser's onSelectMediaFile wrapper now passes hasThumbnail on to the entry editor, so an image too large for a thumbnail is inserted at its own URL instead of the thumbnail URL that shows the placeholder icon.

The derived overThumbnailPixelLimit getter is listed as transient in MediaFile.orm.xml like the other derived getters.

@snoopdave snoopdave left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of fa7a0142f: The upload path reads image dimensions before decoding, and the earlier review points are addressed. The twin’s extra media UI hunks apply to master’s different UI; this branch uses its existing views and a small icon response. One preview case remains when the configured pixel limit changes after upload. This was a read-only review; tests were not run.

Earlier comments: 3 of 3 resolved.

Whether an image has a thumbnail was worked out from the current uploads.thumbnail.maxPixels setting, but the thumbnail was made or skipped at upload time, so changing the limit later could make a file with a thumbnail look like it had none, or the other way around. isOverThumbnailPixelLimit() is replaced by isThumbnailSkipped(), which is true for an image whose size was read on upload but which has no _sm file stored, whatever the limit is now.
The limit-change tests now end the session before reloading the file without its content, as the list views load it, so whether it has a thumbnail is looked up in the file store rather than read from the instance loaded during upload.
Most images on an upgraded blog have no _sm file, because they were carried over from older versions or by migrations, so treating every image without one as skipped would have taken away their previews. An image now counts as skipped only when it has no _sm file and its recorded size is over the current uploads.thumbnail.maxPixels; an image without a thumbnail under the limit is previewed from the original as before, and one with a thumbnail always gets it.
…ecoding-6.1.x

# Conflicts:
#	app/src/main/java/org/apache/roller/weblogger/ui/rendering/servlets/MediaResourceServlet.java
… to scale

The subsampling step now comes from whichever side needs the larger step, so both sides are decoded at about twice the thumbnail size. An image so much wider than tall, or taller than wide, that its thumbnail would be less than a pixel across is no longer decoded, since no thumbnail could be made from it anyway. Setting a media file's width or height also resets its cached thumbnail size, so a replaced image's thumbnail gets the new image's shape.
These are the formats whose image readers decode a subsampled image row by row. The TIFF reader reads a whole strip or tile into memory even when subsampling, so TIFF images, and any other format a reader is installed for, are now stored with their size and without a thumbnail. The format comes from the content, not the file name. CHANGES lists the images that get no thumbnail.
Running out of memory while decoding an image for its thumbnail is logged and leaves the image without a thumbnail, instead of failing the upload after the file was saved. An image whose header can be read but whose pixels can't is stored without a size, as it was before the size came from the header. A thumbnail that was saved is kept when a later step fails, and removing an old thumbnail also clears it from the media file and logs failures other than a missing file.
…wn name

The invalid-limit test named its uploads by the length of the limit value, so two of them shared a name.
…hat isn't an image

Before, the replaced image's thumbnail was still served at the file's thumbnail address.
Entries that embed an image's thumbnail address showed the file icon once the image was stored without a thumbnail. The thumbnail request now falls back to the original, as it does for any image without a thumbnail and as it did before. The media list and chooser already show the file type icon for such an image themselves, so they don't load the original as a preview.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants