Search in sources :

Example 1 with RuntimeExecutableContentTransformerOptions

use of org.alfresco.repo.content.transform.RuntimeExecutableContentTransformerOptions in project alfresco-repository by Alfresco.

the class TransformationOptionsConverter method getOptions.

public Map<String, String> getOptions(TransformationOptions options, String sourceMimetype, String targetMimetype) {
    boolean sourceIsPdf = MIMETYPE_PDF.equals(sourceMimetype);
    Map<String, String> map = new HashMap<>();
    map.put(TIMEOUT, "-1");
    if (options != null) {
        if (options instanceof ImageTransformationOptions) {
            ImageTransformationOptions opts = (ImageTransformationOptions) options;
            // TODO We don't support this any more for security reasons, however it might be possible to
            // extract some of the well know values and add them to the newer ImageMagick transform options.
            String commandOptions = opts.getCommandOptions();
            if (commandOptions != null && !commandOptions.isBlank()) {
                logger.error("ImageMagick commandOptions are no longer supported for security reasons: " + commandOptions);
            }
            ImageResizeOptions imageResizeOptions = opts.getResizeOptions();
            if (imageResizeOptions != null) {
                int width = imageResizeOptions.getWidth();
                int height = imageResizeOptions.getHeight();
                ifSet(width != -1, map, RESIZE_WIDTH, width);
                ifSet(height != -1, map, RESIZE_HEIGHT, height);
                ifSet(imageResizeOptions.isResizeToThumbnail(), map, THUMBNAIL, true);
                ifSet(imageResizeOptions.isPercentResize(), map, RESIZE_PERCENTAGE, true);
                map.put(ALLOW_ENLARGEMENT, Boolean.toString(imageResizeOptions.getAllowEnlargement()));
                map.put(MAINTAIN_ASPECT_RATIO, Boolean.toString(imageResizeOptions.isMaintainAspectRatio()));
            }
            ifSet(MimetypeMap.MIMETYPE_IMAGE_JPEG.equalsIgnoreCase(targetMimetype), map, ALPHA_REMOVE, true);
            map.put(AUTO_ORIENT, Boolean.toString(opts.isAutoOrient()));
            Collection<TransformationSourceOptions> sourceOptionsList = opts.getSourceOptionsList();
            if (sourceOptionsList != null) {
                for (TransformationSourceOptions transformationSourceOptions : sourceOptionsList) {
                    if (transformationSourceOptions instanceof PagedSourceOptions) {
                        PagedSourceOptions pagedSourceOptions = (PagedSourceOptions) transformationSourceOptions;
                        // The legacy transformer options start at page 1, where as image magick and the local
                        // transforms start at 0;
                        Integer startPageNumber = pagedSourceOptions.getStartPageNumber() - 1;
                        Integer endPageNumber = pagedSourceOptions.getEndPageNumber() - 1;
                        // PAGE is not an imagemagick option, but pdfRederer was incorrectly created initially using these options
                        if (startPageNumber == endPageNumber && sourceIsPdf) {
                            map.put(PAGE, Integer.toString(startPageNumber));
                        } else {
                            map.put(START_PAGE, Integer.toString(startPageNumber));
                            map.put(END_PAGE, Integer.toString(endPageNumber));
                        }
                    } else if (transformationSourceOptions instanceof CropSourceOptions) {
                        CropSourceOptions cropSourceOptions = (CropSourceOptions) transformationSourceOptions;
                        String gravity = cropSourceOptions.getGravity();
                        boolean percentageCrop = cropSourceOptions.isPercentageCrop();
                        int height = cropSourceOptions.getHeight();
                        int width = cropSourceOptions.getWidth();
                        int xOffset = cropSourceOptions.getXOffset();
                        int yOffset = cropSourceOptions.getYOffset();
                        ifSet(gravity != null, map, CROP_GRAVITY, gravity);
                        ifSet(percentageCrop, map, CROP_PERCENTAGE, percentageCrop);
                        ifSet(width != -1, map, CROP_WIDTH, width);
                        ifSet(height != -1, map, CROP_HEIGHT, height);
                        map.put(CROP_X_OFFSET, Integer.toString(xOffset));
                        map.put(CROP_Y_OFFSET, Integer.toString(yOffset));
                    } else if (transformationSourceOptions instanceof TemporalSourceOptions) {
                        TemporalSourceOptions temporalSourceOptions = (TemporalSourceOptions) transformationSourceOptions;
                        String duration = temporalSourceOptions.getDuration();
                        String offset = temporalSourceOptions.getOffset();
                        ifSet(duration != null, map, DURATION, duration);
                        ifSet(offset != null, map, OFFSET, offset);
                    } else {
                        logger.error("TransformationOption sourceOptionsList contained a " + transformationSourceOptions.getClass().getName() + ". It is not know how to convert this into newer transform options.");
                    }
                }
            }
        } else if (options instanceof SWFTransformationOptions) {
            SWFTransformationOptions opts = (SWFTransformationOptions) options;
            map.put(FLASH_VERSION, opts.getFlashVersion());
        } else if (options instanceof RuntimeExecutableContentTransformerOptions) {
            RuntimeExecutableContentTransformerOptions opts = (RuntimeExecutableContentTransformerOptions) options;
            map.putAll(opts.getPropertyValues());
        } else if (!options.getClass().equals(TransformationOptions.class)) {
            throw new IllegalArgumentException("Unable to convert " + options.getClass().getSimpleName() + " to new transform options held in a Map<String,String>.\n" + "The TransformOptionConverter may need to be sub classed to support this conversion.");
        }
    }
    return map;
}
Also used : TemporalSourceOptions(org.alfresco.service.cmr.repository.TemporalSourceOptions) HashMap(java.util.HashMap) ImageTransformationOptions(org.alfresco.repo.content.transform.magick.ImageTransformationOptions) PagedSourceOptions(org.alfresco.service.cmr.repository.PagedSourceOptions) CropSourceOptions(org.alfresco.service.cmr.repository.CropSourceOptions) SWFTransformationOptions(org.alfresco.repo.content.transform.swf.SWFTransformationOptions) ImageResizeOptions(org.alfresco.repo.content.transform.magick.ImageResizeOptions) RuntimeExecutableContentTransformerOptions(org.alfresco.repo.content.transform.RuntimeExecutableContentTransformerOptions) TransformationSourceOptions(org.alfresco.service.cmr.repository.TransformationSourceOptions)

Aggregations

HashMap (java.util.HashMap)1 RuntimeExecutableContentTransformerOptions (org.alfresco.repo.content.transform.RuntimeExecutableContentTransformerOptions)1 ImageResizeOptions (org.alfresco.repo.content.transform.magick.ImageResizeOptions)1 ImageTransformationOptions (org.alfresco.repo.content.transform.magick.ImageTransformationOptions)1 SWFTransformationOptions (org.alfresco.repo.content.transform.swf.SWFTransformationOptions)1 CropSourceOptions (org.alfresco.service.cmr.repository.CropSourceOptions)1 PagedSourceOptions (org.alfresco.service.cmr.repository.PagedSourceOptions)1 TemporalSourceOptions (org.alfresco.service.cmr.repository.TemporalSourceOptions)1 TransformationSourceOptions (org.alfresco.service.cmr.repository.TransformationSourceOptions)1