Debian Codegraph @

src/whitenoise/media_types.py

raw
1 from __future__ import annotations
2
3 import os
4
5
6 class MediaTypes:
7 __slots__ = ("types_map",)
8
9 def __init__(self, *, extra_types: dict[str, str] | None = None) -> None:
10 self.types_map = default_types()
11 if extra_types is not None:
12 self.types_map.update(extra_types)
13
14 def get_type(self, path: str) -> str:
15 name = os.path.basename(path).lower()
16 media_type = self.types_map.get(name)
17 if media_type is not None:
18 return media_type
19 extension = os.path.splitext(name)[1]
20 return self.types_map.get(extension, "application/octet-stream")
21
22
23 def default_types() -> dict[str, str]:
24 """
25 We use our own set of default media types rather than the system-supplied
26 ones. This ensures consistent media type behaviour across varied
27 environments. The defaults are based on those shipped with nginx, with
28 some custom additions.
29
30 (Auto-generated by scripts/generate_default_media_types.py)
31 """
32 return {
33 ".3gp": "video/3gpp",
34 ".3gpp": "video/3gpp",
35 ".7z": "application/x-7z-compressed",
36 ".ai": "application/postscript",
37 ".asf": "video/x-ms-asf",
38 ".asx": "video/x-ms-asf",
39 ".atom": "application/atom+xml",
40 ".avi": "video/x-msvideo",
41 ".avif": "image/avif",
42 ".bmp": "image/x-ms-bmp",
43 ".cco": "application/x-cocoa",
44 ".crt": "application/x-x509-ca-cert",
45 ".css": "text/css",
46 ".der": "application/x-x509-ca-cert",
47 ".doc": "application/msword",
48 ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
49 ".ear": "application/java-archive",
50 ".eot": "application/vnd.ms-fontobject",
51 ".eps": "application/postscript",
52 ".flv": "video/x-flv",
53 ".gif": "image/gif",
54 ".hqx": "application/mac-binhex40",
55 ".htc": "text/x-component",
56 ".htm": "text/html",
57 ".html": "text/html",
58 ".ico": "image/x-icon",
59 ".jad": "text/vnd.sun.j2me.app-descriptor",
60 ".jar": "application/java-archive",
61 ".jardiff": "application/x-java-archive-diff",
62 ".jng": "image/x-jng",
63 ".jnlp": "application/x-java-jnlp-file",
64 ".jpeg": "image/jpeg",
65 ".jpg": "image/jpeg",
66 ".js": "text/javascript",
67 ".json": "application/json",
68 ".kar": "audio/midi",
69 ".kml": "application/vnd.google-earth.kml+xml",
70 ".kmz": "application/vnd.google-earth.kmz",
71 ".m3u8": "application/vnd.apple.mpegurl",
72 ".m4a": "audio/x-m4a",
73 ".m4v": "video/x-m4v",
74 ".md": "text/markdown",
75 ".mid": "audio/midi",
76 ".midi": "audio/midi",
77 ".mjs": "text/javascript",
78 ".mml": "text/mathml",
79 ".mng": "video/x-mng",
80 ".mov": "video/quicktime",
81 ".mp3": "audio/mpeg",
82 ".mp4": "video/mp4",
83 ".mpeg": "video/mpeg",
84 ".mpg": "video/mpeg",
85 ".odg": "application/vnd.oasis.opendocument.graphics",
86 ".odp": "application/vnd.oasis.opendocument.presentation",
87 ".ods": "application/vnd.oasis.opendocument.spreadsheet",
88 ".odt": "application/vnd.oasis.opendocument.text",
89 ".ogg": "audio/ogg",
90 ".pdb": "application/x-pilot",
91 ".pdf": "application/pdf",
92 ".pem": "application/x-x509-ca-cert",
93 ".pl": "application/x-perl",
94 ".pm": "application/x-perl",
95 ".png": "image/png",
96 ".ppt": "application/vnd.ms-powerpoint",
97 ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
98 ".prc": "application/x-pilot",
99 ".ps": "application/postscript",
100 ".ra": "audio/x-realaudio",
101 ".rar": "application/x-rar-compressed",
102 ".rpm": "application/x-redhat-package-manager",
103 ".rss": "application/rss+xml",
104 ".rtf": "application/rtf",
105 ".run": "application/x-makeself",
106 ".sea": "application/x-sea",
107 ".shtml": "text/html",
108 ".sit": "application/x-stuffit",
109 ".svg": "image/svg+xml",
110 ".svgz": "image/svg+xml",
111 ".swf": "application/x-shockwave-flash",
112 ".tcl": "application/x-tcl",
113 ".tif": "image/tiff",
114 ".tiff": "image/tiff",
115 ".tk": "application/x-tcl",
116 ".ts": "video/mp2t",
117 ".txt": "text/plain",
118 ".war": "application/java-archive",
119 ".wasm": "application/wasm",
120 ".wbmp": "image/vnd.wap.wbmp",
121 ".webm": "video/webm",
122 ".webp": "image/webp",
123 ".wml": "text/vnd.wap.wml",
124 ".wmlc": "application/vnd.wap.wmlc",
125 ".wmv": "video/x-ms-wmv",
126 ".woff": "application/font-woff",
127 ".woff2": "font/woff2",
128 ".xhtml": "application/xhtml+xml",
129 ".xls": "application/vnd.ms-excel",
130 ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
131 ".xml": "text/xml",
132 ".xpi": "application/x-xpinstall",
133 ".xspf": "application/xspf+xml",
134 ".zip": "application/zip",
135 "apple-app-site-association": "application/pkc7-mime",
136 "crossdomain.xml": "text/x-cross-domain-policy",
137 }