VideoProfile.java 5.61 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.cq.dam.api.RenditionPicker
 *  com.day.cq.dam.commons.util.PrefixRenditionPicker
 *  javax.jcr.Node
 *  org.apache.commons.lang.StringUtils
 *  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.video;

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.RenditionPicker;
import com.day.cq.dam.commons.util.PrefixRenditionPicker;
import java.awt.Dimension;
import javax.jcr.Node;
import org.apache.commons.lang.StringUtils;
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;

public class VideoProfile {
    public static final String PROFILE_BASE_PATH = "/etc/dam/video";
    public static final String PROFILE_TYPE_CQ = "dam/components/video/profile";
    public static final String PROFILE_TYPE_S7 = "dam/components/video/s7profile";
    private Resource resource;

    public static VideoProfile get(ResourceResolver resolver, String name) {
        String path = name.startsWith("/") ? name : "/etc/dam/video/" + name;
        Resource resource = resolver.getResource(path);
        if (resource != null) {
            return new VideoProfile(resource);
        }
        return null;
    }

    public VideoProfile(Resource resource) {
        this.resource = "jcr:content".equals(resource.getName()) ? resource.getParent() : resource;
    }

    public String getName() {
        return this.resource.getName();
    }

    public String getPath() {
        return this.resource.getPath();
    }

    public Resource getResource() {
        return this.resource;
    }

    public Resource getContentResource() {
        return this.resource.getResourceResolver().getResource(this.resource, "jcr:content");
    }

    public ValueMap getProperties() {
        return ResourceUtil.getValueMap((Resource)this.getContentResource());
    }

    public Node getContentNode() {
        Resource content = this.getContentResource();
        return content == null ? null : (Node)content.adaptTo(Node.class);
    }

    public String getHtmlType() {
        return (String)this.getProperties().get("htmlType", (Object)"");
    }

    public Rendition getRendition(Asset asset) {
        if (this.isCQProfile()) {
            String prefix = "cq5dam.video." + this.getName();
            return asset.getRendition((RenditionPicker)new PrefixRenditionPicker(prefix));
        }
        if (this.isS7Profile()) {
            String prefix = "cq5dam.video.s7." + this.getPresetHandle();
            return asset.getRendition((RenditionPicker)new PrefixRenditionPicker(prefix));
        }
        return null;
    }

    public Dimension getOutputSize() {
        ValueMap props = this.getProperties();
        Integer width = (Integer)props.get("width", Integer.class);
        Integer height = (Integer)props.get("height", Integer.class);
        if (width != null && height != null) {
            return new Dimension(width, height);
        }
        return null;
    }

    public String getHtmlSource(Rendition rendition) {
        String htmlSource = "";
        if (rendition != null && this.isCQProfile()) {
            htmlSource = rendition.getPath();
        } else if (rendition != null && this.isS7Profile()) {
            htmlSource = this.getScene7Url(rendition);
        }
        return htmlSource;
    }

    public String getStrobeVideoSource(Rendition rendition) {
        String source = "";
        if (rendition != null && this.isCQProfile()) {
            source = "../../../../.." + rendition.getPath();
        } else if (rendition != null && this.isS7Profile()) {
            source = this.getScene7Url(rendition);
        }
        return source;
    }

    public String getFlvVideoSource(Rendition rendition) {
        String source = "";
        if (rendition != null && this.isCQProfile()) {
            source = rendition.getPath();
        } else if (rendition != null && this.isS7Profile()) {
            source = this.getScene7Url(rendition);
        }
        return source;
    }

    public String getCustomVideoSource(Rendition rendition) {
        return this.getFlvVideoSource(rendition);
    }

    private boolean isS7Profile() {
        return this.getResourceType().equals("dam/components/video/s7profile");
    }

    private boolean isCQProfile() {
        return this.getResourceType().equals("dam/components/video/profile");
    }

    private String getResourceType() {
        String resourcType = "";
        Resource content = this.resource.getChild("jcr:content");
        if (content != null) {
            resourcType = content.getResourceType();
        }
        return resourcType;
    }

    private String getPresetHandle() {
        String presetHandle = (String)this.getProperties().get("preset", (Object)"");
        if (StringUtils.isNotBlank((String)presetHandle)) {
            int lastIndex = presetHandle.lastIndexOf("/");
            presetHandle = presetHandle.substring(lastIndex + 1, presetHandle.length());
            presetHandle = presetHandle.replaceFirst("ps-", "");
        }
        return presetHandle;
    }

    private String getScene7Url(Rendition rendition) {
        String scene7Url = "";
        if (rendition != null) {
            scene7Url = (String)rendition.getProperties().get("scene7.url", (Object)"");
        }
        return scene7Url;
    }
}