const path = require('path');

module.exports.getMimeType = function(file){
    let ext = path.extname(file).replace(/^\./, '').toLowerCase();
    let mime = db[ext];
    if(mime){
        return mime;
    }else{
        return 'text/plain';
    }
}

module.exports.opener = function(file){
    let mime = getMimeType(file);
    if(mime.split('/')[0] === 'text'){
        return 'editor';
    }else if(mime === 'application/pdf'){
        return 'pdf';
    }else if(mime.split('/')[0] === 'image'){
        return 'image';
    }else if(['application/javascript', 'application/xml', 'application/x-perl'].indexOf(mime) !== -1){
        return 'editor';
    }else if(['audio/wav', 'audio/mp3', 'audio/flac'].indexOf(mime) !== -1){
        return 'audio';
    }else if(['video/webm', 'video/mp4', 'application/ogg'].indexOf(mime) !== -1){
        return 'video';
    }else{
        return 'download';
    }
}

const db = {
    "html": "text/html",
    "shtml": "text/html",
    "htm": "text/html",
    "css": "text/css",
    "xml": "text/xml",
    "csv": "text/csv",
    "mml": "text/mathml",
    "txt": "text/plain",
    "jad": "text/vnd.sun.j2me.app-descriptor",
    "wml": "text/vnd.wap.wml",
    "htc": "text/x-component",
    "jpg": "image/jpeg",
    "jpeg": "image/jpeg",
    "svg": "image/svg",
    "png": "image/png",
    "svg": "image/svg+xml",
    "svgz": "image/svg+xml",
    "webp": "image/webp",
    "gif": "image/gif",
    "tiff": "image/tiff",
    "bmp": "image/bmp",
    "tif": "image/x-tif",
    "dng": "image/x-adobe-dng",
    "cr2": "image/x-canon-cr2",
    "crw": "image/x-canon-crw",
    "nef": "image/x-nikon-nef",
    "nrw": "image/x-nikon-nrw",
    "arw": "image/x-sony-arw",
    "sr2": "image/x-sony-sr2",
    "mrw": "image/x-minolta-mrw",
    "mdc": "image/x-minolta-mdc",
    "orf": "image/x-olympus-orf",
    "rw2": "image/x-panasonic-rw2",
    "pef": "image/x-pentax-pef",
    "x3f": "image/x-x3f",
    "raw": "image/x-raw",
    "kdc": "image/x-kodak-kdc",
    "dcr": "image/x-kodak-dcr",
    "srw": "image/x-samsung-srw",
    "erf": "image/x-epson-erf",
    "raf": "image/x-fuji-raf",
    "mos": "image/x-aptus-mos",
    "mef": "image/x-mamiya-mef",
    "3fr": "image/x-hasselblad-3fr",
    "ico": "image/x-icon",
    "wbmp": "image/vnd.wap.wbmp",
    "jng": "image/x-jng",
    "bmp": "image/x-ms-bmp",
    "mpeg": "audio/mpeg",
    "mpg": "audio/mpeg",
    "aif": "audio/x-aiff",
    "aiff": "audio/x-aiff",
    "ra": "audio/x-pn-realaudio",
    "ram": "audio/x-pn-realaudio",
    "wav": "audio/wave",
    "mp3": "audio/mp3",
    "flac": "audio/flac",
    "mid": "audio/midi",
    "kar": "audio/midi",
    "midi": "audio/midi",
    "ogg": "audio/ogg",
    "m4a": "audio/x-m4a",
    "ra": "audio/x-realaudio",
    "mid": "audio/midi",
    "kar": "audio/midi",
    "midi": "audio/midi",
    "m4a": "audio/x-m4a",
    "ra": "audio/x-realaudio",
    "3gpp": "video/3gpp",
    "3gp": "video/3gpp",
    "ts": "video/mp2t",
    "mp4": "video/mp4",
    "mpg": "video/mpeg",
    "mov": "video/quicktime",
    "webm": "video/webm",
    "flv": "video/x-flv",
    "m4v": "video/x-m4v",
    "mng": "video/x-mng",
    "asx": "video/x-ms-asf",
    "asf": "video/x-ms-asf",
    "wmv": "video/x-ms-wmv",
    "avi": "video/x-msvideo",
    "wma": "audio/x-ms-wma",
    "wmv": "video/x-ms-wmv",
    "webm": "video/webm",
    "mp4": "video/mp4",
    "mov": "video/quicktime",
    "avi": "video/x-msvideo",
    "pdf": "application/pdf",
    "cab": "application/vnd.ms-cab-compressed",
    "ogv": "application/ogg",
    "js": "application/javascript",
    "xml": "application/xml",
    "deb": "application/vnd.debian.binary-package",
    "dpkg": "application/dpkg-www-installer",
    "rpm": "application/x-rpm",
    "apk": "application/vnd.android.package-archive",
    "exe": "application/x-msdownload",
    "msi": "application/x-msdownload",
    "dmg": "application/x-apple-diskimage",
    "pkg": "application/x-newton-compatible-pkg",
    "tar": "application/x-tar",
    "zip": "application/x-zip",
    "gz": "application/x-gzip",
    "bz2": "application/x-bz2",
    "rar": "application/x-rar-compressed",
    "so": "application/octet-stream",
    "eps": "application/postscript",
    "ps": "application/postscript",
    "midi": "application/x-midi",
    "odg": "application/vnd.oasis.opendocument.graphics",
    "odp": "application/vnd.oasis.opendocument.presentation",
    "ods": "application/vnd.oasis.opendocument.spreadsheet",
    "odt": "application/vnd.oasis.opendocument.text",
    "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "doc": "application/msword",
    "pps": "application/vnd.ms-powerpoint",
    "ppt": "application/vnd.ms-powerpoint",
    "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    "rtf": "application/rtf",
    "swf": "application/x-shockwave-flash",
    "vrml": "application/x-vrml",
    "wrl": "x-world/x-vrml",
    "xls": "application/vnd.ms-excel",
    "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "ds_store": "application/octet-stream",
    "atom": "application/atom+xml",
    "rss": "application/rss+xml",
    "woff": "application/font-woff",
    "jar": "application/java-archive",
    "ear": "application/java-archive",
    "war": "application/java-archive",
    "json": "application/json",
    "hqx": "application/mac-binhex40",
    "doc": "application/msword",
    "ai": "application/postscript",
    "m3u8": "application/vnd.apple.mpegurl",
    "eot": "application/vnd.ms-fontobject",
    "wmlc": "application/vnd.wap.wmlc",
    "kml": "application/vnd.google-earth.kml+xml",
    "kmz": "application/vnd.google-earth.kmz",
    "7z": "application/x-7z-compressed",
    "cco": "application/x-cocoa",
    "jardiff": "application/x-java-archive-diff",
    "jnlp": "application/x-java-jnlp-file",
    "run": "application/x-makeself",
    "pl": "application/x-perl",
    "pm": "application/x-perl",
    "prc": "application/x-pilot",
    "pdb": "application/x-pilot",
    "rar": "application/x-rar-compressed",
    "rpm": "application/x-redhat-package-manager",
    "sea": "application/x-sea",
    "swf": "application/x-shockwave-flash",
    "sit": "application/x-stuffit",
    "tcl": "application/x-tcl",
    "tk": "application/x-tcl",
    "der": "application/x-x509-ca-cert",
    "crt": "application/x-x509-ca-cert",
    "pem": "application/x-x509-ca-cert",
    "xpi": "application/x-xpinstall",
    "xhtml": "application/xhtml+xml",
    "xspf": "application/xspf+xml",
    "zip": "application/zip",
    "bin": "application/octet-stream",
    "dll": "application/octet-stream",
    "exe": "application/octet-stream",
    "deb": "application/octet-stream",
    "dmg": "application/octet-stream",
    "iso": "application/octet-stream",
    "img": "application/octet-stream",
    "msi": "application/octet-stream",
    "msm": "application/octet-stream",
    "msp": "application/octet-stream",
    "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    "swf": "application/x-shockwave-flash",
    "sit": "application/x-stuffit",
    "tcl": "application/x-tcl",
    "tk": "application/x-tcl",
    "der": "application/x-x509-ca-cert",
    "crt": "application/x-x509-ca-cert",
    "pem": "application/x-x509-ca-cert",
    "xpi": "application/x-xpinstall",
    "xhtml": "application/xhtml+xml",
    "xspf": "application/xspf+xml",
    "zip": "application/zip",
    "bin": "application/octet-stream",
    "dll": "application/octet-stream",
    "exe": "application/octet-stream",
    "deb": "application/octet-stream",
    "dmg": "application/octet-stream",
    "iso": "application/octet-stream",
    "img": "application/octet-stream",
    "msi": "application/octet-stream",
    "msm": "application/octet-stream",
    "msp": "application/octet-stream",
    "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation"
};

module.exports.mime = db;
