ContentRepository.fetchImage() FileData not ImageData (FINERACT-1201)

This is more consistent with fetchFile(), and allows to re-use code.
This commit is contained in:
Michael Vorburger
2020-11-19 10:25:32 +01:00
committed by Percy Enoabane
parent 3e1444a6e4
commit 6c853a6cc7
15 changed files with 148 additions and 161 deletions
@@ -0,0 +1,61 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.documentmanagement.api;
import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.apache.fineract.infrastructure.documentmanagement.data.FileData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utilities common to file upload/download resources.
*
* @author Michael Vorburger.ch
*/
final class ContentResources {
private static final Logger LOG = LoggerFactory.getLogger(ContentResources.class);
private ContentResources() {}
static Response fileDataToResponse(FileData fileData, String fileName, String dispositionType) {
ResponseBuilder response;
try {
ByteSource byteSource = fileData.getByteSource();
// 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();
response = Response.ok(is);
response.header("Content-Disposition", dispositionType + "; filename=\"" + fileName + "\"");
response.header("Content-Length", byteSource.sizeIfKnown().or(-1L));
response.header("Content-Type", fileData.contentType());
} catch (IOException e) {
LOG.error("resizedImage.getByteSource().openBufferedStream() failed", e);
response = Response.serverError();
}
return response.build();
}
static Response fileDataToResponse(FileData fileData, String dispositionType) {
return fileDataToResponse(fileData, fileData.name(), dispositionType);
}
}
@@ -46,7 +46,6 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriInfo;
import org.apache.fineract.infrastructure.core.api.ApiRequestParameterHelper;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
@@ -225,13 +224,8 @@ public class DocumentManagementApiResource {
@PathParam("documentId") @Parameter(description = "documentId") final Long documentId) {
this.context.authenticatedUser().validateHasReadPermission(this.systemEntityType);
final FileData fileData = this.documentReadPlatformService.retrieveFileData(entityType, entityId, documentId);
final ResponseBuilder response = Response.ok(fileData.inputStream());
response.header("Content-Disposition", "attachment; filename=\"" + fileData.name() + "\"");
response.header("Content-Type", fileData.contentType());
return response.build();
return ContentResources.fileDataToResponse(fileData, "attachment");
}
@DELETE
@@ -18,7 +18,6 @@
*/
package org.apache.fineract.infrastructure.documentmanagement.api;
import com.google.common.io.ByteSource;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataParam;
@@ -37,14 +36,13 @@ import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.domain.Base64EncodedImage;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
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.FileData;
import org.apache.fineract.infrastructure.documentmanagement.data.ImageResizer;
import org.apache.fineract.infrastructure.documentmanagement.exception.ContentManagementException;
import org.apache.fineract.infrastructure.documentmanagement.exception.InvalidEntityTypeForImageManagementException;
@@ -52,8 +50,6 @@ import org.apache.fineract.infrastructure.documentmanagement.service.ImageReadPl
import org.apache.fineract.infrastructure.documentmanagement.service.ImageWritePlatformService;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
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.context.annotation.Scope;
import org.springframework.stereotype.Component;
@@ -63,8 +59,6 @@ import org.springframework.stereotype.Component;
@Path("{entity}/{entityId}/images")
public class ImagesApiResource {
private static final Logger LOG = LoggerFactory.getLogger(ImagesApiResource.class);
private final PlatformSecurityContext context;
private final ImageReadPlatformService imageReadPlatformService;
private final ImageWritePlatformService imageWritePlatformService;
@@ -144,23 +138,23 @@ public class ImagesApiResource {
return downloadClientImage(entityName, entityId, maxWidth, maxHeight, output);
}
final ImageData imageData = this.imageReadPlatformService.retrieveImage(entityName, entityId);
final FileData imageData = this.imageReadPlatformService.retrieveImage(entityName, entityId);
// TODO: Need a better way of determining image type
String imageDataURISuffix = ContentRepositoryUtils.ImageDataURIsuffix.JPEG.getValue();
if (StringUtils.endsWith(imageData.location(), ContentRepositoryUtils.ImageFileExtension.GIF.getValue())) {
if (StringUtils.endsWith(imageData.name(), ContentRepositoryUtils.ImageFileExtension.GIF.getValue())) {
imageDataURISuffix = ContentRepositoryUtils.ImageDataURIsuffix.GIF.getValue();
} else if (StringUtils.endsWith(imageData.location(), ContentRepositoryUtils.ImageFileExtension.PNG.getValue())) {
} else if (StringUtils.endsWith(imageData.name(), ContentRepositoryUtils.ImageFileExtension.PNG.getValue())) {
imageDataURISuffix = ContentRepositoryUtils.ImageDataURIsuffix.PNG.getValue();
}
ImageData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
FileData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
try {
byte[] resizedImageBytes = resizedImage.getByteSource().read();
final String clientImageAsBase64Text = imageDataURISuffix + Base64.getMimeEncoder().encodeToString(resizedImageBytes);
return Response.ok(clientImageAsBase64Text).build();
} catch (IOException e) {
throw new ContentManagementException(imageData.getEntityDisplayName(), e.getMessage(), e);
throw new ContentManagementException(imageData.name(), e.getMessage(), e);
}
}
@@ -177,25 +171,10 @@ public class ImagesApiResource {
this.context.authenticatedUser().validateHasReadPermission("STAFFIMAGE");
}
final ImageData imageData = this.imageReadPlatformService.retrieveImage(entityName, entityId);
final ImageData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
ResponseBuilder response;
try {
ByteSource byteSource = resizedImage.getByteSource();
// 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();
response = Response.ok(is);
final String dispositionType = "inline_octet".equals(output) ? "inline" : "attachment";
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();
final FileData imageData = this.imageReadPlatformService.retrieveImage(entityName, entityId);
final FileData resizedImage = imageResizer.resize(imageData, maxWidth, maxHeight);
return ContentResources.fileDataToResponse(resizedImage, resizedImage.name() + ImageFileExtension.JPEG,
"inline_octet".equals(output) ? "inline" : "attachment");
}
/**
@@ -49,7 +49,7 @@ public interface ContentRepository {
void deleteImage(String location);
ImageData fetchImage(ImageData imageData);
FileData fetchImage(ImageData imageData);
StorageType getStorageType();
@@ -113,14 +113,24 @@ public final class ContentRepositoryUtils {
}
}
public static ImageFileExtension imageExtensionFromFileName(String fileName) {
if (StringUtils.endsWith(fileName.toLowerCase(), ContentRepositoryUtils.ImageFileExtension.GIF.getValue())) {
return ContentRepositoryUtils.ImageFileExtension.GIF;
} else if (StringUtils.endsWith(fileName, ContentRepositoryUtils.ImageFileExtension.PNG.getValue())) {
return ContentRepositoryUtils.ImageFileExtension.PNG;
} else {
return ContentRepositoryUtils.ImageFileExtension.JPEG;
}
}
/**
* Validates that passed in Mime type maps to known image mime types
*
* @param mimeType
*/
public static void validateImageMimeType(final String mimeType) {
if (!(mimeType.equalsIgnoreCase(ImageMIMEtype.GIF.getValue()) || mimeType.equalsIgnoreCase(ImageMIMEtype.JPEG.getValue())
|| mimeType.equalsIgnoreCase(ImageMIMEtype.PNG.getValue()))) {
if ((!mimeType.equalsIgnoreCase(ImageMIMEtype.GIF.getValue()) && !mimeType.equalsIgnoreCase(ImageMIMEtype.JPEG.getValue())
&& !mimeType.equalsIgnoreCase(ImageMIMEtype.PNG.getValue()))) {
throw new ImageUploadException(mimeType);
}
}
@@ -99,14 +99,13 @@ public class FileSystemContentRepository implements ContentRepository {
@Override
public FileData fetchFile(final DocumentData documentData) {
final File file = new File(documentData.fileLocation());
return new FileData(file, documentData.fileName(), documentData.contentType());
return new FileData(Files.asByteSource(file), documentData.fileName(), documentData.contentType());
}
@Override
public ImageData fetchImage(final ImageData imageData) {
public FileData fetchImage(final ImageData imageData) {
final File file = new File(imageData.location());
imageData.updateContent(file);
return imageData;
return new FileData(Files.asByteSource(file), imageData.getEntityDisplayName(), imageData.contentType().getValue());
}
@Override
@@ -105,21 +105,26 @@ public class S3ContentRepository implements ContentRepository {
@Override
public FileData fetchFile(final DocumentData documentData) throws DocumentNotFoundException {
final S3Object s3object = getObject(documentData.fileLocation());
return new FileData(s3object.getObjectContent(), documentData.fileName(), documentData.contentType());
return new FileData(new ByteSource() {
@Override
public InputStream openStream() throws IOException {
final S3Object s3object = getObject(documentData.fileLocation());
return s3object.getObjectContent();
}
}, documentData.fileName(), documentData.contentType());
}
@Override
public ImageData fetchImage(final ImageData imageData) {
imageData.updateContent(new ByteSource() {
public FileData fetchImage(final ImageData imageData) {
return new FileData(new ByteSource() {
@Override
public InputStream openStream() throws IOException {
final S3Object s3object = getObject(imageData.location());
return s3object.getObjectContent();
}
});
return imageData;
}, imageData.getEntityDisplayName(), imageData.contentType().getValue());
}
@Override
@@ -21,22 +21,19 @@ package org.apache.fineract.infrastructure.documentmanagement.data;
import org.apache.fineract.infrastructure.documentmanagement.domain.StorageType;
/**
* Immutable data object represent document being managed on platform.
* Immutable data object representing a user document being managed on the platform.
*/
public class DocumentData {
private final Long id;
private final String parentEntityType;
private final Long parentEntityId;
@SuppressWarnings("unused")
private final String name;
private final String fileName;
@SuppressWarnings("unused")
private final Long size;
private final String type;
@SuppressWarnings("unused")
private final String description;
private final String location;
private final String description;
private final Integer storageType;
public DocumentData(final Long id, final String parentEntityType, final Long parentEntityId, final String name, final String fileName,
@@ -81,4 +78,15 @@ public class DocumentData {
return this.id;
}
public String getName() {
return this.name;
}
public String getDescription() {
return this.description;
}
public Long getSize() {
return this.size;
}
}
@@ -18,34 +18,18 @@
*/
package org.apache.fineract.infrastructure.documentmanagement.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.io.ByteSource;
public class FileData {
private static final Logger LOG = LoggerFactory.getLogger(FileData.class);
private final File file;
private final String fileName;
private final String contentType;
private final InputStream inputStream;
private final ByteSource byteSource;
public FileData(final File file, final String fileName, final String contentType) {
this.file = file;
this.fileName = fileName;
this.contentType = contentType;
this.inputStream = null;
}
public FileData(final InputStream inputStream, final String fileName, final String contentType) {
this.file = null;
this.inputStream = inputStream;
public FileData(final ByteSource byteSource, final String fileName, final String contentType) {
this.fileName = fileName;
this.contentType = contentType;
this.byteSource = byteSource;
}
public String contentType() {
@@ -56,15 +40,7 @@ public class FileData {
return this.fileName;
}
public InputStream inputStream() {
try {
if (this.inputStream == null) {
return new FileInputStream(this.file);
}
return this.inputStream;
} catch (final FileNotFoundException e) {
LOG.error("Error occured.", e);
return null;
}
public ByteSource getByteSource() {
return this.byteSource;
}
}
@@ -18,11 +18,8 @@
*/
package org.apache.fineract.infrastructure.documentmanagement.data;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import java.io.File;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils;
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryUtils.ImageMIMEtype;
import org.apache.fineract.infrastructure.documentmanagement.domain.StorageType;
public class ImageData {
@@ -30,61 +27,24 @@ public class ImageData {
private final String location;
private final StorageType storageType;
private final String entityDisplayName;
private File file;
private ByteSource byteSource;
private ContentRepositoryUtils.ImageFileExtension fileExtension;
private final ContentRepositoryUtils.ImageMIMEtype contentType;
public ImageData(final String location, final StorageType storageType, final String entityDisplayName) {
this.location = location;
this.storageType = storageType;
this.entityDisplayName = entityDisplayName;
this.contentType = ContentRepositoryUtils.ImageMIMEtype
.fromFileExtension(ContentRepositoryUtils.imageExtensionFromFileName(location));
}
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 ByteSource byteSource) {
this.byteSource = byteSource;
}
public ByteSource getByteSource() {
if (this.file != null) {
return Files.asByteSource(file);
}
return this.byteSource;
}
public String contentType() {
return ContentRepositoryUtils.ImageMIMEtype.fromFileExtension(this.fileExtension).getValue();
}
public ContentRepositoryUtils.ImageFileExtension getFileExtension() {
return this.fileExtension;
public ImageMIMEtype contentType() {
return this.contentType;
}
public StorageType storageType() {
return this.storageType;
}
public String name() {
return this.file.getName();
}
public String location() {
return this.location;
}
@@ -40,28 +40,27 @@ public class ImageResizer {
private static final Logger LOG = LoggerFactory.getLogger(ImageResizer.class);
public ImageData resize(ImageData image, Integer maxWidth, Integer maxHeight) {
public FileData resize(FileData fileData, Integer maxWidth, Integer maxHeight) {
if (maxWidth == null && maxHeight != null) {
return image;
return fileData;
}
try (InputStream is = image.getByteSource().openBufferedStream()) {
Optional<InputStream> optResizedIS = resizeImage(image.getFileExtension(), is, maxWidth != null ? maxWidth : Integer.MAX_VALUE,
maxHeight != null ? maxHeight : Integer.MAX_VALUE);
try (InputStream is = fileData.getByteSource().openBufferedStream()) {
Optional<InputStream> optResizedIS = resizeImage(ContentRepositoryUtils.imageExtensionFromFileName(fileData.name()), is,
maxWidth != null ? maxWidth : 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() {
FileData resizedImage = new FileData(new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return optResizedIS.get();
}
});
}, fileData.name(), fileData.contentType());
return resizedImage;
}
return image;
return fileData;
} catch (IOException e) {
LOG.warn("resize() failed, returning original image: {}", e.getMessage(), e);
return image;
return fileData;
}
}
@@ -60,7 +60,7 @@ public class DocumentReadPlatformServiceImpl implements DocumentReadPlatformServ
// scope for the particular entities
final DocumentMapper mapper = new DocumentMapper(true, true);
final String sql = "select " + mapper.schema() + " order by d.id";
return this.jdbcTemplate.query(sql, mapper, new Object[] { entityType, entityId });
return this.jdbcTemplate.query(sql, mapper, entityType, entityId);
}
@Override
@@ -85,17 +85,10 @@ public class DocumentReadPlatformServiceImpl implements DocumentReadPlatformServ
}
}
/**
* @param entityType
* @param entityId
* @param documentId
* @param mapper
* @return
*/
private DocumentData fetchDocumentDetails(final String entityType, final Long entityId, final Long documentId,
final DocumentMapper mapper) {
final String sql = "select " + mapper.schema() + " and d.id=? ";
return this.jdbcTemplate.queryForObject(sql, mapper, new Object[] { entityType, entityId, documentId });
return this.jdbcTemplate.queryForObject(sql, mapper, entityType, entityId, documentId);
}
private static final class DocumentMapper implements RowMapper<DocumentData> {
@@ -117,7 +110,6 @@ public class DocumentReadPlatformServiceImpl implements DocumentReadPlatformServ
@Override
public DocumentData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = JdbcSupport.getLong(rs, "id");
final Long parentEntityId = JdbcSupport.getLong(rs, "parentEntityId");
final Long fileSize = JdbcSupport.getLong(rs, "fileSize");
@@ -138,5 +130,4 @@ public class DocumentReadPlatformServiceImpl implements DocumentReadPlatformServ
storageType);
}
}
}
@@ -18,10 +18,10 @@
*/
package org.apache.fineract.infrastructure.documentmanagement.service;
import org.apache.fineract.infrastructure.documentmanagement.data.ImageData;
import org.apache.fineract.infrastructure.documentmanagement.data.FileData;
public interface ImageReadPlatformService {
ImageData retrieveImage(String entityType, Long entityId);
FileData retrieveImage(String entityType, Long entityId);
}
@@ -25,6 +25,7 @@ import org.apache.fineract.infrastructure.core.service.RoutingDataSource;
import org.apache.fineract.infrastructure.documentmanagement.api.ImagesApiResource.EntityTypeForImages;
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepository;
import org.apache.fineract.infrastructure.documentmanagement.contentrepository.ContentRepositoryFactory;
import org.apache.fineract.infrastructure.documentmanagement.data.FileData;
import org.apache.fineract.infrastructure.documentmanagement.data.ImageData;
import org.apache.fineract.infrastructure.documentmanagement.domain.StorageType;
import org.apache.fineract.organisation.staff.domain.Staff;
@@ -84,7 +85,7 @@ public class ImageReadPlatformServiceImpl implements ImageReadPlatformService {
}
@Override
public ImageData retrieveImage(String entityType, final Long entityId) {
public FileData retrieveImage(String entityType, final Long entityId) {
try {
String displayName;
if (EntityTypeForImages.CLIENTS.toString().equalsIgnoreCase(entityType)) {
@@ -52,7 +52,7 @@ public class DocumentTest extends IntegrationTest {
void createDocument() {
String name = "Test";
Part part = Parts.fromFile(testFile);
String description = null;
String description = "The Description";
var response = ok(fineract().documents.createDocument("clients", clientId, part, name, description));
assertThat(response.getResourceId()).isNotNull();
assertThat(response.getResourceIdentifier()).isNotEmpty();
@@ -65,12 +65,13 @@ public class DocumentTest extends IntegrationTest {
GetEntityTypeEntityIdDocumentsResponse doc = ok(fineract().documents.getDocument("clients", clientId, documentId));
assertThat(doc.getName()).isEqualTo("Test");
assertThat(doc.getFileName()).isEqualTo(testFile.getName());
assertThat(doc.getDescription()).isNull();
assertThat(doc.getDescription()).isEqualTo("The Description");
assertThat(doc.getId()).isEqualTo(documentId);
assertThat(doc.getParentEntityType()).isEqualTo("clients");
assertThat(doc.getParentEntityId()).isEqualTo(clientId);
// TODO huh?! It's more than uploaded file; seems like a bug - it's including create body, not just file size
assertThat(doc.getSize()).isEqualTo(testFile.length() + 411);
// TODO FINERACT-1251 It's more than uploaded file; seems like a bug - it's including create body, not just file
// size
assertThat(doc.getSize()).isEqualTo(testFile.length() + 618);
assertThat(doc.getType()).isEqualTo("image/jpeg");
// TODO doc.getStorageType() shouldn't be exposed by the API?!
}
@@ -82,7 +83,7 @@ public class DocumentTest extends IntegrationTest {
try (ResponseBody body = r.body()) {
assertThat(body.contentType()).isEqualTo(MediaType.get("image/jpeg"));
assertThat(body.bytes().length).isEqualTo(testFile.length());
assertThat(body.contentLength()).isEqualTo(-1); // TODO testFile.length()
assertThat(body.contentLength()).isEqualTo(testFile.length());
}
assertThat(Parts.fileName(r)).hasValue(testFile.getName());
}
@@ -97,6 +98,9 @@ public class DocumentTest extends IntegrationTest {
GetEntityTypeEntityIdDocumentsResponse doc = ok(fineract().documents.getDocument("clients", clientId, documentId));
assertThat(doc.getName()).isEqualTo(newName);
assertThat(doc.getDescription()).isEqualTo(newDescription);
// TODO FINERACT-1251 It's more than uploaded file; seems like a bug - it's including create body, not just file
// size
assertThat(doc.getSize()).isEqualTo(testFile.length() + 618);
}
@Test