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


##########
app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAMediaFileManagerImpl.java:
##########
@@ -252,20 +260,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, 
this return leaves its old `_sm` file in place. `getMediaFile(..., true)` still 
loads that file, so the thumbnail shows the previous image. Remove the existing 
thumbnail when skipping creation, and test replacement through 
`updateMediaFile()`.



##########
app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAMediaFileManagerImpl.java:
##########
@@ -252,20 +260,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;
+                    }
 
-            // determine and save width and height
-            mediaFile.setWidth(img.getWidth());
-            mediaFile.setHeight(img.getHeight());
-            strategy.store(mediaFile);
+                    // determine and save width and height

Review Comment:
   **Important:** The size is stored only after the limit check. A newly 
uploaded image over the limit therefore retains 0 × 0 dimensions, which the 
upload success page displays. Store the header dimensions before returning for 
an over-limit image, and assert them in the test.



##########
app/src/main/java/org/apache/roller/weblogger/business/jpa/JPAMediaFileManagerImpl.java:
##########
@@ -252,20 +260,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

Review Comment:
   **Important:** Skipping the thumbnail leaves the thumbnail URL without an 
`_sm` file. `MediaResourceServlet` then serves the full original image at that 
URL, so the media library and chooser load oversized originals for their 
previews. Make skipped thumbnails return a placeholder or missing-thumbnail 
response that lets the UI show its file icon.



-- 
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