-
Notifications
You must be signed in to change notification settings - Fork 168
/
media_type.ts
81 lines (77 loc) · 2.3 KB
/
media_type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { splitBy, trimPrefix } from "../shared/util.ts";
// MIME types for web
const mimeTypes: Record<string, string[]> = {
// application
"application/javascript": ["js", "mjs"],
"application/typescript": ["ts", "mts"],
"application/wasm": ["wasm"],
"application/json": ["json", "jsonc", "map"],
"application/json5": ["json5"],
"application/pdf": ["pdf"],
"application/xml": ["xml", "plist", "tmLanguage", "tmTheme"],
"application/zip": ["zip"],
"application/gzip": ["gz"],
"application/tar": ["tar"],
"application/tar+gzip": ["tar.gz", "tgz"],
// text
"text/html": ["html", "htm"],
"text/markdown": ["md", "markdown"],
"text/mdx": ["mdx"],
"text/jsx": ["jsx"],
"text/tsx": ["tsx"],
"text/vue": ["vue"],
"text/svelte": ["svelte"],
"text/css": ["css"],
"text/postcss": ["pcss", "postcss"],
"text/less": ["less"],
"text/sass": ["sass", "scss"],
"text/stylus": ["stylus", "styl"],
"text/csv": ["csv"],
"text/yaml": ["yaml", "yml"],
"text/plain": ["txt", "glsl"],
// font
"font/ttf": ["ttf"],
"font/otf": ["otf"],
"font/woff": ["woff"],
"font/woff2": ["woff2"],
"font/collection": ["ttc"],
// image
"image/jpeg": ["jpg", "jpeg"],
"image/png": ["png"],
"image/apng": ["apng"],
"image/gif": ["gif"],
"image/webp": ["webp"],
"image/avif": ["avif"],
"image/svg+xml": ["svg", "svgz"],
"image/x-icon": ["ico"],
// audio
"audio/mp4": ["m4a"],
"audio/mpeg": ["mp3", "m3a"],
"audio/ogg": ["ogg", "oga"],
"audio/wav": ["wav"],
"audio/webm": ["weba"],
// video
"video/mp4": ["mp4", "m4v"],
"video/ogg": ["ogv"],
"video/webm": ["webm"],
"video/x-matroska": ["mkv"],
// shader
"x-shader/x-fragment": ["frag"],
"x-shader/x-vertex": ["vert"],
};
const typesMap = Object.entries(mimeTypes).reduce((map, [contentType, exts]) => {
exts.forEach((ext) => map.set(ext, contentType));
return map;
}, new Map<string, string>());
/** register a new type */
export function registerType(ext: string, contentType: string) {
typesMap.set(trimPrefix(ext, "."), contentType);
}
/** get the content type by file name */
export function getContentType(filename: string): string {
let [prefix, ext] = splitBy(filename, ".", true);
if (ext === "gz" && prefix.endsWith(".tar")) {
ext = "tar.gz";
}
return typesMap.get(ext) ?? "application/octet-stream";
}