ThumbnailHelper.java 7.97 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.image.Layer
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.ValueFactory
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.io.IOUtils
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.core.impl;

import com.day.cq.dam.core.impl.ui.preview.PreviewOptions;
import com.day.image.Layer;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Collections;
import java.util.Map;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.resource.collection.ResourceCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThumbnailHelper {
    private static final Logger log = LoggerFactory.getLogger(ThumbnailHelper.class);
    private static final String MIMETYPE = "image/jpeg";
    private static final String MANUAL_THUMBNAIL = "manualThumbnail.jpg";
    private static final String PARAMETER_MANUAL_THUMBNAIL = "coverImage";
    private static final String PARAMETER_REMOVE_MANUAL_THUMBNAIL = "removemanualthumbnail";

    public static void updateManualThumbnail(SlingHttpServletRequest request, Resource res, PreviewOptions options) throws IOException {
        boolean removeManualThumbnail = Boolean.valueOf(request.getParameter("removemanualthumbnail"));
        if (removeManualThumbnail) {
            ThumbnailHelper.removeManualThumbnail(res);
        } else {
            RequestParameter thumbnailParameter = request.getRequestParameter("coverImage");
            if (thumbnailParameter != null && thumbnailParameter.getSize() > 0) {
                InputStream thumbnailStream = thumbnailParameter.getInputStream();
                ThumbnailHelper.setManualThumbnail(res, thumbnailStream, options);
            }
        }
    }

    private static void setManualThumbnail(Resource res, InputStream thumbnailStream, PreviewOptions options) throws IOException {
        if (res != null) {
            Resource targetResource = ThumbnailHelper.getTargetResource(res);
            ThumbnailHelper.saveThumbnail(targetResource, thumbnailStream, options);
        }
    }

    private static Resource getTargetResource(Resource res) {
        Resource targetResource = null;
        ResourceCollection coll = (ResourceCollection)res.adaptTo(ResourceCollection.class);
        targetResource = coll != null ? res : res.getChild("jcr:content");
        return targetResource;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private static void saveThumbnail(Resource targetResource, InputStream thumbnailStream, PreviewOptions options) throws IOException {
        FileOutputStream itout = null;
        File imageTmpFile = null;
        FileInputStream is = null;
        try {
            imageTmpFile = File.createTempFile("image", ".tmp");
            Layer thumbnailLayer = new Layer(thumbnailStream);
            itout = new FileOutputStream(imageTmpFile);
            thumbnailLayer = ThumbnailHelper.adjustLayer(thumbnailLayer, options);
            thumbnailLayer.write("image/jpeg", 1.0, (OutputStream)itout);
            is = new FileInputStream(imageTmpFile);
            Resource assetContent = ThumbnailHelper.createFolderThumbnailContent(targetResource);
            Node node = (Node)assetContent.adaptTo(Node.class);
            if (!node.hasNode("jcr:content")) {
                log.debug("Unable to write thumbnail " + node.getPath());
                return;
            }
            Node thumbnailContent = node.getNode("jcr:content");
            thumbnailContent.setProperty("jcr:data", node.getSession().getValueFactory().createBinary((InputStream)is));
            thumbnailContent.setProperty("width", (long)options.getWidth());
            thumbnailContent.setProperty("height", (long)options.getHeight());
            thumbnailContent.setProperty("bgcolor", (long)options.getBgcolor().getRGB());
            thumbnailContent.setProperty("jcr:lastModified", Calendar.getInstance());
        }
        catch (RepositoryException re) {
            log.warn("could not save thumbnail" + re.getMessage());
        }
        finally {
            IOUtils.closeQuietly((InputStream)is);
            IOUtils.closeQuietly((OutputStream)itout);
            FileUtils.deleteQuietly((File)imageTmpFile);
        }
    }

    private static Resource createResource(Resource resource, String name, String type) throws PersistenceException {
        ResourceResolver resourceResolver = resource.getResourceResolver();
        Resource thumbnailResource = resourceResolver.getResource(resource.getPath() + "/" + name);
        if (null != thumbnailResource) {
            return thumbnailResource;
        }
        return resourceResolver.create(resource, name, Collections.singletonMap("jcr:primaryType", type));
    }

    private static Resource createFolderThumbnailContent(Resource resource) throws PersistenceException {
        Resource folderThumbnail = ThumbnailHelper.createResource(resource, "manualThumbnail.jpg", "nt:file");
        ThumbnailHelper.createResource(folderThumbnail, "jcr:content", "nt:unstructured");
        return folderThumbnail;
    }

    private static Layer adjustLayer(Layer layer, PreviewOptions options) {
        Rectangle rect;
        double aspectRatioThumb;
        double aspectRatioImage = (double)layer.getWidth() / (double)(layer.getHeight() + 1);
        if (aspectRatioImage < (aspectRatioThumb = (double)options.getWidth() / (double)(options.getHeight() + 1))) {
            int width = Math.min(options.getWidth(), layer.getWidth());
            layer.resize(width, layer.getHeight() * width / (layer.getWidth() + 1));
            rect = new Rectangle(0, Math.max((layer.getHeight() - options.getHeight()) / 2, 0), options.getWidth(), options.getHeight());
        } else {
            int height = Math.min(options.getHeight(), layer.getHeight());
            layer.resize(layer.getWidth() * height / (layer.getHeight() + 1), height);
            rect = new Rectangle(Math.max((layer.getWidth() - options.getWidth()) / 2, 0), 0, options.getWidth(), options.getHeight());
        }
        layer.crop((Rectangle2D)rect);
        int x = (options.getWidth() - layer.getWidth()) / 2;
        int y = (options.getHeight() - layer.getHeight()) / 2;
        Layer bgLayer = new Layer(options.getWidth(), options.getHeight(), (Paint)options.getBgcolor());
        layer.setX(x);
        layer.setY(y);
        bgLayer.merge(layer);
        return bgLayer;
    }

    private static void removeManualThumbnail(Resource res) throws PersistenceException {
        Resource targetResource = ThumbnailHelper.getTargetResource(res);
        ResourceResolver resourceResolver = targetResource.getResourceResolver();
        Resource thumbnailResource = resourceResolver.getResource(targetResource.getPath() + "/" + "manualThumbnail.jpg");
        if (null != thumbnailResource) {
            resourceResolver.delete(thumbnailResource);
        }
    }
}