branch develop updated (ba7fb6bf -> a384bec0)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git from ba7fb6bf fix poll#picture relation new a384bec0 Resizing image when it is uploaded The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit a384bec0516ae1d33cf4e5a2731baeb1f4dad366 Author: Cécilia Bossard <bossard@codelutin.com> Date: Fri Jul 10 15:34:30 2020 +0200 Resizing image when it is uploaded Summary of changes: .../org/chorem/pollen/rest/api/v1/ApiUtils.java | 142 +++++++++++++++++---- .../pollen/rest/api/v1/PollenResourceApi.java | 12 +- 2 files changed, 127 insertions(+), 27 deletions(-) -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit a384bec0516ae1d33cf4e5a2731baeb1f4dad366 Author: Cécilia Bossard <bossard@codelutin.com> Date: Fri Jul 10 15:34:30 2020 +0200 Resizing image when it is uploaded --- .../org/chorem/pollen/rest/api/v1/ApiUtils.java | 142 +++++++++++++++++---- .../pollen/rest/api/v1/PollenResourceApi.java | 12 +- 2 files changed, 127 insertions(+), 27 deletions(-) diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ApiUtils.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ApiUtils.java index 5834b267..167e6e13 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ApiUtils.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/ApiUtils.java @@ -23,6 +23,7 @@ package org.chorem.pollen.rest.api.v1; import com.google.common.base.Charsets; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.chorem.pollen.persistence.entity.ResourceType; import org.chorem.pollen.rest.api.beans.Resource64Bean; @@ -32,7 +33,10 @@ import org.chorem.pollen.services.bean.export.ExportBean; import org.jboss.resteasy.plugins.providers.multipart.InputPart; import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; +import javax.imageio.ImageIO; import javax.ws.rs.core.Response; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; @@ -62,41 +66,36 @@ public class ApiUtils { .build(); } - public static ResourceFileBean multipartToResourceBean(MultipartFormDataInput multipartFormDataInput, String inputName) { + + public static ResourceFileBean imageMultipartToResourceBean(MultipartFormDataInput multipartFormDataInput, String inputName, Integer maxWidth, Integer maxHeight) { try { - InputStream in = multipartFormDataInput.getFormDataPart(inputName, InputStream.class, null); - java.nio.file.Path tempPath = Files.createTempDirectory("pollen"); InputPart inputPart = multipartFormDataInput .getFormDataMap() .get(inputName) .get(0); - String contentDisposition = inputPart - .getHeaders() - .get("Content-Disposition") - .get(0); - Pattern FilenamePattern = Pattern.compile("filename=\"(.*)\""); - Matcher matcher = FilenamePattern.matcher(contentDisposition); - String fileName = inputName; - if (matcher.find()) { - fileName = matcher.group(1); + File uploadFile = uploadFile(multipartFormDataInput, inputPart, inputName); + + if(maxWidth != null || maxHeight != null) { + resizeAndCropImage(uploadFile, maxWidth, maxHeight); } - File uploadFile = new File(tempPath.toFile(), fileName); - Files.copy(in, uploadFile.toPath()); - ResourceFileBean resourceBean = new ResourceFileBean(); - resourceBean.setFile(uploadFile); - resourceBean.setName(fileName); + ResourceFileBean resourceBean = createResourceFileBean(multipartFormDataInput, uploadFile, inputPart); - String contentType = inputPart.getMediaType().toString(); + return resourceBean; + } catch (IOException e) { + throw new PollenTechnicalException(e); + } + } - resourceBean.setContentType(contentType); - resourceBean.setSize(uploadFile.length()); - List<InputPart> resourceTypeInputs = multipartFormDataInput + public static ResourceFileBean multipartToResourceBean(MultipartFormDataInput multipartFormDataInput, String inputName) { + try { + InputPart inputPart = multipartFormDataInput .getFormDataMap() - .get("resourceType"); - if (CollectionUtils.isNotEmpty(resourceTypeInputs)) { - resourceBean.setResourceType(ResourceType.valueOf(resourceTypeInputs.get(0).getBodyAsString())); - } + .get(inputName) + .get(0); + File uploadFile = uploadFile(multipartFormDataInput, inputPart, inputName); + + ResourceFileBean resourceBean = createResourceFileBean(multipartFormDataInput, uploadFile, inputPart); return resourceBean; } catch (IOException e) { @@ -104,6 +103,99 @@ public class ApiUtils { } } + + private static File uploadFile(MultipartFormDataInput multipartFormDataInput, InputPart inputPart, String inputName) throws IOException { + InputStream in = multipartFormDataInput.getFormDataPart(inputName, InputStream.class, null); + java.nio.file.Path tempPath = Files.createTempDirectory("pollen"); + String contentDisposition = inputPart + .getHeaders() + .get("Content-Disposition") + .get(0); + Pattern FilenamePattern = Pattern.compile("filename=\"(.*)\""); + Matcher matcher = FilenamePattern.matcher(contentDisposition); + String fileName = inputName; + if (matcher.find()) { + fileName = matcher.group(1); + } + File uploadFile = new File(tempPath.toFile(), fileName); + Files.copy(in, uploadFile.toPath()); + + return uploadFile; + } + + private static ResourceFileBean createResourceFileBean(MultipartFormDataInput multipartFormDataInput, File uploadFile, InputPart inputPart) throws IOException { + ResourceFileBean resourceBean = new ResourceFileBean(); + resourceBean.setFile(uploadFile); + resourceBean.setName(uploadFile.getName()); + + String contentType = inputPart.getMediaType().toString(); + + resourceBean.setContentType(contentType); + resourceBean.setSize(uploadFile.length()); + List<InputPart> resourceTypeInputs = multipartFormDataInput + .getFormDataMap() + .get("resourceType"); + if (CollectionUtils.isNotEmpty(resourceTypeInputs)) { + resourceBean.setResourceType(ResourceType.valueOf(resourceTypeInputs.get(0).getBodyAsString())); + } + return resourceBean; + } + + + + private static void resizeAndCropImage(File uploadFile, Integer maxWidth, Integer maxHeight) { + + BufferedImage bufferedImage =null; + try + { + bufferedImage = ImageIO.read(uploadFile); + if(bufferedImage != null) { + + BufferedImage resizeImage = resizeImage(bufferedImage, bufferedImage.getType(), maxWidth, maxHeight); + BufferedImage image = cropImage(resizeImage, maxWidth, maxHeight); + + ImageIO.write(image, FilenameUtils.getExtension(uploadFile.getName()), uploadFile); + } + } + catch (IOException e) { + throw new PollenTechnicalException(e); + } + } + + private static BufferedImage cropImage(BufferedImage originalImage, Integer maxWidth, Integer maxHeight) { + + if (originalImage.getHeight() > maxHeight) { + + int width = originalImage.getWidth() > maxWidth ? maxWidth : originalImage.getWidth(); + + int xIndex = (originalImage.getHeight() - maxHeight) /2; + + return originalImage.getSubimage(0, xIndex, width, maxHeight); + } else { + return originalImage; + } + } + + private static BufferedImage resizeImage(BufferedImage bufferedImage, int type, Integer maxWidth, Integer maxHeight) { + + int originalWidth = bufferedImage.getWidth(); + int originalHeight = bufferedImage.getHeight(); + + if (originalWidth > maxWidth) { + + int targetHeight = originalHeight * maxWidth / originalWidth; + + BufferedImage resizedImage = new BufferedImage(maxWidth, targetHeight, type); + Graphics2D g = resizedImage.createGraphics(); + g.drawImage(bufferedImage, 0, 0, maxWidth, targetHeight, null); + g.dispose(); + + return resizedImage; + } else { + return bufferedImage; + } + } + public static ResourceFileBean resource64ToResourceBean(Resource64Bean resource64Bean) { try { diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java index 4ce7e96c..918beb69 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java @@ -101,9 +101,17 @@ public class PollenResourceApi { @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public PollenEntityRef<PollenResource> createResource(@Context PollenResourceService pollenResourceService, - MultipartFormDataInput input) throws InvalidFormException { + MultipartFormDataInput input, + @QueryParam("width") Integer maxWidth, + @QueryParam("height") Integer maxHeight) throws InvalidFormException { - ResourceFileBean resourceBean = ApiUtils.multipartToResourceBean(input, "resource"); + ResourceFileBean resourceBean; + + if( maxWidth != null || maxHeight != null ) { + resourceBean = ApiUtils.imageMultipartToResourceBean(input, "resource", maxWidth, maxHeight); + } else { + resourceBean = ApiUtils.multipartToResourceBean(input, "resource"); + } PollenEntityRef<PollenResource> createRef = pollenResourceService.createResource(resourceBean); -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
participants (1)
-
chorem.org scm