All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.cq.dam.core.process.CreatePdfPreviewProcess Maven / Gradle / Ivy

//******************************************************************************
// ADOBE CONFIDENTIAL
// ___________________
//
//  Copyright 2015 Adobe Systems Incorporated
//  All Rights Reserved.
//
// NOTICE:  All information contained herein is, and remains
// the property of Adobe Systems Incorporated and its suppliers,
// if any.  The intellectual and technical concepts contained
// herein are proprietary to Adobe Systems Incorporated and its
// suppliers and may be covered by U.S. and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Adobe Systems Incorporated.
//******************************************************************************

package com.day.cq.dam.core.process;

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.handler.AssetHandler;
import com.day.cq.dam.api.handler.store.AssetStore;
import com.day.cq.dam.commons.process.AbstractAssetWorkflowProcess;
import com.day.cq.dam.commons.util.AssetUpdate;
import com.day.cq.dam.commons.util.AssetUpdateMonitor;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.metadata.MetaDataMap;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.jcr.Session;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Workflow step that rasterizes PDF and Adobe Illustrator documents to a preview image rendition
 */
@Component
@Service
@Property(name = "process.label", value = "Rasterize PDF and Adobe Illustrator documents to a preview image rendition")
public class CreatePdfPreviewProcess extends AbstractAssetWorkflowProcess {

    private static final Logger log = LoggerFactory.getLogger(CreatePdfPreviewProcess.class);
    private static final String OPTION_MIME_TYPES = "MIME_TYPES";
    private static final String OPTION_MAX_WIDTH = "MAX_WIDTH";
    private static final String OPTION_MAX_HEIGHT = "MAX_HEIGHT";
    private static final String OPTION_RESOLUTION = "RESOLUTION";
    private static final String[] DEFAULT_MIME_TYPES = {};

    @Reference
    protected AssetStore assetStore;

    @Reference
    AssetUpdateMonitor monitor;

    @Override
    public void execute(final WorkItem wfItem, WorkflowSession wfSession, final MetaDataMap args) throws WorkflowException {
        AssetUpdate update = monitor.startUpdate(wfItem, getResourceResolver(wfSession.getSession()), this);
        update.checkAndRun(new AssetUpdate.AssetCheck() {
            @Override
            public boolean isAcceptable(Asset asset) throws WorkflowException {
            List mimeTypes = Arrays.asList(args.get(OPTION_MIME_TYPES, DEFAULT_MIME_TYPES));
            if (!mimeTypes.contains(asset.getMimeType())) {
                log.debug("Skipping asset, unsupported mime type: {} ({})", wfItem, asset.getMimeType());
                    return false;
                }
                return true;
            }

            @Override
            public boolean isNullAcceptable() throws WorkflowException {
                log.debug("Skipping work item, since it's not an asset: {}", wfItem);
                return false;
            }
        }, new AssetUpdate.Runner() {
            @Override
            public void run(Asset asset, AssetUpdate update) throws WorkflowException, Exception {
            // Retrieve page width and height
            ValueMap metadata = new ValueMapDecorator(asset.getMetadata());
            double pageWidth = metadata.get("dam:Physicalwidthininches", 0.0);
            double pageHeight = metadata.get("dam:Physicalheightininches", 0.0);
            if ((pageWidth == 0) || (pageHeight == 0)) {
                // Nothing to rasterize
                return;
            }

            // Determine rasterize resolution
            double maxWidth = getClamped(args, OPTION_MAX_WIDTH, 2048, 1, 32000);
            double maxHeight = getClamped(args, OPTION_MAX_HEIGHT, 2048, 1, 32000);
            double maxRes = Math.min(maxWidth / pageWidth, maxHeight / pageHeight);
            double res = getClamped(args, OPTION_RESOLUTION, 72, 1, maxRes);

            // Determine dimension to render at
            Dimension dim = new Dimension(
                    (int) Math.round(pageWidth * res),
                    (int) Math.round(pageHeight * res)
            );

            // Rasterize
            Rendition original = asset.getOriginal();
            AssetHandler assetHandler = assetStore.getAssetHandler(original.getMimeType());
            BufferedImage image = assetHandler.getImage(original, dim);
            log.info("Rasterized {} to an image with dim {}x{}", new Object[] { original.getPath(), image.getWidth(), image.getHeight() });

            // Store as PNG preview
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            boolean w = ImageIO.write(image, "png", out);
            if (w) {
                ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
                asset.setBatchMode(true);
                asset.addRendition("cqdam.preview.png", in, "image/png");
            }
        }
        });
    }

    private static double getClamped(MetaDataMap args, String option, double def, double min, double max) {
        double v = args.get(option, def);
        return Math.min(Math.max(v, min), max);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy