From 5c1f9fcc82ec82edae3a3f8454b49e8d0576dfec Mon Sep 17 00:00:00 2001 From: David Edwards Date: Fri, 3 Apr 2020 12:33:50 +0100 Subject: [PATCH] ATS-675/ATS-705 Implemented ImageMagickAdapter Moved and renamed OptionsBuilder. Moved to alfresco-transform-imagemagick, renamed ImageMagickOptionsBuilder. Added dependencies to pom.xml --- .../alfresco-transform-core-aio/pom.xml | 5 + .../transformers/ImageMagickAdapter.java | 124 ++++++++++++++++++ .../transformer/ImageMagickController.java | 4 +- .../ImageMagickOptionsBuilder.java} | 72 +++++----- 4 files changed, 167 insertions(+), 38 deletions(-) create mode 100644 alfresco-transform-core-aio/alfresco-transform-core-aio/src/main/java/org/alfresco/transformer/transformers/ImageMagickAdapter.java rename alfresco-transform-imagemagick/{alfresco-transform-imagemagick-boot/src/main/java/org/alfresco/transformer/OptionsBuilder.java => alfresco-transform-imagemagick/src/main/java/org/alfresco/transformer/ImageMagickOptionsBuilder.java} (74%) diff --git a/alfresco-transform-core-aio/alfresco-transform-core-aio/pom.xml b/alfresco-transform-core-aio/alfresco-transform-core-aio/pom.xml index d390d355..33c1f0ba 100644 --- a/alfresco-transform-core-aio/alfresco-transform-core-aio/pom.xml +++ b/alfresco-transform-core-aio/alfresco-transform-core-aio/pom.xml @@ -40,6 +40,11 @@ alfresco-transform-libreoffice ${project.version} + + org.alfresco + alfresco-transform-imagemagick + ${project.version} + junit diff --git a/alfresco-transform-core-aio/alfresco-transform-core-aio/src/main/java/org/alfresco/transformer/transformers/ImageMagickAdapter.java b/alfresco-transform-core-aio/alfresco-transform-core-aio/src/main/java/org/alfresco/transformer/transformers/ImageMagickAdapter.java new file mode 100644 index 00000000..7935131d --- /dev/null +++ b/alfresco-transform-core-aio/alfresco-transform-core-aio/src/main/java/org/alfresco/transformer/transformers/ImageMagickAdapter.java @@ -0,0 +1,124 @@ +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2005 - 2019 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.transformer.transformers; + +import static org.alfresco.transformer.util.Util.stringToInteger; +import static org.alfresco.transformer.util.Util.stringToLong; + +import java.io.File; +import java.util.Map; + +import org.alfresco.transformer.ImageMagickOptionsBuilder; +import org.alfresco.transformer.executors.ImageMagickCommandExecutor; + +public class ImageMagickAdapter extends AbstractTransformer +{ + + private static String CONFIG_PREFIX = "imagemagick"; + private ImageMagickCommandExecutor commandExecutor; + + //TODO move key strings to a central class + private static final String START_PAGE = "startPage"; + private static final String END_PAGE = "endPage"; + private static final String ALPHA_REMOVE = "alphaRemove"; + private static final String AUTO_ORIENT = "autoOrient"; + private static final String CROP_GRAVITY = "cropGravity"; + private static final String CROP_WIDTH = "cropWidth"; + private static final String CROP_HEIGHT = "cropHeight"; + private static final String CROP_PERCENTAGE = "cropPercentage"; + private static final String CROP_X_OFFSET = "cropXOffset"; + private static final String CROP_Y_OFFSET = "cropYOffset"; + private static final String THUMBNAIL = "thumbnail"; + private static final String RESIZE_WIDTH = "resizeWidth"; + private static final String RESIZE_HEIGHT = "resizeHeight"; + private static final String RESIZE_PERCENTAGE = "resizePercentage"; + private static final String ALLOW_ENLARGEMENT = "allowEnlargement"; + private static final String MAINTAIN_ASPECT_RATIO = "maintainAspectRatio"; + private static final String COMMAND_OPTIONS = "commandOptions"; + private static final String TIMEOUT_REQUEST_PARAM = "timeOut"; + + + public ImageMagickAdapter() throws Exception + { + super(); + commandExecutor = new ImageMagickCommandExecutor(); + } + + @Override + String getTransformerConfigPrefix() + { + return CONFIG_PREFIX; + } + + @Override + public void transform(File sourceFile, File targetFile, String sourceMimetype, String targetMimetype, + Map transformOptions) throws Exception + { + + final String options = ImageMagickOptionsBuilder + .builder() + .withStartPage(transformOptions.get(START_PAGE)) + .withEndPage(transformOptions.get(END_PAGE)) + .withAlphaRemove(transformOptions.get(ALPHA_REMOVE)) + .withAutoOrient(transformOptions.get(AUTO_ORIENT)) + .withCropGravity(transformOptions.get(CROP_GRAVITY)) + .withCropWidth(transformOptions.get(CROP_WIDTH)) + .withCropHeight(transformOptions.get(CROP_HEIGHT)) + .withCropPercentage(transformOptions.get(CROP_PERCENTAGE)) + .withCropXOffset(transformOptions.get(CROP_X_OFFSET)) + .withCropYOffset(transformOptions.get(CROP_Y_OFFSET)) + .withThumbnail(transformOptions.get(THUMBNAIL)) + .withResizeWidth(transformOptions.get(RESIZE_WIDTH)) + .withResizeHeight(transformOptions.get(RESIZE_HEIGHT)) + .withResizePercentage(transformOptions.get(RESIZE_PERCENTAGE)) + .withAllowEnlargement(transformOptions.get(ALLOW_ENLARGEMENT)) + .withMaintainAspectRatio(transformOptions.get(MAINTAIN_ASPECT_RATIO)) + .withCommandOptions(transformOptions.get(COMMAND_OPTIONS)) + .build(); + + String pageRange = calculatePageRange( + stringToInteger(transformOptions.get(START_PAGE)), + stringToInteger(transformOptions.get(END_PAGE)) + ); + + Long timeout = stringToLong(transformOptions.get(TIMEOUT_REQUEST_PARAM)); + + commandExecutor.run(options, sourceFile, pageRange, targetFile, timeout); + } + + // COPIED From ImageMagickController + private static String calculatePageRange(Integer startPage, Integer endPage) + { + return startPage == null + ? endPage == null + ? "" + : "[" + endPage + ']' + : endPage == null || startPage.equals(endPage) + ? "[" + startPage + ']' + : "[" + startPage + '-' + endPage + ']'; + } +} \ No newline at end of file diff --git a/alfresco-transform-imagemagick/alfresco-transform-imagemagick-boot/src/main/java/org/alfresco/transformer/ImageMagickController.java b/alfresco-transform-imagemagick/alfresco-transform-imagemagick-boot/src/main/java/org/alfresco/transformer/ImageMagickController.java index f97c6b51..95cfb7b4 100644 --- a/alfresco-transform-imagemagick/alfresco-transform-imagemagick-boot/src/main/java/org/alfresco/transformer/ImageMagickController.java +++ b/alfresco-transform-imagemagick/alfresco-transform-imagemagick-boot/src/main/java/org/alfresco/transformer/ImageMagickController.java @@ -154,7 +154,7 @@ public class ImageMagickController extends AbstractTransformerController File targetFile = createTargetFile(request, targetFilename); // Both files are deleted by TransformInterceptor.afterCompletion - final String options = OptionsBuilder + final String options = ImageMagickOptionsBuilder .builder() .withStartPage(startPage) .withEndPage(endPage) @@ -196,7 +196,7 @@ public class ImageMagickController extends AbstractTransformerController logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" + " '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout); - final String options = OptionsBuilder + final String options = ImageMagickOptionsBuilder .builder() .withStartPage(transformOptions.get("startPage")) .withEndPage(transformOptions.get("endPage")) diff --git a/alfresco-transform-imagemagick/alfresco-transform-imagemagick-boot/src/main/java/org/alfresco/transformer/OptionsBuilder.java b/alfresco-transform-imagemagick/alfresco-transform-imagemagick/src/main/java/org/alfresco/transformer/ImageMagickOptionsBuilder.java similarity index 74% rename from alfresco-transform-imagemagick/alfresco-transform-imagemagick-boot/src/main/java/org/alfresco/transformer/OptionsBuilder.java rename to alfresco-transform-imagemagick/alfresco-transform-imagemagick/src/main/java/org/alfresco/transformer/ImageMagickOptionsBuilder.java index 9c0e7b99..af33ad73 100644 --- a/alfresco-transform-imagemagick/alfresco-transform-imagemagick-boot/src/main/java/org/alfresco/transformer/OptionsBuilder.java +++ b/alfresco-transform-imagemagick/alfresco-transform-imagemagick/src/main/java/org/alfresco/transformer/ImageMagickOptionsBuilder.java @@ -42,7 +42,7 @@ import com.google.common.collect.ImmutableList; * * @author Cezar Leahu */ -final class OptionsBuilder +public final class ImageMagickOptionsBuilder { private static final List GRAVITY_VALUES = ImmutableList.of("North", "NorthEast", "East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center"); @@ -65,180 +65,180 @@ final class OptionsBuilder private Boolean maintainAspectRatio; private String commandOptions; - private OptionsBuilder() {} + private ImageMagickOptionsBuilder() {} - public OptionsBuilder withStartPage(final String startPage) + public ImageMagickOptionsBuilder withStartPage(final String startPage) { return withStartPage(stringToInteger(startPage)); } - public OptionsBuilder withStartPage(final Integer startPage) + public ImageMagickOptionsBuilder withStartPage(final Integer startPage) { this.startPage = startPage; return this; } - public OptionsBuilder withEndPage(final String endPage) + public ImageMagickOptionsBuilder withEndPage(final String endPage) { return withEndPage(stringToInteger(endPage)); } - public OptionsBuilder withEndPage(final Integer endPage) + public ImageMagickOptionsBuilder withEndPage(final Integer endPage) { this.endPage = endPage; return this; } - public OptionsBuilder withAlphaRemove(final String alphaRemove) + public ImageMagickOptionsBuilder withAlphaRemove(final String alphaRemove) { return withAlphaRemove(stringToBoolean(alphaRemove)); } - public OptionsBuilder withAlphaRemove(final Boolean alphaRemove) + public ImageMagickOptionsBuilder withAlphaRemove(final Boolean alphaRemove) { this.alphaRemove = alphaRemove; return this; } - public OptionsBuilder withAutoOrient(final String autoOrient) + public ImageMagickOptionsBuilder withAutoOrient(final String autoOrient) { return withAutoOrient(stringToBoolean(autoOrient)); } - public OptionsBuilder withAutoOrient(final Boolean autoOrient) + public ImageMagickOptionsBuilder withAutoOrient(final Boolean autoOrient) { this.autoOrient = autoOrient; return this; } - public OptionsBuilder withCropGravity(final String cropGravity) + public ImageMagickOptionsBuilder withCropGravity(final String cropGravity) { this.cropGravity = cropGravity; return this; } - public OptionsBuilder withCropWidth(final String cropWidth) + public ImageMagickOptionsBuilder withCropWidth(final String cropWidth) { return withCropWidth(stringToInteger(cropWidth)); } - public OptionsBuilder withCropWidth(final Integer cropWidth) + public ImageMagickOptionsBuilder withCropWidth(final Integer cropWidth) { this.cropWidth = cropWidth; return this; } - public OptionsBuilder withCropHeight(final String cropHeight) + public ImageMagickOptionsBuilder withCropHeight(final String cropHeight) { return withCropHeight(stringToInteger(cropHeight)); } - public OptionsBuilder withCropHeight(final Integer cropHeight) + public ImageMagickOptionsBuilder withCropHeight(final Integer cropHeight) { this.cropHeight = cropHeight; return this; } - public OptionsBuilder withCropPercentage(final String cropPercentage) + public ImageMagickOptionsBuilder withCropPercentage(final String cropPercentage) { return withCropPercentage(stringToBoolean(cropPercentage)); } - public OptionsBuilder withCropPercentage(final Boolean cropPercentage) + public ImageMagickOptionsBuilder withCropPercentage(final Boolean cropPercentage) { this.cropPercentage = cropPercentage; return this; } - public OptionsBuilder withCropXOffset(final String cropXOffset) + public ImageMagickOptionsBuilder withCropXOffset(final String cropXOffset) { return withCropXOffset(stringToInteger(cropXOffset)); } - public OptionsBuilder withCropXOffset(final Integer cropXOffset) + public ImageMagickOptionsBuilder withCropXOffset(final Integer cropXOffset) { this.cropXOffset = cropXOffset; return this; } - public OptionsBuilder withCropYOffset(final String cropYOffset) + public ImageMagickOptionsBuilder withCropYOffset(final String cropYOffset) { return withCropYOffset(stringToInteger(cropYOffset)); } - public OptionsBuilder withCropYOffset(final Integer cropYOffset) + public ImageMagickOptionsBuilder withCropYOffset(final Integer cropYOffset) { this.cropYOffset = cropYOffset; return this; } - public OptionsBuilder withThumbnail(final String thumbnail) + public ImageMagickOptionsBuilder withThumbnail(final String thumbnail) { return withThumbnail(stringToBoolean(thumbnail)); } - public OptionsBuilder withThumbnail(final Boolean thumbnail) + public ImageMagickOptionsBuilder withThumbnail(final Boolean thumbnail) { this.thumbnail = thumbnail; return this; } - public OptionsBuilder withResizeWidth(final String resizeWidth) + public ImageMagickOptionsBuilder withResizeWidth(final String resizeWidth) { return withResizeWidth(stringToInteger(resizeWidth)); } - public OptionsBuilder withResizeWidth(final Integer resizeWidth) + public ImageMagickOptionsBuilder withResizeWidth(final Integer resizeWidth) { this.resizeWidth = resizeWidth; return this; } - public OptionsBuilder withResizeHeight(final String resizeHeight) + public ImageMagickOptionsBuilder withResizeHeight(final String resizeHeight) { return withResizeHeight(stringToInteger(resizeHeight)); } - public OptionsBuilder withResizeHeight(final Integer resizeHeight) + public ImageMagickOptionsBuilder withResizeHeight(final Integer resizeHeight) { this.resizeHeight = resizeHeight; return this; } - public OptionsBuilder withResizePercentage(final String resizePercentage) + public ImageMagickOptionsBuilder withResizePercentage(final String resizePercentage) { return withResizePercentage(stringToBoolean(resizePercentage)); } - public OptionsBuilder withResizePercentage(final Boolean resizePercentage) + public ImageMagickOptionsBuilder withResizePercentage(final Boolean resizePercentage) { this.resizePercentage = resizePercentage; return this; } - public OptionsBuilder withAllowEnlargement(final String allowEnlargement) + public ImageMagickOptionsBuilder withAllowEnlargement(final String allowEnlargement) { return withAllowEnlargement(stringToBoolean(allowEnlargement)); } - public OptionsBuilder withAllowEnlargement(final Boolean allowEnlargement) + public ImageMagickOptionsBuilder withAllowEnlargement(final Boolean allowEnlargement) { this.allowEnlargement = allowEnlargement; return this; } - public OptionsBuilder withMaintainAspectRatio(final String maintainAspectRatio) + public ImageMagickOptionsBuilder withMaintainAspectRatio(final String maintainAspectRatio) { return withMaintainAspectRatio(stringToBoolean(maintainAspectRatio)); } - public OptionsBuilder withMaintainAspectRatio(final Boolean maintainAspectRatio) + public ImageMagickOptionsBuilder withMaintainAspectRatio(final Boolean maintainAspectRatio) { this.maintainAspectRatio = maintainAspectRatio; return this; } - public OptionsBuilder withCommandOptions(final String commandOptions) + public ImageMagickOptionsBuilder withCommandOptions(final String commandOptions) { this.commandOptions = commandOptions; return this; @@ -354,8 +354,8 @@ final class OptionsBuilder args.toString(); } - public static OptionsBuilder builder() + public static ImageMagickOptionsBuilder builder() { - return new OptionsBuilder(); + return new ImageMagickOptionsBuilder(); } }