WebEnabledImageCreator.java 6.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.image.Layer
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.commons.mime.MimeTypeService
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.commons.util;

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.OrientationUtil;
import com.day.image.Layer;
import java.awt.Color;
import java.awt.image.BufferedImage;
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 javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.commons.mime.MimeTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Deprecated
public class WebEnabledImageCreator {
    private static final Logger log = LoggerFactory.getLogger(WebEnabledImageCreator.class);
    private static final String WEB_SPECIFIER = "web";
    private Asset asset;
    private MimeTypeService mimeTypeService;

    @Deprecated
    public WebEnabledImageCreator(Asset asset, MimeTypeService mimeTypeService) {
        this.asset = asset;
        this.mimeTypeService = mimeTypeService;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Deprecated
    public void create(BufferedImage image, String defaultMimetype, String dimensions, String keepFormat, String qualityStr, boolean force) throws RepositoryException, IOException {
        int maxWidth = this.getDimension(dimensions)[0];
        int maxHeight = this.getDimension(dimensions)[1];
        String oriMimeType = this.getMimeType(this.asset);
        String mimetype = StringUtils.isNotBlank((String)oriMimeType) && keepFormat.contains(oriMimeType) ? oriMimeType : defaultMimetype;
        double quality = mimetype.equals("image/gif") ? this.getQuality(255.0, qualityStr) : this.getQuality(1.0, qualityStr);
        Layer layer = this.createImage(image, maxWidth, maxHeight);
        OrientationUtil.adjustOrientation(this.asset, layer);
        String renditionName = "cq5dam.web." + maxWidth + "." + maxHeight + "." + this.getExtension(mimetype);
        if (StringUtils.contains((String)keepFormat, (String)oriMimeType)) {
            if (image.getHeight() == layer.getHeight() && image.getWidth() == layer.getWidth() && !force) {
                InputStream oriIs = null;
                try {
                    oriIs = ((Node)this.asset.getOriginal().adaptTo(Node.class)).getProperty("jcr:content/jcr:data").getBinary().getStream();
                    this.asset.addRendition(renditionName, oriIs, mimetype);
                }
                finally {
                    IOUtils.closeQuietly((InputStream)oriIs);
                }
            }
            this.saveImage(this.asset, layer, mimetype, quality, renditionName);
        } else {
            this.saveImage(this.asset, layer, mimetype, quality, renditionName);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void saveImage(Asset asset, Layer layer, String mimetype, double quality, String renditionName) throws IOException {
        File tmpFile = File.createTempFile("web", "." + this.getExtension(mimetype));
        FileOutputStream out = FileUtils.openOutputStream((File)tmpFile);
        FileInputStream is = null;
        try {
            layer.write(mimetype, quality, (OutputStream)out);
            is = FileUtils.openInputStream((File)tmpFile);
            asset.addRendition(renditionName, (InputStream)is, mimetype);
        }
        finally {
            IOUtils.closeQuietly((OutputStream)out);
            IOUtils.closeQuietly((InputStream)is);
            FileUtils.deleteQuietly((File)tmpFile);
        }
    }

    protected Layer createImage(BufferedImage image, int maxWidth, int maxHeight) {
        long startTime = System.currentTimeMillis();
        Layer layer = new Layer(image);
        int height = layer.getHeight();
        int width = layer.getWidth();
        if (height > maxHeight || width > maxWidth) {
            int newHeight;
            int newWidth;
            if (height > width) {
                newHeight = maxHeight;
                newWidth = Math.round((float)width * (float)maxHeight / (float)height);
                if (newWidth > maxWidth) {
                    newWidth = maxWidth;
                    newHeight = Math.round((float)height * (float)maxWidth / (float)width);
                }
            } else {
                newWidth = maxWidth;
                newHeight = Math.round((float)height * (float)maxWidth / (float)width);
                if (newHeight > maxHeight) {
                    newHeight = maxHeight;
                    newWidth = Math.round((float)width * (float)maxHeight / (float)height);
                }
            }
            layer.resize(newWidth, newHeight);
        }
        if (this.asset.getName().endsWith(".gif")) {
            layer.setTransparency(new Color(-991024));
        }
        log.debug("createImage took " + (System.currentTimeMillis() - startTime) + "ms");
        return layer;
    }

    protected String getExtension(String mimetype) {
        return this.mimeTypeService.getExtension(mimetype);
    }

    protected String getMimeType(Asset asset) {
        String name = asset.getName().toLowerCase();
        return this.mimeTypeService.getMimeType(name);
    }

    protected Integer[] getDimension(String dimensions) {
        if (dimensions != null) {
            String[] splits = dimensions.split(":");
            Integer[] d = new Integer[]{Integer.valueOf(splits[0]), Integer.valueOf(splits[1])};
            return d;
        }
        return new Integer[]{1000, 1000};
    }

    protected double getQuality(double base, String qualityStr) {
        int q = Integer.valueOf(qualityStr);
        double res = base * (double)q / 100.0;
        return res;
    }
}