fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ptuom...@apache.org
Subject [fineract] 03/04: Factor out ImageData.resize into ImageResizer (FINERACT-1201)
Date Fri, 06 Nov 2020 03:27:28 GMT
This is an automated email from the ASF dual-hosted git repository.

ptuomola pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git

commit 465623d5ae9481fea31b55191c61c659a7ba5787
Author: Michael Vorburger <mike@vorburger.ch>
AuthorDate: Sun Oct 18 02:51:16 2020 +0200

    Factor out ImageData.resize into ImageResizer (FINERACT-1201)
---
 .../documentmanagement/api/ImagesApiResource.java  |  9 ++-
 .../documentmanagement/data/ImageData.java         | 55 +--------------
 .../data/{ImageData.java => ImageResizer.java}     | 80 ++--------------------
 3 files changed, 11 insertions(+), 133 deletions(-)

diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java
index 1bf5c12..851af09 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/api/ImagesApiResource.java
@@ -44,6 +44,7 @@ import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSer
 import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils;
 import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils.ImageFileExtension;
 import org.apache.fineract.infrastructure.documentmanagement.data.ImageData;
+import org.apache.fineract.infrastructure.documentmanagement.data.ImageResizer;
 import org.apache.fineract.infrastructure.documentmanagement.exception.ContentManagementException;
 import org.apache.fineract.infrastructure.documentmanagement.exception.InvalidEntityTypeForImageManagementException;
 import org.apache.fineract.infrastructure.documentmanagement.service.ImageReadPlatformService;
