RenditionResource.java 5.07 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.Asset
 *  com.adobe.granite.asset.api.AssetManager
 *  com.adobe.granite.asset.api.Rendition
 *  com.adobe.granite.rest.utils.DeepModifiableValueMapDecorator
 *  org.apache.commons.io.IOUtils
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceMetadata
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 */
package com.adobe.granite.rest.assets.impl;

import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetManager;
import com.adobe.granite.asset.api.Rendition;
import com.adobe.granite.rest.assets.impl.AbstractAssetResource;
import com.adobe.granite.rest.assets.impl.AssetResourceProvider;
import com.adobe.granite.rest.assets.impl.FileInputUtils;
import com.adobe.granite.rest.utils.DeepModifiableValueMapDecorator;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;

public class RenditionResource
extends AbstractAssetResource {
    public static final String RESOURCE_TYPE = "granite/rest/assets/rendition";
    private final String mimeType;
    private long size = 0;
    private ValueMap valueMap;

    public RenditionResource(Resource resource, String path) {
        Resource content;
        super(resource, path);
        Rendition rendition = (Rendition)resource.adaptTo(Rendition.class);
        if (rendition != null) {
            this.size = rendition.getSize();
        }
        if ((content = resource.getChild("jcr:content")) == null) {
            this.mimeType = "application/octet-stream";
        } else {
            if (content.getResourceMetadata() != null) {
                this.resourceMetadata.setModificationTime(content.getResourceMetadata().getModificationTime());
            }
            ValueMap base = (ValueMap)content.adaptTo(ValueMap.class);
            this.mimeType = (String)base.get("jcr:mimeType", String.class);
            this.valueMap = new RenditionValueMapDecorator(this, base);
        }
    }

    public String getResourceType() {
        return "granite/rest/assets/rendition";
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        Resource content;
        if (type == ValueMap.class || type == ModifiableValueMap.class) {
            return (AdapterType)this.valueMap;
        }
        if (type == InputStream.class && (content = this.getResourceResolver().getResource(this.resource, "jcr:content")) != null) {
            return (AdapterType)content.adaptTo(InputStream.class);
        }
        return (AdapterType)super.adaptTo(type);
    }

    public String getMimeType() {
        return this.mimeType;
    }

    public long getSize() {
        return this.size;
    }

    private class RenditionValueMapDecorator
    extends DeepModifiableValueMapDecorator {
        private AssetManager assetMgr;
        private Asset asset;
        private String dataParameter;

        public RenditionValueMapDecorator(RenditionResource resource, ValueMap base) {
            super((Resource)resource, (Map)base, new String[0]);
            this.dataParameter = AssetResourceProvider.getDataPropertyKey(base);
            String fileName = "";
            if (this.dataParameter != null) {
                Map fileProperties = (Map)base.get((Object)"file");
                fileName = (String)base.get((Object)"name");
            }
            String name = (String)this.get("name", (Object)fileName);
            String path = resource.getParent().getParent().getPath();
            String resourcePath = AssetResourceProvider.getResourcePath(path, name);
            this.assetMgr = (AssetManager)resource.getResourceResolver().adaptTo(AssetManager.class);
            this.asset = this.assetMgr.getAsset(resourcePath);
        }

        public Object put(String key, Object value) {
            if ("file".equals(key) || key.equals(this.dataParameter)) {
                return this.addRendition(this.asset, RenditionResource.this.resource.getName(), FileInputUtils.getFileInputStream(value), FileInputUtils.getRenditionMap(value, "rendition.mime"));
            }
            return super.put(key, value);
        }

        /*
         * WARNING - Removed try catching itself - possible behaviour change.
         */
        private Rendition addRendition(Asset asset, String renditionName, InputStream is, Map<String, Object> renditionProperties) {
            if (asset != null) {
                try {
                    Rendition rendition = asset.setRendition(renditionName, is, renditionProperties);
                    return rendition;
                }
                finally {
                    IOUtils.closeQuietly((InputStream)is);
                }
            }
            return null;
        }
    }

}