Clean up ImageData (FINERACT-1201)

This commit is contained in:
Michael Vorburger
2020-11-06 05:27:17 +02:00
committed by Petri Tuomola
parent 814455769b
commit 0af9aa3d0c
3 changed files with 15 additions and 21 deletions
@@ -37,9 +37,7 @@ import org.springframework.jdbc.support.JdbcUtils;
*/ */
public final class JdbcSupport { public final class JdbcSupport {
private JdbcSupport() { private JdbcSupport() {}
}
public static ZonedDateTime getDateTime(final ResultSet rs, final String columnName) throws SQLException { public static ZonedDateTime getDateTime(final ResultSet rs, final String columnName) throws SQLException {
ZonedDateTime dateTime = null; ZonedDateTime dateTime = null;
@@ -40,28 +40,25 @@ public class ImageData {
private static final Logger LOG = LoggerFactory.getLogger(ImageData.class); private static final Logger LOG = LoggerFactory.getLogger(ImageData.class);
@SuppressWarnings("unused")
private final Long imageId;
private final String location; private final String location;
private final Integer storageType; private final StorageType storageType;
private final String entityDisplayName; private final String entityDisplayName;
private File file; private File file;
private ContentRepositoryUtils.ImageFileExtension fileExtension; private ContentRepositoryUtils.ImageFileExtension fileExtension;
private InputStream inputStream; private InputStream inputStream;
public ImageData(final Long imageId, final String location, final Integer storageType, final String entityDisplayName) { public ImageData(final String location, final StorageType storageType, final String entityDisplayName) {
this.imageId = imageId;
this.location = location; this.location = location;
this.storageType = storageType; this.storageType = storageType;
this.entityDisplayName = entityDisplayName; this.entityDisplayName = entityDisplayName;
} }
public byte[] getContent() { private byte[] getContent() {
try { try {
if (this.storageType.equals(StorageType.S3.getValue()) && this.inputStream != null) { if (this.storageType.equals(StorageType.S3) && this.inputStream != null) {
return IOUtils.toByteArray(this.inputStream); return IOUtils.toByteArray(this.inputStream);
} else if (this.storageType.equals(StorageType.FILE_SYSTEM.getValue()) && this.file != null) { } else if (this.storageType.equals(StorageType.FILE_SYSTEM) && 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);
} }
@@ -71,14 +68,13 @@ public class ImageData {
return null; return null;
} }
public byte[] resizeImage(InputStream in, int maxWidth, int maxHeight) throws IOException { private byte[] resizeImage(InputStream in, int maxWidth, int maxHeight) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
resizeImage(in, out, maxWidth, maxHeight); resizeImage(in, out, maxWidth, maxHeight);
return out.toByteArray(); return out.toByteArray();
} }
public void resizeImage(InputStream in, OutputStream out, int maxWidth, int maxHeight) throws IOException { private void resizeImage(InputStream in, OutputStream out, int maxWidth, int maxHeight) 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) {
out.write(getContent()); out.write(getContent());
@@ -108,14 +104,14 @@ public class ImageData {
return getContent(); return getContent();
} }
byte[] out = null; byte[] out = null;
if (this.storageType.equals(StorageType.S3.getValue()) && this.inputStream != null) { if (this.storageType.equals(StorageType.S3) && this.inputStream != null) {
try { try {
out = resizeImage(this.inputStream, maxWidth != null ? maxWidth : Integer.MAX_VALUE, out = resizeImage(this.inputStream, maxWidth != null ? maxWidth : Integer.MAX_VALUE,
maxHeight != null ? maxHeight : Integer.MAX_VALUE); maxHeight != null ? maxHeight : Integer.MAX_VALUE);
} catch (IOException e) { } catch (IOException e) {
LOG.error("Error occured.", e); LOG.error("Error occured.", e);
} }
} else if (this.storageType.equals(StorageType.FILE_SYSTEM.getValue()) && this.file != null) { } else if (this.storageType.equals(StorageType.FILE_SYSTEM) && this.file != null) {
FileInputStream fis = null; FileInputStream fis = null;
try { try {
fis = new FileInputStream(this.file); fis = new FileInputStream(this.file);
@@ -157,7 +153,7 @@ public class ImageData {
} }
public StorageType storageType() { public StorageType storageType() {
return StorageType.fromInt(this.storageType); return this.storageType;
} }
public String name() { public String name() {
@@ -26,6 +26,7 @@ import org.apache.fineract.infrastructure.documentmanagement.api.ImagesApiResour
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepository; import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepository;
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryFactory; import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryFactory;
import org.apache.fineract.infrastructure.documentmanagement.data.ImageData; import org.apache.fineract.infrastructure.documentmanagement.data.ImageData;
import org.apache.fineract.infrastructure.documentmanagement.domain.StorageType;
import org.apache.fineract.organisation.staff.domain.Staff; import org.apache.fineract.organisation.staff.domain.Staff;
import org.apache.fineract.organisation.staff.domain.StaffRepositoryWrapper; import org.apache.fineract.organisation.staff.domain.StaffRepositoryWrapper;
import org.apache.fineract.portfolio.client.domain.Client; import org.apache.fineract.portfolio.client.domain.Client;
@@ -75,11 +76,10 @@ public class ImageReadPlatformServiceImpl implements ImageReadPlatformService {
@Override @Override
public ImageData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { public ImageData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = JdbcSupport.getLong(rs, "id");
final String location = rs.getString("location"); final String location = rs.getString("location");
final Integer storageType = JdbcSupport.getInteger(rs, "storageType"); final Integer storageTypeInt = JdbcSupport.getInteger(rs, "storageType");
return new ImageData(id, location, storageType, this.entityDisplayName); StorageType storageType = storageTypeInt != null ? StorageType.fromInt(storageTypeInt) : null;
return new ImageData(location, storageType, this.entityDisplayName);
} }
} }