@@ -64,16 +65,18 @@ public class ImagesApiResource {
     private final ImageWritePlatformService imageWritePlatformService;
     private final DefaultToApiJsonSerializer<ClientData> toApiJsonSerializer;
     private final FileUploadValidator fileUploadValidator;
+    private final ImageResizer imageResizer;
 
     @Autowired
     public ImagesApiResource(final PlatformSecurityContext context, final ImageReadPlatformService
readPlatformService,
             final ImageWritePlatformService imageWritePlatformService, final DefaultToApiJsonSerializer<ClientData>
toApiJsonSerializer,
-            final FileUploadValidator fileUploadValidator) {
+            final FileUploadValidator fileUploadValidator, final ImageResizer imageResizer)
{
         this.context = context;
         this.imageReadPlatformService = readPlatformService;
         this.imageWritePlatformService = imageWritePlatformService;
         this.toApiJsonSerializer = toApiJsonSerializer;
         this.fileUploadValidator = fileUploadValidator;
+        this.imageResizer = imageResizer;
     }
 
     /**
@@ -146,7 +149,7 @@ public class ImagesApiResource {
             imageDataURISuffix = ContentRepositoryUtils.ImageDataURIsuffix.PNG.getValue();
         }
 
-        ImageData resizedImage = imageData.resize(imageData, maxWidth, maxHeight);
+        ImageData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
         try {
             byte[] resizedImageBytes = resizedImage.getInputStream().readAllBytes();
             final String clientImageAsBase64Text = imageDataURISuffix + Base64.getMimeEncoder().encodeToString(resizedImageBytes);
@@ -170,7 +173,7 @@ public class ImagesApiResource {
         }
 
         final ImageData imageData = this.imageReadPlatformService.retrieveImage(entityName,
entityId);
-        final ImageData resizedImage = imageData.resize(imageData, maxWidth, maxHeight);
+        final ImageData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
         final ResponseBuilder response = Response.ok(resizedImage.getInputStream());
         final String dispositionType = "inline_octet".equals(output) ? "inline" : "attachment";
         response.header("Content-Disposition",
diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageData.java
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageData.java
index e0509b5..3f7df51 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageData.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageData.java
@@ -18,20 +18,12 @@
  */
 package org.apache.fineract.infrastructure.documentmanagement.data;
 
-import java.awt.Color;
-import java.awt.Graphics2D;
-import java.awt.RenderingHints;
-import java.awt.image.BufferedImage;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.IOException;
+import java.io.FileNotFoundException;
 import java.io.InputStream;
-import javax.imageio.ImageIO;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils;
-import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils.ImageFileExtension;
 import org.apache.fineract.infrastructure.documentmanagement.domain.StorageType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -54,51 +46,6 @@ public class ImageData {
         this.entityDisplayName = entityDisplayName;
     }
 
-    public ImageData resize(ImageData image, Integer maxWidth, Integer maxHeight) {
-        if (maxWidth == null && maxHeight != null) {
-            return image;
-        }
-        try (InputStream is = image.getInputStream()) {
-            InputStream resizedIS = resizeImage(image.getFileExtension(), is, maxWidth !=
null ? maxWidth : Integer.MAX_VALUE,
-                    maxHeight != null ? maxHeight : Integer.MAX_VALUE);
-
-            ImageData resizedImage = new ImageData(image.location(), image.storageType(),
image.getEntityDisplayName());
-            resizedImage.updateContent(resizedIS);
-            return resizedImage;
-        } catch (IOException e) {
-            LOG.warn("resize() failed, returning original image: {}", e.getMessage(), e);
-            return image;
-        }
-    }
-
-    private InputStream resizeImage(ImageFileExtension fileExtension, InputStream in, int
maxWidth, int maxHeight) throws IOException {
-        ByteArrayOutputStream os = new ByteArrayOutputStream();
-
-        BufferedImage src = ImageIO.read(in);
-        if (src.getWidth() <= maxWidth && src.getHeight() <= maxHeight) {
-            in.reset();
-            return in;
-        }
-        float widthRatio = (float) src.getWidth() / maxWidth;
-        float heightRatio = (float) src.getHeight() / maxHeight;
-        float scaleRatio = widthRatio > heightRatio ? widthRatio : heightRatio;
-
-        // TODO(lindahl): Improve compressed image quality (perhaps quality ratio)
-
-        int newWidth = (int) (src.getWidth() / scaleRatio);
-        int newHeight = (int) (src.getHeight() / scaleRatio);
-        int colorModel = fileExtension == ContentRepositoryUtils.ImageFileExtension.JPEG
? BufferedImage.TYPE_INT_RGB
-                : BufferedImage.TYPE_INT_ARGB;
-        BufferedImage target = new BufferedImage(newWidth, newHeight, colorModel);
-        Graphics2D g = target.createGraphics();
-        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
-        g.drawImage(src, 0, 0, newWidth, newHeight, Color.BLACK, null);
-        g.dispose();
-        ImageIO.write(target, fileExtension != null ? fileExtension.getValueWithoutDot()
: "jpeg", os);
-
-        return new ByteArrayInputStream(os.toByteArray());
-    }
-
     private void setImageFileExtension(String filename) {
         fileExtension = ContentRepositoryUtils.ImageFileExtension.JPEG;
 
diff --git a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageData.java
b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageResizer.java
similarity index 60%
copy from fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageData.java
copy to fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageResizer.java
index e0509b5..9241ea6 100644
--- a/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageData.java
+++ b/fineract-provider/src/main/java/org/apache/fineract/infrastructure/documentmanagement/data/ImageResizer.java
@@ -24,35 +24,19 @@ import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import javax.imageio.ImageIO;
-import org.apache.commons.lang3.StringUtils;
 import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils;
 import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils.ImageFileExtension;
-import org.apache.fineract.infrastructure.documentmanagement.domain.StorageType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
 
-public class ImageData {
+@Service
+public class ImageResizer {
 
-    private static final Logger LOG = LoggerFactory.getLogger(ImageData.class);
-
-    private final String location;
-    private final StorageType storageType;
-    private final String entityDisplayName;
-
-    private File file;
-    private ContentRepositoryUtils.ImageFileExtension fileExtension;
-    private InputStream inputStream;
-
-    public ImageData(final String location, final StorageType storageType, final String entityDisplayName)
{
-        this.location = location;
-        this.storageType = storageType;
-        this.entityDisplayName = entityDisplayName;
-    }
+    private static final Logger LOG = LoggerFactory.getLogger(ImageResizer.class);
 
     public ImageData resize(ImageData image, Integer maxWidth, Integer maxHeight) {
         if (maxWidth == null && maxHeight != null) {
@@ -98,60 +82,4 @@ public class ImageData {
 
         return new ByteArrayInputStream(os.toByteArray());
     }
-
-    private void setImageFileExtension(String filename) {
-        fileExtension = ContentRepositoryUtils.ImageFileExtension.JPEG;
-
-        if (StringUtils.endsWith(filename.toLowerCase(), ContentRepositoryUtils.ImageFileExtension.GIF.getValue()))
{
-            fileExtension = ContentRepositoryUtils.ImageFileExtension.GIF;
-        } else if (StringUtils.endsWith(filename, ContentRepositoryUtils.ImageFileExtension.PNG.getValue()))
{
-            fileExtension = ContentRepositoryUtils.ImageFileExtension.PNG;
-        }
-    }
-
-    public void updateContent(final File file) {
-        this.file = file;
-        if (this.file != null) {
-            setImageFileExtension(this.file.getName());
-        }
-    }
-
-    public void updateContent(final InputStream objectContent) {
-        this.inputStream = objectContent;
-    }
-
-    public InputStream getInputStream() {
-        if (this.file != null) {
-            try {
-                return new FileInputStream(this.file);
-            } catch (FileNotFoundException e) {
-                throw new IllegalStateException("FileNotFoundException: " + file, e);
-            }
-        }
-        return this.inputStream;
-    }
-
-    public String contentType() {
-        return ContentRepositoryUtils.ImageMIMEtype.fromFileExtension(this.fileExtension).getValue();
-    }
-
-    public ContentRepositoryUtils.ImageFileExtension getFileExtension() {
-        return this.fileExtension;
-    }
-
-    public StorageType storageType() {
-        return this.storageType;
-    }
-
-    public String name() {
-        return this.file.getName();
-    }
-
-    public String location() {
-        return this.location;
-    }
-
-    public String getEntityDisplayName() {
-        return this.entityDisplayName;
-    }
 }


Mime
View raw message