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

@@ -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,31 +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.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.PdfRendererCommandExecutor;
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 alfresco-pdf-renderer transformer.
@@ -111,68 +97,22 @@ public class AlfrescoPdfRendererController extends AbstractTransformerController
@Override
protected void executeTransformCommand(File sourceFile, File targetFile)
{
commandExecutor.run("", sourceFile, targetFile, null);
transform(null, null, null, Collections.emptyMap(), sourceFile, targetFile);
}
};
}
@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 String getTransformerName(final File sourceFile, final String sourceMimetype,
final String targetMimetype, final Map<String, String> transformOptions)
{
logger.debug("Processing request with: sourceFile '{}', targetFile '{}', transformOptions" +
" '{}', timeout {} ms", sourceFile, targetFile, transformOptions, timeout);
final String options = PdfRendererOptionsBuilder
.builder()
.withPage(transformOptions.get("page"))
.withWidth(transformOptions.get("width"))
.withHeight(transformOptions.get("height"))
.withAllowPdfEnlargement(transformOptions.get("allowPdfEnlargement"))
.withMaintainPdfAspectRatio(transformOptions.get("maintainPdfAspectRatio"))
.build();
commandExecutor.run(options, sourceFile, targetFile, timeout);
return null; // does not matter what value is returned, as it is not used because there is only one.
}
@Deprecated
@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 = "page", required = false) Integer page,
@RequestParam(value = "width", required = false) Integer width,
@RequestParam(value = "height", required = false) Integer height,
@RequestParam(value = "allowPdfEnlargement", required = false) Boolean allowPdfEnlargement,
@RequestParam(value = "maintainPdfAspectRatio", required = false) Boolean maintainPdfAspectRatio)
@Override
protected void transform(String transformName, String sourceMimetype, String targetMimetype,
Map<String, String> transformOptions, File sourceFile, File targetFile)
{
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 = PdfRendererOptionsBuilder
.builder()
.withPage(page)
.withWidth(width)
.withHeight(height)
.withAllowPdfEnlargement(allowPdfEnlargement)
.withMaintainPdfAspectRatio(maintainPdfAspectRatio)
.build();
commandExecutor.run(options, sourceFile, 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;
commandExecutor.transform(sourceMimetype, targetMimetype, transformOptions, sourceFile, targetFile);
}
}

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.PdfRendererCommandExecutor;
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

@@ -26,15 +26,29 @@
*/
package org.alfresco.transformer.executors;
import org.alfresco.transform.exceptions.TransformException;
import org.alfresco.transformer.PdfRendererOptionsBuilder;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static org.alfresco.transformer.util.RequestParamMap.ALLOW_PDF_ENLARGEMENT;
import static org.alfresco.transformer.util.RequestParamMap.HEIGHT_REQUEST_PARAM;
import static org.alfresco.transformer.util.RequestParamMap.MAINTAIN_PDF_ASPECT_RATIO;
import static org.alfresco.transformer.util.RequestParamMap.PAGE_REQUEST_PARAM;
import static org.alfresco.transformer.util.RequestParamMap.TIMEOUT;
import static org.alfresco.transformer.util.RequestParamMap.WIDTH_REQUEST_PARAM;
import static org.alfresco.transformer.util.Util.stringToLong;
/**
* CommandExecutor implementation for running PDF Renderer transformations. It runs the
* transformation logic as a separate Shell process.
*/
public class PdfRendererCommandExecutor extends AbstractCommandExecutor
{
private static String ID = "pdfrenderer";
public static final String LICENCE = "This transformer uses alfresco-pdf-renderer which uses the PDFium library from Google Inc. See the license at https://pdfium.googlesource.com/pdfium/+/master/LICENSE or in /pdfium.txt";
private final String EXE;
@@ -50,6 +64,12 @@ public class PdfRendererCommandExecutor extends AbstractCommandExecutor
super.checkCommand = createCheckCommand();
}
@Override
public String getTransformerId()
{
return ID;
}
@Override
protected RuntimeExec createTransformCommand()
{
@@ -77,4 +97,23 @@ public class PdfRendererCommandExecutor 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 = PdfRendererOptionsBuilder
.builder()
.withPage(transformOptions.get(PAGE_REQUEST_PARAM))
.withWidth(transformOptions.get(WIDTH_REQUEST_PARAM))
.withHeight(transformOptions.get(HEIGHT_REQUEST_PARAM))
.withAllowPdfEnlargement(transformOptions.get(ALLOW_PDF_ENLARGEMENT))
.withMaintainPdfAspectRatio(transformOptions.get(MAINTAIN_PDF_ASPECT_RATIO))
.build();
Long timeout = stringToLong(transformOptions.get(TIMEOUT));
run(options, sourceFile, targetFile, timeout);
}
}