|
| 1 | +import String from "string" |
| 2 | +import Array from "array" |
| 3 | +import Option from "option" |
| 4 | +import Map from "map" |
| 5 | +import {lastIndexOf, reverse} from "./stringutil" |
| 6 | + |
| 7 | +let default_mt = "application/octet-stream" |
| 8 | +let mut mediatypes = Map.make() |
| 9 | + |
| 10 | +// Text formats |
| 11 | +Map.set("txt", "text/plain", mediatypes) |
| 12 | +Map.set("md", "text/plain", mediatypes) |
| 13 | +Map.set("mdown", "text/plain", mediatypes) |
| 14 | +Map.set("htm", "text/html", mediatypes) |
| 15 | +Map.set("html", "text/html", mediatypes) |
| 16 | +Map.set("xhtml", "application/xhtml+xml", mediatypes) |
| 17 | +Map.set("xml", "application/xml", mediatypes) |
| 18 | +Map.set("css", "text/css", mediatypes) |
| 19 | +Map.set("ics", "text/calendar", mediatypes) |
| 20 | + |
| 21 | +// Serialization formats |
| 22 | +Map.set("json", "application/json", mediatypes) |
| 23 | +Map.set("jsonld", "application/ld+json", mediatypes) |
| 24 | +Map.set("toml", "application/toml", mediatypes) |
| 25 | +Map.set("yaml", "application/yaml", mediatypes) |
| 26 | + |
| 27 | +// Applications |
| 28 | +// According to MSDN, prefered is text/javascript |
| 29 | +Map.set("js", "text/javascript", mediatypes) |
| 30 | +Map.set("mjs", "text/javascript", mediatypes) |
| 31 | +Map.set("wasm", "application/wasm", mediatypes) |
| 32 | +Map.set("csv", "text/csv", mediatypes) |
| 33 | +Map.set("sh", "application/x-sh", mediatypes) |
| 34 | + |
| 35 | +// Images |
| 36 | +Map.set("apng", "image/apng", mediatypes) |
| 37 | +Map.set("avif", "image/avif", mediatypes) |
| 38 | +Map.set("png", "image/png", mediatypes) |
| 39 | +Map.set("png", "image/png", mediatypes) |
| 40 | +Map.set("jpg", "image/jpeg", mediatypes) |
| 41 | +Map.set("jpeg", "image/jpeg", mediatypes) |
| 42 | +Map.set("pjpeg", "image/jpeg", mediatypes) |
| 43 | +Map.set("pjp", "image/jpeg", mediatypes) |
| 44 | +Map.set("jfif", "image/jpeg", mediatypes) |
| 45 | +Map.set("gif", "image/gif", mediatypes) |
| 46 | +Map.set("tif", "image/tiff", mediatypes) |
| 47 | +Map.set("tiff", "image/tiff", mediatypes) |
| 48 | +Map.set("webp", "image/webp", mediatypes) |
| 49 | +Map.set("svg", "image/svg+xml", mediatypes) |
| 50 | +Map.set("bmp", "image/bmp", mediatypes) |
| 51 | +Map.set("ico", "image/vnd.microsoft.icon", mediatypes) |
| 52 | + |
| 53 | +// Audio/Video |
| 54 | +Map.set("aac", "audio/aac", mediatypes) |
| 55 | +Map.set("avi", "video/x-msvideo", mediatypes) |
| 56 | +Map.set("wav", "audio/wave", mediatypes) |
| 57 | +Map.set("webm", "video/webm", mediatypes) |
| 58 | +Map.set("mp3", "audio/mpeg", mediatypes) |
| 59 | +Map.set("mp4", "video/mp4", mediatypes) |
| 60 | +Map.set("mpeg", "video/mpeg", mediatypes) |
| 61 | +Map.set("oga", "audio/ogg", mediatypes) |
| 62 | +Map.set("ogv", "video/ogg", mediatypes) |
| 63 | +Map.set("ogx", "application/ogg", mediatypes) |
| 64 | +Map.set("ts", "video/mp2t", mediatypes) |
| 65 | + |
| 66 | +// Compressed |
| 67 | +Map.set("bz2", "application/x-bzip2", mediatypes) |
| 68 | +Map.set("tbz", "application/x-bzip2", mediatypes) |
| 69 | +Map.set("tbz2", "application/x-bzip2", mediatypes) |
| 70 | +Map.set("gz", "application/gzip", mediatypes) |
| 71 | +Map.set("rar", "application/vnd.rar", mediatypes) |
| 72 | +Map.set("tar", "text/x-tar", mediatypes) |
| 73 | +Map.set("tgz", "application/gzip", mediatypes) |
| 74 | +Map.set("jar", "application/java-archive", mediatypes) |
| 75 | +Map.set("mpkg", "application/vnd.apple.installer+xml", mediatypes) |
| 76 | +Map.set("zip", "application/zip", mediatypes) |
| 77 | +Map.set("7z", "application/x-7z-compressed", mediatypes) |
| 78 | + |
| 79 | +// Binary |
| 80 | +Map.set("azw", "application/vnd.amazon.ebook", mediatypes) |
| 81 | +Map.set("bin", "application/octet-stream", mediatypes) |
| 82 | +Map.set("doc", "application/msword", mediatypes) |
| 83 | +Map.set("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", mediatypes) |
| 84 | +Map.set("epub", "application/epub+zip", mediatypes) |
| 85 | +Map.set("odp", "application/vnd.oasis.opendocument.presentation", mediatypes) |
| 86 | +Map.set("ods", "application/vnd.oasis.opendocument.spreadsheet", mediatypes) |
| 87 | +Map.set("odt", "application/vnd.oasis.opendocument.text", mediatypes) |
| 88 | +Map.set("pdf", "application/pdf", mediatypes) |
| 89 | +Map.set("ppt", "application/vnd.ms-powerpoint", mediatypes) |
| 90 | +Map.set("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation", mediatypes) |
| 91 | +Map.set("rtf", "application/rtf", mediatypes) |
| 92 | +Map.set("vsd", "application/vnd.visio", mediatypes) |
| 93 | +Map.set("xls", "application/vnd.ms-excel", mediatypes) |
| 94 | +Map.set("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", mediatypes) |
| 95 | + |
| 96 | +// Fonts |
| 97 | +Map.set("eot", "application/vnd.ms-fontobject", mediatypes) |
| 98 | +Map.set("otf", "font/otf", mediatypes) |
| 99 | +Map.set("ttf", "font/ttf", mediatypes) |
| 100 | +Map.set("woff", "font/woff", mediatypes) |
| 101 | +Map.set("woff2", "font/woff2", mediatypes) |
| 102 | + |
| 103 | +// Guess the media type of this file |
| 104 | +// @param filename: The name of the file |
| 105 | +export let guess = (filename: String) => { |
| 106 | + match (lastIndexOf(".", filename)) { |
| 107 | + Some(extOffset) => { |
| 108 | + let ext = String.slice(extOffset + 1, String.length(filename), filename) |
| 109 | + Option.unwrapWithDefault(default_mt, Map.get(ext, mediatypes)) |
| 110 | + }, |
| 111 | + None => default_mt |
| 112 | + } |
| 113 | +} |
0 commit comments