REPO-4334 Move metadata extraction into T-Engines (#247)

* Metadata extract code added to T-Engines
* Required a refactor of duplicate code to avoid 3x more duplication:
        - try catches used to return return exit codes
        - calls to java libraries or commands to external processes
        - building of transform options in controllers, adaptors
* integration tests based on current extracts performed in the repo
* included extract code for libreoffice, and embed code even though not used out of the box any more. There may well be custom extracts using them that move to T-Engines
* removal of unused imports
* minor autoOrient / allowEnlargement bug fixes that were not included in Paddington on the T-Engine side.
This commit is contained in:
Alan Davis
2020-06-11 20:20:22 +01:00
committed by GitHub
parent ca394440bb
commit 06109dee75
158 changed files with 10288 additions and 1454 deletions

View File

@@ -26,10 +26,7 @@
*/
package org.alfresco.transformer;
import static org.alfresco.transformer.logging.StandardMessages.LICENCE;
import java.util.Arrays;
import io.micrometer.core.instrument.MeterRegistry;
import org.alfresco.transformer.executors.ImageMagickCommandExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -43,7 +40,9 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import io.micrometer.core.instrument.MeterRegistry;
import java.util.Arrays;
import static org.alfresco.transformer.logging.StandardMessages.LICENCE;
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Transform Core
* %%
* Copyright (C) 2005 - 2019 Alfresco Software Limited
* Copyright (C) 2005 - 2020 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
@@ -26,32 +26,17 @@
*/
package org.alfresco.transformer;
import static org.alfresco.transformer.fs.FileManager.createAttachment;
import static org.alfresco.transformer.fs.FileManager.createSourceFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFile;
import static org.alfresco.transformer.fs.FileManager.createTargetFileName;
import static org.alfresco.transformer.util.Util.stringToInteger;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
import java.io.File;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.transformer.executors.ImageMagickCommandExecutor;
import org.alfresco.transformer.logging.LogEntry;
import org.alfresco.transformer.probes.ProbeTestTransform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.io.File;
import java.util.Collections;
import java.util.Map;
/**
* Controller for the Docker based ImageMagick transformer.
@@ -76,7 +61,7 @@ import org.springframework.web.multipart.MultipartFile;
* 507 Insufficient Storage: Failed to store the source file
*/
@Controller
public class ImageMagickController extends AbstractTransformerController
public class ImageMagickController extends AbstractTransformerController
{
private static final Logger logger = LoggerFactory.getLogger(ImageMagickController.class);
@@ -125,135 +110,22 @@ public class ImageMagickController extends AbstractTransformerController
@Override
protected void executeTransformCommand(File sourceFile, File targetFile)
{
commandExecutor.run("", sourceFile, "", targetFile, null);
transform(null, null, null, Collections.emptyMap(), sourceFile, targetFile);
}
};
}
@PostMapping(value = "/transform", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Resource> transform(HttpServletRequest request,
@RequestParam("file") MultipartFile sourceMultipartFile,
@RequestParam("targetExtension") String targetExtension,
@RequestParam(value = "timeout", required = false) Long timeout,
@RequestParam(value = "testDelay", required = false) Long testDelay,
@RequestParam(value = "startPage", required = false) Integer startPage,
@RequestParam(value = "endPage", required = false) Integer endPage,
@RequestParam(value = "alphaRemove", required = false) Boolean alphaRemove,
@RequestParam(value = "autoOrient", required = false) Boolean autoOrient,
@RequestParam(value = "cropGravity", required = false) String cropGravity,
@RequestParam(value = "cropWidth", required = false) Integer cropWidth,
@RequestParam(value = "cropHeight", required = false) Integer cropHeight,
@RequestParam(value = "cropPercentage", required = false) Boolean cropPercentage,
@RequestParam(value = "cropXOffset", required = false) Integer cropXOffset,
@RequestParam(value = "cropYOffset", required = false) Integer cropYOffset,
@RequestParam(value = "thumbnail", required = false) Boolean thumbnail,
@RequestParam(value = "resizeWidth", required = false) Integer resizeWidth,
@RequestParam(value = "resizeHeight", required = false) Integer resizeHeight,
@RequestParam(value = "resizePercentage", required = false) Boolean resizePercentage,
@RequestParam(value = "allowEnlargement", required = false) Boolean allowEnlargement,
@RequestParam(value = "maintainAspectRatio", required = false) Boolean maintainAspectRatio,
// The commandOptions parameter is supported in ACS 6.0.1 because there may be
// custom renditions that use it. However the Transform service should
// not support it as it provides the option to specify arbitrary command
// options or even the option to run something else on the command line.
// All Transform service options should be checked as is done for the other
// request parameters. Setting this option in the rendition's
// ImageTransformationOptions object is being deprecated for the point where
// The Transform service is being used for all transforms. In the case of
// ACS 6.0, this is relatively safe as it requires an AMP to be installed
// which supplies the commandOptions.
@RequestParam(value = "commandOptions", required = false) String commandOptions)
@Override
protected String getTransformerName(final File sourceFile, final String sourceMimetype,
final String targetMimetype, final Map<String, String> transformOptions)
{
String targetFilename = createTargetFileName(sourceMultipartFile.getOriginalFilename(),
targetExtension);
getProbeTestTransform().incrementTransformerCount();
File sourceFile = createSourceFile(request, sourceMultipartFile);
File targetFile = createTargetFile(request, targetFilename);
// Both files are deleted by TransformInterceptor.afterCompletion
final String options = ImageMagickOptionsBuilder
.builder()
.withStartPage(startPage)
.withEndPage(endPage)
.withAlphaRemove(alphaRemove)
.withAutoOrient(autoOrient)
.withCropGravity(cropGravity)
.withCropWidth(cropWidth)
.withCropHeight(cropHeight)
.withCropPercentage(cropPercentage)
.withCropXOffset(cropXOffset)
.withCropYOffset(cropYOffset)
.withThumbnail(thumbnail)
.withResizeWidth(resizeWidth)
.withResizeHeight(resizeHeight)
.withResizePercentage(resizePercentage)
.withAllowEnlargement(allowEnlargement)
.withMaintainAspectRatio(maintainAspectRatio)
.withCommandOptions(commandOptions)
.build();
String pageRange = calculatePageRange(startPage, endPage);
commandExecutor.run(options, sourceFile, pageRange, targetFile,
timeout);
final ResponseEntity<Resource> body = createAttachment(targetFilename, targetFile);
LogEntry.setTargetSize(targetFile.length());
long time = LogEntry.setStatusCodeAndMessage(OK.value(), "Success");
time += LogEntry.addDelay(testDelay);
getProbeTestTransform().recordTransformTime(time);
return body;
return null; // does not matter what value is returned, as it is not used because there is only one.
}
@Override
public void processTransform(final File sourceFile, final File targetFile,
final String sourceMimetype, final String targetMimetype,
final Map<String, String> transformOptions, final Long timeout)
protected void transform(String transformName, String sourceMimetype, String targetMimetype,
Map<String, String> transformOptions, File sourceFile, File targetFile)
{
logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" +
" '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout);
final String options = ImageMagickOptionsBuilder
.builder()
.withStartPage(transformOptions.get("startPage"))
.withEndPage(transformOptions.get("endPage"))
.withAlphaRemove(transformOptions.get("alphaRemove"))
.withAutoOrient(transformOptions.get("autoOrient"))
.withCropGravity(transformOptions.get("cropGravity"))
.withCropWidth(transformOptions.get("cropWidth"))
.withCropHeight(transformOptions.get("cropHeight"))
.withCropPercentage(transformOptions.get("cropPercentage"))
.withCropXOffset(transformOptions.get("cropXOffset"))
.withCropYOffset(transformOptions.get("cropYOffset"))
.withThumbnail(transformOptions.get("thumbnail"))
.withResizeWidth(transformOptions.get("resizeWidth"))
.withResizeHeight(transformOptions.get("resizeHeight"))
.withResizePercentage(transformOptions.get("resizePercentage"))
.withAllowEnlargement(transformOptions.get("allowEnlargement"))
.withMaintainAspectRatio(transformOptions.get("maintainAspectRatio"))
.build();
final String pageRange = calculatePageRange(
stringToInteger(transformOptions.get("startPage")),
stringToInteger(transformOptions.get("endPage")));
commandExecutor.run(options, sourceFile, pageRange, targetFile,
timeout);
}
private static String calculatePageRange(Integer startPage, Integer endPage)
{
return startPage == null
? endPage == null
? ""
: "[" + endPage + ']'
: endPage == null || startPage.equals(endPage)
? "[" + startPage + ']'
: "[" + startPage + '-' + endPage + ']';
commandExecutor.transform(sourceMimetype, targetMimetype, transformOptions, sourceFile, targetFile);
}
}

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Transform Core
* %%
* Copyright (C) 2005 - 2019 Alfresco Software Limited
* Copyright (C) 2005 - 2020 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
@@ -26,36 +26,6 @@
*/
package org.alfresco.transformer;
import static org.alfresco.transformer.executors.RuntimeExec.ExecutionResult;
import static org.alfresco.transformer.util.MimetypeMap.PREFIX_IMAGE;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.IMAGE_PNG_VALUE;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.util.StringUtils.getFilenameExtension;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest;
import org.alfresco.transformer.executors.ImageMagickCommandExecutor;
@@ -80,6 +50,35 @@ import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static org.alfresco.transformer.executors.RuntimeExec.ExecutionResult;
import static org.alfresco.transformer.util.MimetypeMap.PREFIX_IMAGE;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.IMAGE_PNG_VALUE;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.util.StringUtils.getFilenameExtension;
/**
* Test the ImageMagickController without a server.
@@ -228,7 +227,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
{
for (String value : new String[]{"North", "NorthEast", "East", "SouthEast", "South", "SouthWest", "West", "NorthWest", "Center"})
{
expectedOptions = "-gravity " + value + " +repage";
expectedOptions = "-auto-orient " + "-gravity " + value + " +repage";
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")
@@ -338,7 +337,7 @@ public class ImageMagickControllerTest extends AbstractTransformerControllerTest
public void deprecatedCommandOptionsTest() throws Exception
{
// Example of why the commandOptions parameter is a bad idea.
expectedOptions = "( horrible command / ); -resize 321x654>";
expectedOptions = "( horrible command / ); -auto-orient -resize 321x654";
mockMvc
.perform(MockMvcRequestBuilders
.multipart("/transform")

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Transform Core
* %%
* Copyright (C) 2005 - 2019 Alfresco Software Limited
* Copyright (C) 2005 - 2020 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
@@ -26,16 +26,15 @@
*/
package org.alfresco.transformer;
import static org.alfresco.transformer.util.Util.stringToBoolean;
import static org.alfresco.transformer.util.Util.stringToInteger;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import com.google.common.collect.ImmutableList;
import org.alfresco.transform.exceptions.TransformException;
import java.util.List;
import java.util.StringJoiner;
import org.alfresco.transform.exceptions.TransformException;
import com.google.common.collect.ImmutableList;
import static org.alfresco.transformer.util.Util.stringToBoolean;
import static org.alfresco.transformer.util.Util.stringToInteger;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
/**
* ImageMagick options builder.
@@ -107,7 +106,7 @@ public final class ImageMagickOptionsBuilder
public ImageMagickOptionsBuilder withAutoOrient(final Boolean autoOrient)
{
this.autoOrient = autoOrient;
this.autoOrient = autoOrient == null ? true : autoOrient;
return this;
}
@@ -223,7 +222,7 @@ public final class ImageMagickOptionsBuilder
public ImageMagickOptionsBuilder withAllowEnlargement(final Boolean allowEnlargement)
{
this.allowEnlargement = allowEnlargement;
this.allowEnlargement = allowEnlargement == null ? true : allowEnlargement;
return this;
}

View File

@@ -26,15 +26,42 @@
*/
package org.alfresco.transformer.executors;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transformer.ImageMagickOptionsBuilder;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transformer.util.RequestParamMap.ALLOW_ENLARGEMENT;
import static org.alfresco.transformer.util.RequestParamMap.ALPHA_REMOVE;
import static org.alfresco.transformer.util.RequestParamMap.AUTO_ORIENT;
import static org.alfresco.transformer.util.RequestParamMap.COMMAND_OPTIONS;
import static org.alfresco.transformer.util.RequestParamMap.CROP_GRAVITY;
import static org.alfresco.transformer.util.RequestParamMap.CROP_HEIGHT;
import static org.alfresco.transformer.util.RequestParamMap.CROP_PERCENTAGE;
import static org.alfresco.transformer.util.RequestParamMap.CROP_WIDTH;
import static org.alfresco.transformer.util.RequestParamMap.CROP_X_OFFSET;
import static org.alfresco.transformer.util.RequestParamMap.CROP_Y_OFFSET;
import static org.alfresco.transformer.util.RequestParamMap.END_PAGE;
import static org.alfresco.transformer.util.RequestParamMap.MAINTAIN_ASPECT_RATIO;
import static org.alfresco.transformer.util.RequestParamMap.RESIZE_HEIGHT;
import static org.alfresco.transformer.util.RequestParamMap.RESIZE_PERCENTAGE;
import static org.alfresco.transformer.util.RequestParamMap.RESIZE_WIDTH;
import static org.alfresco.transformer.util.RequestParamMap.START_PAGE;
import static org.alfresco.transformer.util.RequestParamMap.THUMBNAIL;
import static org.alfresco.transformer.util.RequestParamMap.TIMEOUT;
import static org.alfresco.transformer.util.Util.stringToInteger;
import static org.alfresco.transformer.util.Util.stringToLong;
/**
* CommandExecutor implementation for running ImageMagick transformations. It runs the
* transformation logic as a separate Shell process.
*/
public class ImageMagickCommandExecutor extends AbstractCommandExecutor
{
private static final String ID = "imagemagick";
private final String ROOT;
private final String DYN;
private final String EXE;
@@ -65,6 +92,12 @@ public class ImageMagickCommandExecutor extends AbstractCommandExecutor
super.checkCommand = createCheckCommand();
}
@Override
public String getTransformerId()
{
return ID;
}
public static final String LICENCE = "This transformer uses ImageMagick from ImageMagick Studio LLC. See the license at http://www.imagemagick.org/script/license.php or in /ImageMagick-license.txt";
@Override
@@ -111,4 +144,51 @@ public class ImageMagickCommandExecutor extends AbstractCommandExecutor
runtimeExec.setCommandsAndArguments(commandsAndArguments);
return runtimeExec;
}
@Override
public void transform(String transformName, String sourceMimetype, String targetMimetype,
Map<String, String> transformOptions,
File sourceFile, File targetFile) throws TransformException
{
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));
run(options, sourceFile, pageRange, targetFile, timeout);
}
private static String calculatePageRange(Integer startPage, Integer endPage)
{
return startPage == null
? endPage == null
? ""
: "[" + endPage + ']'
: endPage == null || startPage.equals(endPage)
? "[" + startPage + ']'
: "[" + startPage + '-' + endPage + ']';
}
}