/*******************************************************************************
 * Copyright 2013 2Fi Business Solutions Ltd.
 *
 * This code is part of the Core project.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.core.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * File Utils
 * 
 * @author Patrick
 */
public abstract class FileUtils {

	private static final Map<String, String> MIMETYPES = new HashMap<>();

	static {
		MIMETYPES.put("pdf", "application/pdf");

		MIMETYPES.put("doc", "application/msword");
		MIMETYPES.put("dot", "application/msword");
		MIMETYPES.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

		MIMETYPES.put("xls", "application/vnd.ms-excel");
		MIMETYPES.put("xlm", "application/vnd.ms-excel");
		MIMETYPES.put("xla", "application/vnd.ms-excel");
		MIMETYPES.put("xlc", "application/vnd.ms-excel");
		MIMETYPES.put("xlt", "application/vnd.ms-excel");
		MIMETYPES.put("xlw", "application/vnd.ms-excel");
		MIMETYPES.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

		MIMETYPES.put("ppt", "application/vnd.ms-powerpoint");
		MIMETYPES.put("pps", "application/vnd.ms-powerpoint");
		MIMETYPES.put("pot", "application/vnd.ms-powerpoint");
		MIMETYPES.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");

		MIMETYPES.put("bat", "application/x-msdownload");
		MIMETYPES.put("com", "application/x-msdownload");
		MIMETYPES.put("dll", "application/x-msdownload");
		MIMETYPES.put("exe", "application/x-msdownload");
		MIMETYPES.put("msi", "application/x-msdownload");

		MIMETYPES.put("swf", "application/x-shockwave-flash");

		MIMETYPES.put("7z", "application/x-7z-compressed");
		MIMETYPES.put("rar", "application/x-rar-compressed");
		MIMETYPES.put("zip", "application/zip");

		MIMETYPES.put("js", "application/javascript");
		MIMETYPES.put("json", "application/json");

		MIMETYPES.put("mpga", "audio/mpeg");
		MIMETYPES.put("mp2", "audio/mpeg");
		MIMETYPES.put("mp2a", "audio/mpeg");
		MIMETYPES.put("mp3", "audio/mpeg");
		MIMETYPES.put("m2a", "audio/mpeg");
		MIMETYPES.put("m3a", "audio/mpeg");

		MIMETYPES.put("bmp", "image/bmp");
		MIMETYPES.put("gif", "image/gif");
		MIMETYPES.put("jpeg", "image/jpeg");
		MIMETYPES.put("jpg", "image/jpeg");
		MIMETYPES.put("jpe", "image/jpeg");
		MIMETYPES.put("png", "image/png");
		MIMETYPES.put("tiff", "image/tiff");
		MIMETYPES.put("tif", "image/tiff");
		MIMETYPES.put("avif", "image/avif");


		MIMETYPES.put("css", "text/css");

		MIMETYPES.put("csv", "text/csv");

		MIMETYPES.put("html", "text/html");
		MIMETYPES.put("htm", "text/html");

		MIMETYPES.put("txt", "text/plain");
		MIMETYPES.put("text", "text/plain");
		MIMETYPES.put("conf", "text/plain");
		MIMETYPES.put("log", "text/plain");

		MIMETYPES.put("mp4", "video/mp4");
		MIMETYPES.put("mp4v", "video/mp4");
		MIMETYPES.put("mpg4", "video/mp4");

		MIMETYPES.put("mkv", "video/x-matroska");

		MIMETYPES.put("mpeg", "video/mpeg");
		MIMETYPES.put("mpg", "video/mpeg");
		MIMETYPES.put("mpe", "video/mpeg");
		MIMETYPES.put("m1v", "video/mpeg");
		MIMETYPES.put("m2v", "video/mpeg");

		MIMETYPES.put("qt", "video/quicktime");
		MIMETYPES.put("mov", "video/quicktime");

		MIMETYPES.put("wmv", "video/x-ms-wmv");
		MIMETYPES.put("wmx", "video/x-ms-wmx");
		MIMETYPES.put("wvx", "video/x-ms-wvx");
		MIMETYPES.put("avi", "video/x-msvideo");

		// MIMETYPES.put("xxxxx", "xxxxx");
	}

	/**
	 * Guess the mimetype from the file name extension
	 * 
	 * @return The mimetype guessed from the file name extension, or {@code null} if the mimetype cannot be determined
	 */
	public static String guessMimetype(String filename) {
		String extension = StringUtils.substringAfterLast(filename, ".");
		extension = extension.toLowerCase();
		String mimetype = MIMETYPES.get(extension);
		return mimetype != null ? mimetype : "application/octet-stream";
	}

}
