ATS-675/ATS-705 Implemented ImageMagickAdapter

Moved and renamed OptionsBuilder. Moved to alfresco-transform-imagemagick, renamed ImageMagickOptionsBuilder.
Added dependencies to pom.xml
This commit is contained in:
David Edwards
2020-04-03 12:33:50 +01:00
parent 31fa9cb7ee
commit 5c1f9fcc82
4 changed files with 167 additions and 38 deletions

View File

@@ -40,6 +40,11 @@
<artifactId>alfresco-transform-libreoffice</artifactId> <artifactId>alfresco-transform-libreoffice</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.alfresco</groupId>
<artifactId>alfresco-transform-imagemagick</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@@ -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 <http://www.gnu.org/licenses/>.
* #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<String, String> 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 + ']';
}
}

View File

@@ -154,7 +154,7 @@ public class ImageMagickController extends AbstractTransformerController
File targetFile = createTargetFile(request, targetFilename); File targetFile = createTargetFile(request, targetFilename);
// Both files are deleted by TransformInterceptor.afterCompletion // Both files are deleted by TransformInterceptor.afterCompletion
final String options = OptionsBuilder final String options = ImageMagickOptionsBuilder
.builder() .builder()
.withStartPage(startPage) .withStartPage(startPage)
.withEndPage(endPage) .withEndPage(endPage)
@@ -196,7 +196,7 @@ public class ImageMagickController extends AbstractTransformerController
logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" + logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" +
" '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout); " '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout);
final String options = OptionsBuilder final String options = ImageMagickOptionsBuilder
.builder() .builder()
.withStartPage(transformOptions.get("startPage")) .withStartPage(transformOptions.get("startPage"))
.withEndPage(transformOptions.get("endPage")) .withEndPage(transformOptions.get("endPage"))

View File

