Merge branch 'AmazonS3ImageFix' into develop
This commit is contained in:
+7
-2
@@ -38,6 +38,7 @@ import com.amazonaws.AmazonServiceException;
|
|||||||
import com.amazonaws.auth.BasicAWSCredentials;
|
import com.amazonaws.auth.BasicAWSCredentials;
|
||||||
import com.amazonaws.services.s3.AmazonS3;
|
import com.amazonaws.services.s3.AmazonS3;
|
||||||
import com.amazonaws.services.s3.AmazonS3Client;
|
import com.amazonaws.services.s3.AmazonS3Client;
|
||||||
|
import com.amazonaws.services.s3.model.AmazonS3Exception;
|
||||||
import com.amazonaws.services.s3.model.DeleteObjectRequest;
|
import com.amazonaws.services.s3.model.DeleteObjectRequest;
|
||||||
import com.amazonaws.services.s3.model.GetObjectRequest;
|
import com.amazonaws.services.s3.model.GetObjectRequest;
|
||||||
import com.amazonaws.services.s3.model.ObjectMetadata;
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||||
@@ -134,8 +135,12 @@ public class S3ContentRepository implements ContentRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ImageData fetchImage(final ImageData imageData) {
|
public ImageData fetchImage(final ImageData imageData) {
|
||||||
final S3Object s3object = this.s3Client.getObject(new GetObjectRequest(this.s3BucketName, imageData.location()));
|
try {
|
||||||
imageData.updateContent(s3object.getObjectContent());
|
final S3Object s3object = this.s3Client.getObject(new GetObjectRequest(this.s3BucketName, imageData.location()));
|
||||||
|
imageData.updateContent(s3object.getObjectContent());
|
||||||
|
}catch(AmazonS3Exception e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
}
|
||||||
return imageData;
|
return imageData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+66
-20
@@ -25,6 +25,7 @@ import java.awt.image.BufferedImage;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -35,9 +36,13 @@ import org.apache.commons.lang.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.apache.poi.util.IOUtils;
|
import org.apache.poi.util.IOUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class ImageData {
|
public class ImageData {
|
||||||
|
|
||||||
|
private final static Logger logger = LoggerFactory.getLogger(ImageData.class);
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private final Long imageId;
|
private final Long imageId;
|
||||||
private final String location;
|
private final String location;
|
||||||
@@ -56,17 +61,17 @@ public class ImageData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getContent() {
|
public byte[] getContent() {
|
||||||
// TODO Vishwas Fix error handling
|
|
||||||
try {
|
try {
|
||||||
if (this.inputStream == null) {
|
if (this.storageType.equals(StorageType.S3.getValue()) && this.inputStream != null) {
|
||||||
|
return IOUtils.toByteArray(this.inputStream);
|
||||||
|
} else if (this.storageType.equals(StorageType.FILE_SYSTEM.getValue()) && this.file != null) {
|
||||||
final FileInputStream fileInputStream = new FileInputStream(this.file);
|
final FileInputStream fileInputStream = new FileInputStream(this.file);
|
||||||
return IOUtils.toByteArray(fileInputStream);
|
return IOUtils.toByteArray(fileInputStream);
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
return IOUtils.toByteArray(this.inputStream);
|
logger.error(e.getMessage());
|
||||||
} catch (final IOException e) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] resizeImage(InputStream in, int maxWidth, int maxHeight) throws IOException {
|
public byte[] resizeImage(InputStream in, int maxWidth, int maxHeight) throws IOException {
|
||||||
@@ -102,22 +107,33 @@ public class ImageData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getContentOfSize(Integer maxWidth, Integer maxHeight) {
|
public byte[] getContentOfSize(Integer maxWidth, Integer maxHeight) {
|
||||||
if (maxWidth == null && maxHeight == null) { return getContent(); }
|
if (maxWidth == null && maxHeight != null) { return getContent(); }
|
||||||
FileInputStream fis = null;
|
byte[] out = null;
|
||||||
try {
|
if (this.storageType.equals(StorageType.S3.getValue()) && this.inputStream != null) {
|
||||||
fis = new FileInputStream(this.file);
|
try {
|
||||||
byte[] out = resizeImage(fis, maxWidth != null ? maxWidth : Integer.MAX_VALUE, maxHeight != null ? maxHeight
|
out = resizeImage(this.inputStream, maxWidth != null ? maxWidth : Integer.MAX_VALUE,
|
||||||
: Integer.MAX_VALUE);
|
maxHeight != null ? maxHeight : Integer.MAX_VALUE);
|
||||||
return out;
|
} catch (IOException e) {
|
||||||
} catch (IOException ex) {
|
logger.error(e.getMessage());
|
||||||
return null;
|
}
|
||||||
} finally {
|
} else if (this.storageType.equals(StorageType.FILE_SYSTEM.getValue()) && this.file != null) {
|
||||||
if (fis != null) {
|
FileInputStream fis = null;
|
||||||
try {
|
try {
|
||||||
fis.close();
|
fis = new FileInputStream(this.file);
|
||||||
} catch (IOException ex) {}
|
out = resizeImage(fis, maxWidth != null ? maxWidth : Integer.MAX_VALUE, maxHeight != null ? maxHeight : Integer.MAX_VALUE);
|
||||||
|
} catch (IOException ex) {
|
||||||
|
logger.error(ex.getMessage());
|
||||||
|
} finally {
|
||||||
|
if (fis != null) {
|
||||||
|
try {
|
||||||
|
fis.close();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
logger.error(ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setImageContentType(String filename) {
|
private void setImageContentType(String filename) {
|
||||||
@@ -161,4 +177,34 @@ public class ImageData {
|
|||||||
return this.entityDisplayName;
|
return this.entityDisplayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean available() {
|
||||||
|
int available = -1; // not -1
|
||||||
|
if (this.storageType.equals(StorageType.S3.getValue()) && this.inputStream != null) {
|
||||||
|
try {
|
||||||
|
available = this.inputStream.available();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
}
|
||||||
|
} else if (this.storageType.equals(StorageType.FILE_SYSTEM.getValue()) && this.file != null) {
|
||||||
|
FileInputStream fileInputStream = null;
|
||||||
|
try {
|
||||||
|
fileInputStream = new FileInputStream(this.file);
|
||||||
|
available = fileInputStream.available();
|
||||||
|
fileInputStream.close();
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error(e.getMessage());
|
||||||
|
} finally {
|
||||||
|
if (fileInputStream != null) {
|
||||||
|
try {
|
||||||
|
fileInputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return available >= 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -103,7 +103,9 @@ public class ImageReadPlatformServiceImpl implements ImageReadPlatformService {
|
|||||||
final ContentRepository contentRepository = this.contentRepositoryFactory.getRepository(imageData.storageType());
|
final ContentRepository contentRepository = this.contentRepositoryFactory.getRepository(imageData.storageType());
|
||||||
final ImageData result = contentRepository.fetchImage(imageData);
|
final ImageData result = contentRepository.fetchImage(imageData);
|
||||||
|
|
||||||
if (result.getContent() == null) { throw new ImageNotFoundException(entityType, entityId); }
|
//Once we read content EofSensorInputStream, the wrappedStream object is becoming null. So further image source is becoming null
|
||||||
|
//For Amazon S3. If file is not present, already S3ContentRepository would have thrown this exception.
|
||||||
|
if (!result.available()) { throw new ImageNotFoundException(entityType, entityId); }
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (final EmptyResultDataAccessException e) {
|
} catch (final EmptyResultDataAccessException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user