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


##########
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:
   Fixed in cd42458b2: the header's width and height are stored before the 
pixel-limit check, so a new over-limit image no longer keeps -1 and a 
replacement no longer keeps the previous image's size. MediaFileThumbnailTest 
checks both the new-upload and the replacement case.



##########
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:
   Fixed in cd42458b2: when new content gets no thumbnail, any _sm file left 
from the previous content is deleted. 
testReplacingWithHugeImageRemovesOldThumbnail replaces a normal image with an 
over-limit PNG through updateMediaFile() and checks after reload that no 
thumbnail is loaded.



##########
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:
   Fixed in cd42458b2 and a7a105046. 6.1.x's JSPs have no image error fallback, 
so instead of a 404 the servlet answers a t=true request for an over-limit 
image without a thumbnail with the 16x16 /images/page.png file icon. The media 
grid and chooser also skip the thumbnail request for these images and show 
their existing file icons, and inserting one into an entry uses its full URL. 
Other images without a thumbnail are still served in full as before. 
MediaResourceThumbnailTest covers the servlet path.



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