FileThumbnailProvider.java 3.16 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.thumbnail.ThumbnailProvider
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 */
package com.day.cq.dam.commons.thumbnail.provider;

import com.day.cq.commons.thumbnail.ThumbnailProvider;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;

@Component(metatype=0)
@Service
@Property(name="thumbnail.provider.name", value={"nt:file"})
public class FileThumbnailProvider
implements ThumbnailProvider {
    private static final String LIGHBOX_ASSETREF_SEPARATOR = "_:_";
    private static final String PROP_NAME_ASSETREFS = "assetRefs";
    private static final Pattern lightboxPattern = Pattern.compile("/home/users/(.*)/(.*)/profile/lightbox/(.*)");

    public String getThumbnailPath(Resource resource, int width, int height, Map<String, Object> additionalConf) {
        if (this.isLightBoxAsset(resource)) {
            return this.getThumbnailPathForLightboxEntry(resource, width, height);
        }
        return null;
    }

    private String getThumbnailPathForLightboxEntry(Resource entry, int width, int height) {
        String[] assetRefs;
        String path = null;
        String lightboxPath = entry.getParent().getPath();
        ResourceResolver resolver = entry.getResourceResolver();
        ValueMap lightboxProps = ResourceUtil.getValueMap((Resource)resolver.getResource(lightboxPath));
        for (String ref : assetRefs = (String[])lightboxProps.get("assetRefs", (Object)new String[0])) {
            Asset asset;
            String assetPath;
            Rendition rendition;
            Resource assetResource;
            String renditionName;
            String[] fragments = StringUtils.splitByWholeSeparator((String)ref, (String)"_:_");
            if (2 != fragments.length || !ResourceUtil.getName((Resource)entry).equals(fragments[0]) || null == (asset = (Asset)(assetResource = resolver.getResource(assetPath = fragments[1])).adaptTo(Asset.class)) || null == (rendition = asset.getRendition(renditionName = "cq5dam.thumbnail." + width + "." + height + ".png"))) continue;
            path = rendition.getPath();
        }
        return path;
    }

    private boolean isLightBoxAsset(Resource resource) {
        Matcher matcher = lightboxPattern.matcher(resource.getPath());
        return matcher.matches();
    }
}