@@ -42,7 +42,7 @@ import com.google.common.collect.ImmutableList;
* *
* @author Cezar Leahu * @author Cezar Leahu
*/ */
final class OptionsBuilder public final class ImageMagickOptionsBuilder
{ {
private static final List<String> GRAVITY_VALUES = ImmutableList.of("North", "NorthEast", private static final List<String> GRAVITY_VALUES = ImmutableList.of("North", "NorthEast",
"East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center"); "East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center");
@@ -65,180 +65,180 @@ final class OptionsBuilder
private Boolean maintainAspectRatio; private Boolean maintainAspectRatio;
private String commandOptions; private String commandOptions;
private OptionsBuilder() {} private ImageMagickOptionsBuilder() {}
public OptionsBuilder withStartPage(final String startPage) public ImageMagickOptionsBuilder withStartPage(final String startPage)
{ {
return withStartPage(stringToInteger(startPage)); return withStartPage(stringToInteger(startPage));
} }
public OptionsBuilder withStartPage(final Integer startPage) public ImageMagickOptionsBuilder withStartPage(final Integer startPage)
{ {
this.startPage = startPage; this.startPage = startPage;
return this; return this;
} }
public OptionsBuilder withEndPage(final String endPage) public ImageMagickOptionsBuilder withEndPage(final String endPage)
{ {
return withEndPage(stringToInteger(endPage)); return withEndPage(stringToInteger(endPage));
} }
public OptionsBuilder withEndPage(final Integer endPage) public ImageMagickOptionsBuilder withEndPage(final Integer endPage)
{ {
this.endPage = endPage; this.endPage = endPage;
return this; return this;
} }
public OptionsBuilder withAlphaRemove(final String alphaRemove) public ImageMagickOptionsBuilder withAlphaRemove(final String alphaRemove)
{ {
return withAlphaRemove(stringToBoolean(alphaRemove)); return withAlphaRemove(stringToBoolean(alphaRemove));
} }
public OptionsBuilder withAlphaRemove(final Boolean alphaRemove) public ImageMagickOptionsBuilder withAlphaRemove(final Boolean alphaRemove)
{ {
this.alphaRemove = alphaRemove; this.alphaRemove = alphaRemove;
return this; return this;
} }
public OptionsBuilder withAutoOrient(final String autoOrient) public ImageMagickOptionsBuilder withAutoOrient(final String autoOrient)
{ {
return withAutoOrient(stringToBoolean(autoOrient)); return withAutoOrient(stringToBoolean(autoOrient));
} }
public OptionsBuilder withAutoOrient(final Boolean autoOrient) public ImageMagickOptionsBuilder withAutoOrient(final Boolean autoOrient)
{ {
this.autoOrient = autoOrient; this.autoOrient = autoOrient;
return this; return this;
} }
public OptionsBuilder withCropGravity(final String cropGravity) public ImageMagickOptionsBuilder withCropGravity(final String cropGravity)
{ {
this.cropGravity = cropGravity; this.cropGravity = cropGravity;
return this; return this;
} }
public OptionsBuilder withCropWidth(final String cropWidth) public ImageMagickOptionsBuilder withCropWidth(final String cropWidth)
{ {
return withCropWidth(stringToInteger(cropWidth)); return withCropWidth(stringToInteger(cropWidth));
} }
public OptionsBuilder withCropWidth(final Integer cropWidth) public ImageMagickOptionsBuilder withCropWidth(final Integer cropWidth)
{ {
this.cropWidth = cropWidth; this.cropWidth = cropWidth;
return this; return this;
} }
public OptionsBuilder withCropHeight(final String cropHeight) public ImageMagickOptionsBuilder withCropHeight(final String cropHeight)
{ {
return withCropHeight(stringToInteger(cropHeight)); return withCropHeight(stringToInteger(cropHeight));
} }
public OptionsBuilder withCropHeight(final Integer cropHeight) public ImageMagickOptionsBuilder withCropHeight(final Integer cropHeight)
{ {
this.cropHeight = cropHeight; this.cropHeight = cropHeight;
return this; return this;
} }
public OptionsBuilder withCropPercentage(final String cropPercentage) public ImageMagickOptionsBuilder withCropPercentage(final String cropPercentage)
{ {
return withCropPercentage(stringToBoolean(cropPercentage)); return withCropPercentage(stringToBoolean(cropPercentage));
} }
public OptionsBuilder withCropPercentage(final Boolean cropPercentage) public ImageMagickOptionsBuilder withCropPercentage(final Boolean cropPercentage)
{ {
this.cropPercentage = cropPercentage; this.cropPercentage = cropPercentage;
return this; return this;
} }
public OptionsBuilder withCropXOffset(final String cropXOffset) public ImageMagickOptionsBuilder withCropXOffset(final String cropXOffset)
{ {
return withCropXOffset(stringToInteger(cropXOffset)); return withCropXOffset(stringToInteger(cropXOffset));
} }
public OptionsBuilder withCropXOffset(final Integer cropXOffset) public ImageMagickOptionsBuilder withCropXOffset(final Integer cropXOffset)
{ {
this.cropXOffset = cropXOffset; this.cropXOffset = cropXOffset;
return this; return this;
} }
public OptionsBuilder withCropYOffset(final String cropYOffset) public ImageMagickOptionsBuilder withCropYOffset(final String cropYOffset)
{ {
return withCropYOffset(stringToInteger(cropYOffset)); return withCropYOffset(stringToInteger(cropYOffset));
} }
public OptionsBuilder withCropYOffset(final Integer cropYOffset) public ImageMagickOptionsBuilder withCropYOffset(final Integer cropYOffset)
{ {
this.cropYOffset = cropYOffset; this.cropYOffset = cropYOffset;
return this; return this;
} }
public OptionsBuilder withThumbnail(final String thumbnail) public ImageMagickOptionsBuilder withThumbnail(final String thumbnail)
{ {
return withThumbnail(stringToBoolean(thumbnail)); return withThumbnail(stringToBoolean(thumbnail));
} }
public OptionsBuilder withThumbnail(final Boolean thumbnail) public ImageMagickOptionsBuilder withThumbnail(final Boolean thumbnail)
{ {
this.thumbnail = thumbnail; this.thumbnail = thumbnail;
return this; return this;
} }
public OptionsBuilder withResizeWidth(final String resizeWidth) public ImageMagickOptionsBuilder withResizeWidth(final String resizeWidth)
{ {
return withResizeWidth(stringToInteger(resizeWidth)); return withResizeWidth(stringToInteger(resizeWidth));
} }
public OptionsBuilder withResizeWidth(final Integer resizeWidth) public ImageMagickOptionsBuilder withResizeWidth(final Integer resizeWidth)
{ {
this.resizeWidth = resizeWidth; this.resizeWidth = resizeWidth;
return this; return this;
} }
public OptionsBuilder withResizeHeight(final String resizeHeight) public ImageMagickOptionsBuilder withResizeHeight(final String resizeHeight)
{ {
return withResizeHeight(stringToInteger(resizeHeight)); return withResizeHeight(stringToInteger(resizeHeight));
} }
public OptionsBuilder withResizeHeight(final Integer resizeHeight) public ImageMagickOptionsBuilder withResizeHeight(final Integer resizeHeight)
{ {
this.resizeHeight = resizeHeight; this.resizeHeight = resizeHeight;
return this; return this;
} }
public OptionsBuilder withResizePercentage(final String resizePercentage) public ImageMagickOptionsBuilder withResizePercentage(final String resizePercentage)
{ {
return withResizePercentage(stringToBoolean(resizePercentage)); return withResizePercentage(stringToBoolean(resizePercentage));
} }
public OptionsBuilder withResizePercentage(final Boolean resizePercentage) public ImageMagickOptionsBuilder withResizePercentage(final Boolean resizePercentage)
{ {
this.resizePercentage = resizePercentage; this.resizePercentage = resizePercentage;
return this; return this;
} }
public OptionsBuilder withAllowEnlargement(final String allowEnlargement) public ImageMagickOptionsBuilder withAllowEnlargement(final String allowEnlargement)
{ {
return withAllowEnlargement(stringToBoolean(allowEnlargement)); return withAllowEnlargement(stringToBoolean(allowEnlargement));
} }
public OptionsBuilder withAllowEnlargement(final Boolean allowEnlargement) public ImageMagickOptionsBuilder withAllowEnlargement(final Boolean allowEnlargement)
{ {
this.allowEnlargement = allowEnlargement; this.allowEnlargement = allowEnlargement;
return this; return this;
} }
public OptionsBuilder withMaintainAspectRatio(final String maintainAspectRatio) public ImageMagickOptionsBuilder withMaintainAspectRatio(final String maintainAspectRatio)
{ {
return withMaintainAspectRatio(stringToBoolean(maintainAspectRatio)); return withMaintainAspectRatio(stringToBoolean(maintainAspectRatio));
} }
public OptionsBuilder withMaintainAspectRatio(final Boolean maintainAspectRatio) public ImageMagickOptionsBuilder withMaintainAspectRatio(final Boolean maintainAspectRatio)
{ {
this.maintainAspectRatio = maintainAspectRatio; this.maintainAspectRatio = maintainAspectRatio;
return this; return this;
} }
public OptionsBuilder withCommandOptions(final String commandOptions) public ImageMagickOptionsBuilder withCommandOptions(final String commandOptions)
{ {
this.commandOptions = commandOptions; this.commandOptions = commandOptions;
return this; return this;
@@ -354,8 +354,8 @@ final class OptionsBuilder
args.toString(); args.toString();
} }
public static OptionsBuilder builder() public static ImageMagickOptionsBuilder builder()
{ {
return new OptionsBuilder(); return new ImageMagickOptionsBuilder();
} }
} }