workspace-tree-widget/src/main/java/org/gcube/portlets/user/workspace/server/util/MimeTypeUtility.java

252 lines
7.1 KiB
Java

/**
*
*/
package org.gcube.portlets.user.workspace.server.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
/**
* The Class MimeTypeUtil.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Copied from org.gcube.common.homelibrary.util.MimeTypeUtil
* @author Federico De Faveri defaveri@isti.cnr.it
*
* Jul 5, 2019
*/
public class MimeTypeUtility {
protected static Logger logger = Logger.getLogger(MimeTypeUtility.class);
/**
*
*/
public static final String BINARY_MIMETYPE = "application/octet-stream";
public static final String[] ZIP_MIMETYPES = new String[] { "application/octet-stream", "application/x-compress",
"application/x-compressed", "application/x-zip-compressed", "application/x-gzip", "application/x-winzip",
"application/x-zip", "application/zip", "multipart/x-zip" };
protected static final Map<String, List<String>> mimetype_extension_map = new LinkedHashMap<String, List<String>>();
protected static final Map<String, String> extension_mimetype_map = new LinkedHashMap<String, String>();
static {
InputStream extensionToMimetype = MimeTypeUtility.class.getResourceAsStream("/ExtensionToMimeTypeMap.properties");
InputStream mimetypeToExtension = MimeTypeUtility.class.getResourceAsStream("/MimeTypeToExtensionMap.properties");
try {
loadExtensions(extensionToMimetype);
loadMimeTypes(mimetypeToExtension);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Load extensions.
*
* @param is the is
* @throws IOException Signals that an I/O exception has occurred.
*/
protected static void loadExtensions(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
while (line != null) {
String[] split = line.split("=");
if (split.length == 2) {
String mimeType = split[0];
String extension = split[1];
extension_mimetype_map.put(extension, mimeType);
}
line = br.readLine();
}
br.close();
}
/**
* Load mime types.
*
* @param is the is
* @throws IOException Signals that an I/O exception has occurred.
*/
protected static void loadMimeTypes(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
while (line != null) {
String[] split = line.split("=");
if (split.length == 2) {
String mimeType = split[0];
String extension = split[1];
List<String> toExtensions = mimetype_extension_map.get(mimeType);
if(toExtensions==null) {
toExtensions = new ArrayList<String>();
}
toExtensions.add(extension);
mimetype_extension_map.put(mimeType, toExtensions);
//mimetype_extension_map.put(mimeType, extension);
}
line = br.readLine();
}
br.close();
}
/**
* Gets the extension.
*
* @param mimeType the mime type.
* @return the related list of Extensions.
*/
public static List<String> getExtension(String mimeType) {
return mimetype_extension_map.get(mimeType);
}
/**
* Gets the name with extension.
*
* @param name the file name.
* @param mimeType the mime type.
* @return the right name.
* @throws IOException Signals that an I/O exception has occurred.
*/
public static String getNameWithExtension(String name, String mimeType) throws IOException {
logger.trace("getNameWithExtension name: " + name + " mimeType: " + mimeType);
String declaredExtension = FilenameUtils.getExtension(name);
if(declaredExtension.isEmpty()) {
if (mimeType == null)
return name;
List<String> extensions = MimeTypeUtility.getExtension(mimeType);
logger.trace("Available into map extensions is/are: " + extensions);
String extension;
if(extensions==null) {
extension = "";
}else {
extension = extensions.get(extensions.size()-1); //I'm reading the last extension in the map (myme type - list of extensions)
logger.debug("Using the last extension read into list of available extensions: " + extension);
extension = "." + extension;
}
logger.trace("returning " + name + extension);
return name + extension;
}else {
logger.trace("extension already declared in the name "+name+", returning name like is it");
return name;
}
}
/**
* Check if the content type is a zip type.
*
* @param contentType the content type to check.
* @return <code>true</code> if is a zip file, <code>false</code> otherwise.
*/
public static boolean isZipContentType(String contentType) {
for (String zip_mimetype : ZIP_MIMETYPES)
if (zip_mimetype.equals(contentType))
return true;
return false;
}
/**
* Get mime type by file.
*
* @param filenameWithExtension the filename with extension
* @param tmpFile the tmp file
* @return the mime type of the given file
*/
public static String getMimeType(String filenameWithExtension, File tmpFile) {
MediaType mediaType = null;
try {
TikaConfig config = TikaConfig.getDefaultConfig();
Detector detector = config.getDetector();
TikaInputStream stream = TikaInputStream.get(tmpFile);
Metadata metadata = new Metadata();
metadata.add(Metadata.RESOURCE_NAME_KEY, filenameWithExtension);
mediaType = detector.detect(stream, metadata);
} catch (IOException e) {
logger.error("Error detecting mime type for file " + filenameWithExtension);
}
return mediaType.getBaseType().toString();
}
/**
* Get mime type by inpustream.
*
* @param filenameWithExtension the filename with extension
* @param input the input
* @return the mime type of the given file
* @throws IOException Signals that an I/O exception has occurred.
*/
public static String getMimeType(String filenameWithExtension, InputStream input) throws IOException {
MediaType mediaType = null;
try {
TikaConfig config = TikaConfig.getDefaultConfig();
Detector detector = config.getDetector();
TikaInputStream stream = TikaInputStream.get(input);
Metadata metadata = new Metadata();
metadata.add(Metadata.RESOURCE_NAME_KEY, filenameWithExtension);
mediaType = detector.detect(stream, metadata);
} catch (IOException e) {
logger.error("Error detecting mime type for file " + filenameWithExtension);
} finally {
// if (file!=null)
// file.close();
}
return mediaType.getBaseType().toString();
}
/**
* Gets the mime type to extension map.
*
* @return the mime type to extension map
*/
public static Map<String, List<String>> getMimeTypeToExtensionMap() {
return mimetype_extension_map;
}
/**
* Gets the extension to mime type map.
*
* @return the extension to mime type map
*/
public static Map<String, String> getExtensionToMimeTypeMap() {
return extension_mimetype_map;
}
}