package io.stardog.starwizard.services.media;

import io.stardog.starwizard.services.media.data.ImageVersion;

import javax.ws.rs.core.MediaType;
import java.io.File;

public class MediaUtil {
    /**
     * Given a MIME contentType string, return a MediaType object for that content type.
     *
     * @param contentType
     * @return
     */
    public static MediaType toMediaType(String contentType) {
        String[] explode = contentType.split("/");
        return new MediaType(explode[0], explode[1]);
    }

    /**
     * Given a media type, return an appropriate file extension for the media type.
     *
     * @param mediaType
     * @return
     */
    public static String toExtension(MediaType mediaType) {
        String result = mediaType.getSubtype().toLowerCase();
        if (result.indexOf('+') > -1) {
            result = result.substring(0, result.indexOf('+'));
        }
        if (result.equals("jpeg")) {
            result = "jpg";
        }
        return result;
    }

    /**
     * Given a file name, return the extension.
     * @param file  filename
     * @return  extension
     */
    public static String toExtension(String file) {
        int lastDot = file.lastIndexOf(".");
        return file.substring(lastDot+1).toLowerCase();
    }

    public static File toVersionFile(File sourceFile, String versionName) {
        return new File(sourceFile.getAbsolutePath().replace(".", "-" + versionName + "."));
    }

    public static String toVersionPath(String remotePath, String versionName) {
        return remotePath.replace(".", "-" + versionName + ".");
    }
}

@@ -102,16 +102,46 @@
 Version;
+%0Aimport org.im4java.core.Info;
 %0A%0Aimport
@@ -1634,11 +1634,333 @@
 );%0A    %7D
+%0A%0A    public static String getImageFormat(String file) throws InfoException %7B%0A        try %7B%0A            Info imageInfo = new Info(file, true);%0A            return imageInfo.getImageFormat();%0A        %7D catch (Exception e) %7B%0A            throw new InfoException(%22Unable to get file format: %22 + e.getMessage());%0A        %7D%0A    %7D
 %0A%7D%0A

Add util to determine image format