Replace use of InputStream by ByteSource in ImageData (FINERACT-1201)
This commit is contained in:
committed by
Petri Tuomola
parent
465623d5ae
commit
140c55b7f2
+22
-9
@@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.fineract.infrastructure.documentmanagement.api;
|
package org.apache.fineract.infrastructure.documentmanagement.api;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
import com.sun.jersey.core.header.FormDataContentDisposition;
|
import com.sun.jersey.core.header.FormDataContentDisposition;
|
||||||
import com.sun.jersey.multipart.FormDataBodyPart;
|
import com.sun.jersey.multipart.FormDataBodyPart;
|
||||||
import com.sun.jersey.multipart.FormDataParam;
|
import com.sun.jersey.multipart.FormDataParam;
|
||||||
@@ -51,6 +52,8 @@ import org.apache.fineract.infrastructure.documentmanagement.service.ImageReadPl
|
|||||||
import org.apache.fineract.infrastructure.documentmanagement.service.ImageWritePlatformService;
|
import org.apache.fineract.infrastructure.documentmanagement.service.ImageWritePlatformService;
|
||||||
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
|
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
|
||||||
import org.apache.fineract.portfolio.client.data.ClientData;
|
import org.apache.fineract.portfolio.client.data.ClientData;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -60,6 +63,8 @@ import org.springframework.stereotype.Component;
|
|||||||
@Path("{entity}/{entityId}/images")
|
@Path("{entity}/{entityId}/images")
|
||||||
public class ImagesApiResource {
|
public class ImagesApiResource {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ImagesApiResource.class);
|
||||||
|
|
||||||
private final PlatformSecurityContext context;
|
private final PlatformSecurityContext context;
|
||||||
private final ImageReadPlatformService imageReadPlatformService;
|
private final ImageReadPlatformService imageReadPlatformService;
|
||||||
private final ImageWritePlatformService imageWritePlatformService;
|
private final ImageWritePlatformService imageWritePlatformService;
|
||||||
@@ -151,7 +156,7 @@ public class ImagesApiResource {
|
|||||||
|
|
||||||
ImageData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
|
ImageData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
|
||||||
try {
|
try {
|
||||||
byte[] resizedImageBytes = resizedImage.getInputStream().readAllBytes();
|
byte[] resizedImageBytes = resizedImage.getByteSource().read();
|
||||||
final String clientImageAsBase64Text = imageDataURISuffix + Base64.getMimeEncoder().encodeToString(resizedImageBytes);
|
final String clientImageAsBase64Text = imageDataURISuffix + Base64.getMimeEncoder().encodeToString(resizedImageBytes);
|
||||||
return Response.ok(clientImageAsBase64Text).build();
|
return Response.ok(clientImageAsBase64Text).build();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -174,14 +179,22 @@ public class ImagesApiResource {
|
|||||||
|
|
||||||
final ImageData imageData = this.imageReadPlatformService.retrieveImage(entityName, entityId);
|
final ImageData imageData = this.imageReadPlatformService.retrieveImage(entityName, entityId);
|
||||||
final ImageData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
|
final ImageData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
|
||||||
final ResponseBuilder response = Response.ok(resizedImage.getInputStream());
|
ResponseBuilder response;
|
||||||
final String dispositionType = "inline_octet".equals(output) ? "inline" : "attachment";
|
try {
|
||||||
response.header("Content-Disposition",
|
ByteSource byteSource = resizedImage.getByteSource();
|
||||||
dispositionType + "; filename=\"" + imageData.getEntityDisplayName() + ImageFileExtension.JPEG + "\"");
|
// TODO Where is this InputStream closed?! It needs to be AFTER it's read by JAX-RS.. how to do that?
|
||||||
|
InputStream is = byteSource.openBufferedStream();
|
||||||
// TODO: Need a better way of determining image type
|
response = Response.ok(is);
|
||||||
|
final String dispositionType = "inline_octet".equals(output) ? "inline" : "attachment";
|
||||||
response.header("Content-Type", imageData.contentType());
|
response.header("Content-Disposition",
|
||||||
|
dispositionType + "; filename=\"" + imageData.getEntityDisplayName() + ImageFileExtension.JPEG + "\"");
|
||||||
|
response.header("Content-Length", byteSource.sizeIfKnown().or(-1L));
|
||||||
|
// TODO: Need a better way of determining image type
|
||||||
|
response.header("Content-Type", imageData.contentType());
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOG.error("resizedImage.getByteSource().openBufferedStream() failed", e);
|
||||||
|
response = Response.serverError();
|
||||||
|
}
|
||||||
return response.build();
|
return response.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-2
@@ -29,8 +29,10 @@ import com.amazonaws.services.s3.model.GetObjectRequest;
|
|||||||
import com.amazonaws.services.s3.model.ObjectMetadata;
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||||
import com.amazonaws.services.s3.model.PutObjectRequest;
|
import com.amazonaws.services.s3.model.PutObjectRequest;
|
||||||
import com.amazonaws.services.s3.model.S3Object;
|
import com.amazonaws.services.s3.model.S3Object;
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import org.apache.fineract.infrastructure.core.domain.Base64EncodedImage;
|
import org.apache.fineract.infrastructure.core.domain.Base64EncodedImage;
|
||||||
@@ -109,8 +111,14 @@ public class S3ContentRepository implements ContentRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ImageData fetchImage(final ImageData imageData) {
|
public ImageData fetchImage(final ImageData imageData) {
|
||||||
final S3Object s3object = getObject(imageData.location());
|
imageData.updateContent(new ByteSource() {
|
||||||
imageData.updateContent(s3object.getObjectContent());
|
|
||||||
|
@Override
|
||||||
|
public InputStream openStream() throws IOException {
|
||||||
|
final S3Object s3object = getObject(imageData.location());
|
||||||
|
return s3object.getObjectContent();
|
||||||
|
}
|
||||||
|
});
|
||||||
return imageData;
|
return imageData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-17
@@ -18,27 +18,22 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.fineract.infrastructure.documentmanagement.data;
|
package org.apache.fineract.infrastructure.documentmanagement.data;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
|
import com.google.common.io.Files;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils;
|
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils;
|
||||||
import org.apache.fineract.infrastructure.documentmanagement.domain.StorageType;
|
import org.apache.fineract.infrastructure.documentmanagement.domain.StorageType;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
public class ImageData {
|
public class ImageData {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ImageData.class);
|
|
||||||
|
|
||||||
private final String location;
|
private final String location;
|
||||||
private final StorageType storageType;
|
private final StorageType storageType;
|
||||||
private final String entityDisplayName;
|
private final String entityDisplayName;
|
||||||
|
|
||||||
private File file;
|
private File file;
|
||||||
|
private ByteSource byteSource;
|
||||||
private ContentRepositoryUtils.ImageFileExtension fileExtension;
|
private ContentRepositoryUtils.ImageFileExtension fileExtension;
|
||||||
private InputStream inputStream;
|
|
||||||
|
|
||||||
public ImageData(final String location, final StorageType storageType, final String entityDisplayName) {
|
public ImageData(final String location, final StorageType storageType, final String entityDisplayName) {
|
||||||
this.location = location;
|
this.location = location;
|
||||||
@@ -63,19 +58,15 @@ public class ImageData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateContent(final InputStream objectContent) {
|
public void updateContent(final ByteSource byteSource) {
|
||||||
this.inputStream = objectContent;
|
this.byteSource = byteSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() {
|
public ByteSource getByteSource() {
|
||||||
if (this.file != null) {
|
if (this.file != null) {
|
||||||
try {
|
return Files.asByteSource(file);
|
||||||
return new FileInputStream(this.file);
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
throw new IllegalStateException("FileNotFoundException: " + file, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this.inputStream;
|
return this.byteSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String contentType() {
|
public String contentType() {
|
||||||
|
|||||||
+21
-12
@@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.fineract.infrastructure.documentmanagement.data;
|
package org.apache.fineract.infrastructure.documentmanagement.data;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.RenderingHints;
|
import java.awt.RenderingHints;
|
||||||
@@ -26,6 +27,7 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Optional;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils;
|
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils;
|
||||||
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils.ImageFileExtension;
|
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils.ImageFileExtension;
|
||||||
@@ -42,26 +44,32 @@ public class ImageResizer {
|
|||||||
if (maxWidth == null && maxHeight != null) {
|
if (maxWidth == null && maxHeight != null) {
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
try (InputStream is = image.getInputStream()) {
|
try (InputStream is = image.getByteSource().openBufferedStream()) {
|
||||||
InputStream resizedIS = resizeImage(image.getFileExtension(), is, maxWidth != null ? maxWidth : Integer.MAX_VALUE,
|
Optional<InputStream> optResizedIS = resizeImage(image.getFileExtension(), is, maxWidth != null ? maxWidth : Integer.MAX_VALUE,
|
||||||
maxHeight != null ? maxHeight : Integer.MAX_VALUE);
|
maxHeight != null ? maxHeight : Integer.MAX_VALUE);
|
||||||
|
if (optResizedIS.isPresent()) {
|
||||||
|
ImageData resizedImage = new ImageData(image.location(), image.storageType(), image.getEntityDisplayName());
|
||||||
|
resizedImage.updateContent(new ByteSource() {
|
||||||
|
|
||||||
ImageData resizedImage = new ImageData(image.location(), image.storageType(), image.getEntityDisplayName());
|
@Override
|
||||||
resizedImage.updateContent(resizedIS);
|
public InputStream openStream() throws IOException {
|
||||||
return resizedImage;
|
return optResizedIS.get();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return resizedImage;
|
||||||
|
}
|
||||||
|
return image;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOG.warn("resize() failed, returning original image: {}", e.getMessage(), e);
|
LOG.warn("resize() failed, returning original image: {}", e.getMessage(), e);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream resizeImage(ImageFileExtension fileExtension, InputStream in, int maxWidth, int maxHeight) throws IOException {
|
private Optional<InputStream> resizeImage(ImageFileExtension fileExtension, InputStream in, int maxWidth, int maxHeight)
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
throws IOException {
|
||||||
|
|
||||||
BufferedImage src = ImageIO.read(in);
|
BufferedImage src = ImageIO.read(in);
|
||||||
if (src.getWidth() <= maxWidth && src.getHeight() <= maxHeight) {
|
if (src.getWidth() <= maxWidth && src.getHeight() <= maxHeight) {
|
||||||
in.reset();
|
return Optional.empty();
|
||||||
return in;
|
|
||||||
}
|
}
|
||||||
float widthRatio = (float) src.getWidth() / maxWidth;
|
float widthRatio = (float) src.getWidth() / maxWidth;
|
||||||
float heightRatio = (float) src.getHeight() / maxHeight;
|
float heightRatio = (float) src.getHeight() / maxHeight;
|
||||||
@@ -78,8 +86,9 @@ public class ImageResizer {
|
|||||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||||
g.drawImage(src, 0, 0, newWidth, newHeight, Color.BLACK, null);
|
g.drawImage(src, 0, 0, newWidth, newHeight, Color.BLACK, null);
|
||||||
g.dispose();
|
g.dispose();
|
||||||
ImageIO.write(target, fileExtension != null ? fileExtension.getValueWithoutDot() : "jpeg", os);
|
|
||||||
|
|
||||||
return new ByteArrayInputStream(os.toByteArray());
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(target, fileExtension != null ? fileExtension.getValueWithoutDot() : "jpeg", os);
|
||||||
|
return Optional.of(new ByteArrayInputStream(os.toByteArray()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user