FileInputUtils.java 1.43 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.granite.rest.assets.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class FileInputUtils {
    public static InputStream getFileInputStream(Object filePropertyValue) {
        return (InputStream)FileInputUtils.getMapValue(filePropertyValue, "fileinput", InputStream.class);
    }

    public static Map<String, Object> getRenditionMap(Object filePropertyValue, String mimeTypeKey) {
        HashMap<String, Object> renditionMap = new HashMap<String, Object>();
        renditionMap.put(mimeTypeKey, (String)FileInputUtils.getMapValue(filePropertyValue, "mimetype", String.class));
        return renditionMap;
    }

    private static <T> Object getMapValue(Object expectedMap, String key, Class<T> expectedValueClass) {
        if (!(expectedMap instanceof Map)) {
            throw new RuntimeException("file value must be of type Map");
        }
        Map rendition = (Map)expectedMap;
        if (!rendition.containsKey(key)) {
            throw new RuntimeException("Expected key not found in file map: " + key);
        }
        if (!expectedValueClass.isAssignableFrom(rendition.get(key).getClass())) {
            throw new RuntimeException("Value of key " + key + " in file map is of unexpected type. Expected " + expectedValueClass.getName() + " but was " + rendition.get(key).getClass().getName());
        }
        return rendition.get(key);
    }
}