package com.coreoz.plume.file.utils;

import java.net.FileNameMap;
import java.net.URLConnection;

public class FileNameUtils {

	private static final FileNameMap MIME_TYPE_FILENAME_MAP = URLConnection.getFileNameMap();

	/**
	 * Remove all weird characters while trying to ensure
	 * the sanitize filename is close to the original one:
	 * accent are stripped, spaces are replaced by -,
	 * upper case chars are converted to lower case.
	 *
	 * If the filename is null, null is returned
	 */
	public static String sanitize(String filename) {
		if(filename == null) {
			return null;
		}

		return StringUtils
				.stripAccents(filename)
				.toLowerCase()
				.replaceAll("\\s+", "-")
				.replaceAll("[^a-z0-9-_\\.]", "");
	}

	/**
	 * Find the corresponding mime type for the file name.
	 *
	 * If no mime type if found or the file name is empty,
	 * null is returned.
	 */
	public static String guessMimeType(String filename) {
		if(filename == null) {
			return null;
		}

		return MIME_TYPE_FILENAME_MAP.getContentTypeFor(filename);
	}

}

@@ -47,177 +47,109 @@
 ava.
-net.FileNameMap;%0Aimport java.net.URLConnection;%0A%0Apublic class FileNameUtils %7B%0A%0A%09private static final FileNameMap MIME_TYPE_FILENAME_MAP = URLConnection.getFileNameMap();
+io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0A%0Apublic class FileNameUtils %7B
 %0A%0A%09/
@@ -909,66 +909,116 @@
 %0A%0A%09%09
-return MIME_TYPE_FILENAME_MAP.getContentTypeFor(filename);
+try %7B%0A%09%09%09return Files.probeContentType(Paths.get(filename));%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09return null;%0A%09%09%7D
 %0A%09%7D%0A

Improve mimeType detection