snoopdave commented on code in PR #327:
URL: https://github.com/apache/roller/pull/327#discussion_r4608427037


##########
app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAMediaFileManagerImpl.java:
##########
@@ -249,20 +257,65 @@ public void createThemeMediaFile(Weblog weblog, MediaFile 
mediaFile,
         }
     }
 
+    /**
+     * Record the image's size and save a thumbnail of it. The size is read
+     * from the image header first; images with more pixels than
+     * uploads.thumbnail.maxPixels are left without a thumbnail rather than
+     * decoded, and the rest are decoded subsampled to near thumbnail size.
+     * Content that no image reader recognizes gets no thumbnail, whatever
+     * content type the client sent.
+     */
     private void updateThumbnail(MediaFile mediaFile) {
         try {
             FileContentManager cmgr = WebloggerFactory.getWeblogger()
                     .getFileContentManager();
             FileContent fc = cmgr.getFileContent(mediaFile.getWeblog(),
                     mediaFile.getId());
+            int maxPixels = WebloggerConfig.getIntProperty(
+                    THUMBNAIL_MAX_PIXELS_PROPERTY, 
DEFAULT_THUMBNAIL_MAX_PIXELS);
             BufferedImage img;
 
-            img = ImageIO.read(fc.getInputStream());
+            try (InputStream in = fc.getInputStream();
+                 ImageInputStream iis = ImageIO.createImageInputStream(in)) {
+                Iterator<ImageReader> readers = iis == null
+                        ? null : ImageIO.getImageReaders(iis);
+                if (readers == null || !readers.hasNext()) {
+                    log.debug("No image reader for media file "
+                            + mediaFile.getId() + ", not creating a 
thumbnail");
+                    return;
+                }
+                ImageReader reader = readers.next();
+                try {
+                    reader.setInput(iis, true, true);
+                    int width = reader.getWidth(0);
+                    int height = reader.getHeight(0);
+                    if (width <= 0 || height <= 0
+                            || (long) width * height > maxPixels) {
+                        log.info("Not creating a thumbnail for media file "
+                                + mediaFile.getId() + " (" + width + "x"
+                                + height + "), it is larger than "
+                                + THUMBNAIL_MAX_PIXELS_PROPERTY + "="
+                                + maxPixels + " pixels");
+                        return;

Review Comment:
   **Important:** When an existing image is replaced with one over the limit, 
its old `_sm` file remains. `getMediaFile(..., true)` then loads a thumbnail of 
the previous image. Remove any existing thumbnail when generation is skipped, 
and test replacement followed by reload.



##########
app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAMediaFileManagerImpl.java:
##########
@@ -249,20 +257,65 @@ public void createThemeMediaFile(Weblog weblog, MediaFile 
mediaFile,
         }
     }
 
+    /**
+     * Record the image's size and save a thumbnail of it. The size is read
+     * from the image header first; images with more pixels than
+     * uploads.thumbnail.maxPixels are left without a thumbnail rather than
+     * decoded, and the rest are decoded subsampled to near thumbnail size.
+     * Content that no image reader recognizes gets no thumbnail, whatever
+     * content type the client sent.
+     */
     private void updateThumbnail(MediaFile mediaFile) {
         try {
             FileContentManager cmgr = WebloggerFactory.getWeblogger()
                     .getFileContentManager();
             FileContent fc = cmgr.getFileContent(mediaFile.getWeblog(),
                     mediaFile.getId());
+            int maxPixels = WebloggerConfig.getIntProperty(
+                    THUMBNAIL_MAX_PIXELS_PROPERTY, 
DEFAULT_THUMBNAIL_MAX_PIXELS);
             BufferedImage img;
 
-            img = ImageIO.read(fc.getInputStream());
+            try (InputStream in = fc.getInputStream();
+                 ImageInputStream iis = ImageIO.createImageInputStream(in)) {
+                Iterator<ImageReader> readers = iis == null
+                        ? null : ImageIO.getImageReaders(iis);
+                if (readers == null || !readers.hasNext()) {
+                    log.debug("No image reader for media file "
+                            + mediaFile.getId() + ", not creating a 
thumbnail");
+                    return;
+                }
+                ImageReader reader = readers.next();
+                try {
+                    reader.setInput(iis, true, true);
+                    int width = reader.getWidth(0);
+                    int height = reader.getHeight(0);
+                    if (width <= 0 || height <= 0
+                            || (long) width * height > maxPixels) {
+                        log.info("Not creating a thumbnail for media file "
+                                + mediaFile.getId() + " (" + width + "x"
+                                + height + "), it is larger than "
+                                + THUMBNAIL_MAX_PIXELS_PROPERTY + "="
+                                + maxPixels + " pixels");
+                        return;

Review Comment:
   **Important:** A missing thumbnail makes `MediaResourceServlet` serve the 
original file at the thumbnail URL. The media grid and upload success page 
request that URL for images, so an image over the limit is transferred at full 
size for previews. Serve a lightweight placeholder for missing thumbnails or 
make those views avoid the thumbnail request, and test the preview path.



##########
app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAMediaFileManagerImpl.java:
##########
@@ -249,20 +257,65 @@ public void createThemeMediaFile(Weblog weblog, MediaFile 
mediaFile,
         }
     }
 
+    /**
+     * Record the image's size and save a thumbnail of it. The size is read
+     * from the image header first; images with more pixels than
+     * uploads.thumbnail.maxPixels are left without a thumbnail rather than
+     * decoded, and the rest are decoded subsampled to near thumbnail size.
+     * Content that no image reader recognizes gets no thumbnail, whatever
+     * content type the client sent.
+     */
     private void updateThumbnail(MediaFile mediaFile) {
         try {
             FileContentManager cmgr = WebloggerFactory.getWeblogger()
                     .getFileContentManager();
             FileContent fc = cmgr.getFileContent(mediaFile.getWeblog(),
                     mediaFile.getId());
+            int maxPixels = WebloggerConfig.getIntProperty(
+                    THUMBNAIL_MAX_PIXELS_PROPERTY, 
DEFAULT_THUMBNAIL_MAX_PIXELS);
             BufferedImage img;
 
-            img = ImageIO.read(fc.getInputStream());
+            try (InputStream in = fc.getInputStream();
+                 ImageInputStream iis = ImageIO.createImageInputStream(in)) {
+                Iterator<ImageReader> readers = iis == null
+                        ? null : ImageIO.getImageReaders(iis);
+                if (readers == null || !readers.hasNext()) {
+                    log.debug("No image reader for media file "
+                            + mediaFile.getId() + ", not creating a 
thumbnail");
+                    return;
+                }
+                ImageReader reader = readers.next();
+                try {
+                    reader.setInput(iis, true, true);
+                    int width = reader.getWidth(0);
+                    int height = reader.getHeight(0);
+                    if (width <= 0 || height <= 0
+                            || (long) width * height > maxPixels) {

Review Comment:
   **Important:** The size check returns before saving the dimensions read from 
the header. A new image over the limit retains -1 dimensions, and a replacement 
retains the previous image’s dimensions. Save the new width and height before 
deciding whether to create a thumbnail, and assert both cases in tests.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to