Skip to content

Commit 4b3e4f9

Browse files
committed
Refactor of MIMEType
1 parent e0a117f commit 4b3e4f9

File tree

2 files changed

+84
-82
lines changed

2 files changed

+84
-82
lines changed

adafruit_httpserver/mime_type.py

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,87 @@ class MIMEType:
33
From https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
44
"""
55

6-
TEXT_PLAIN = "text/plain"
6+
AAC = "audio/aac"
7+
ABW = "application/x-abiword"
8+
ARC = "application/x-freearc"
9+
AVI = "video/x-msvideo"
10+
AZW = "application/vnd.amazon.ebook"
11+
BIN = "application/octet-stream"
12+
BMP = "image/bmp"
13+
BZ = "application/x-bzip"
14+
BZ2 = "application/x-bzip2"
15+
CSH = "application/x-csh"
16+
CSS = "text/css"
17+
CSV = "text/csv"
18+
DOC = "application/msword"
19+
DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
20+
EOT = "application/vnd.ms-fontobject"
21+
EPUB = "application/epub+zip"
22+
GZ = "application/gzip"
23+
GIF = "image/gif"
24+
HTML = "text/html"
25+
HTM = "text/html"
26+
ICO = "image/vnd.microsoft.icon"
27+
ICS = "text/calendar"
28+
JAR = "application/java-archive"
29+
JPEG = "image/jpeg"
30+
JPG = "image/jpeg"
31+
JS = "text/javascript"
32+
JSON = "application/json"
33+
JSONLD = "application/ld+json"
34+
MID = "audio/midi"
35+
MIDI = "audio/midi"
36+
MJS = "text/javascript"
37+
MP3 = "audio/mpeg"
38+
CDA = "application/x-cdf"
39+
MP4 = "video/mp4"
40+
MPEG = "video/mpeg"
41+
MPKG = "application/vnd.apple.installer+xml"
42+
ODP = "application/vnd.oasis.opendocument.presentation"
43+
ODS = "application/vnd.oasis.opendocument.spreadsheet"
44+
ODT = "application/vnd.oasis.opendocument.text"
45+
OGA = "audio/ogg"
46+
OGV = "video/ogg"
47+
OGX = "application/ogg"
48+
OPUS = "audio/opus"
49+
OTF = "font/otf"
50+
PNG = "image/png"
51+
PDF = "application/pdf"
52+
PHP = "application/x-httpd-php"
53+
PPT = "application/vnd.ms-powerpoint"
54+
PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
55+
RAR = "application/vnd.rar"
56+
RTF = "application/rtf"
57+
SH = "application/x-sh"
58+
SVG = "image/svg+xml"
59+
SWF = "application/x-shockwave-flash"
60+
TAR = "application/x-tar"
61+
TIFF = "image/tiff"
62+
TIF = "image/tiff"
63+
TS = "video/mp2t"
64+
TTF = "font/ttf"
65+
TXT = "text/plain"
66+
VSD = "application/vnd.visio"
67+
WAV = "audio/wav"
68+
WEBA = "audio/webm"
69+
WEBM = "video/webm"
70+
WEBP = "image/webp"
71+
WOFF = "font/woff"
72+
WOFF2 = "font/woff2"
73+
XHTML = "application/xhtml+xml"
74+
XLS = "application/vnd.ms-excel"
75+
XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
76+
XML = "application/xml"
77+
XUL = "application/vnd.mozilla.xul+xml"
78+
ZIP = "application/zip"
79+
_7Z = "application/x-7z-compressed"
780

8-
_MIME_TYPES = {
9-
"aac": "audio/aac",
10-
"abw": "application/x-abiword",
11-
"arc": "application/x-freearc",
12-
"avi": "video/x-msvideo",
13-
"azw": "application/vnd.amazon.ebook",
14-
"bin": "application/octet-stream",
15-
"bmp": "image/bmp",
16-
"bz": "application/x-bzip",
17-
"bz2": "application/x-bzip2",
18-
"csh": "application/x-csh",
19-
"css": "text/css",
20-
"csv": "text/csv",
21-
"doc": "application/msword",
22-
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
23-
"eot": "application/vnd.ms-fontobject",
24-
"epub": "application/epub+zip",
25-
"gz": "application/gzip",
26-
"gif": "image/gif",
27-
"html": "text/html",
28-
"htm": "text/html",
29-
"ico": "image/vnd.microsoft.icon",
30-
"ics": "text/calendar",
31-
"jar": "application/java-archive",
32-
"jpeg .jpg": "image/jpeg",
33-
"js": "text/javascript",
34-
"json": "application/json",
35-
"jsonld": "application/ld+json",
36-
"mid": "audio/midi",
37-
"midi": "audio/midi",
38-
"mjs": "text/javascript",
39-
"mp3": "audio/mpeg",
40-
"cda": "application/x-cdf",
41-
"mp4": "video/mp4",
42-
"mpeg": "video/mpeg",
43-
"mpkg": "application/vnd.apple.installer+xml",
44-
"odp": "application/vnd.oasis.opendocument.presentation",
45-
"ods": "application/vnd.oasis.opendocument.spreadsheet",
46-
"odt": "application/vnd.oasis.opendocument.text",
47-
"oga": "audio/ogg",
48-
"ogv": "video/ogg",
49-
"ogx": "application/ogg",
50-
"opus": "audio/opus",
51-
"otf": "font/otf",
52-
"png": "image/png",
53-
"pdf": "application/pdf",
54-
"php": "application/x-httpd-php",
55-
"ppt": "application/vnd.ms-powerpoint",
56-
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
57-
"rar": "application/vnd.rar",
58-
"rtf": "application/rtf",
59-
"sh": "application/x-sh",
60-
"svg": "image/svg+xml",
61-
"swf": "application/x-shockwave-flash",
62-
"tar": "application/x-tar",
63-
"tiff": "image/tiff",
64-
"tif": "image/tiff",
65-
"ts": "video/mp2t",
66-
"ttf": "font/ttf",
67-
"txt": TEXT_PLAIN,
68-
"vsd": "application/vnd.visio",
69-
"wav": "audio/wav",
70-
"weba": "audio/webm",
71-
"webm": "video/webm",
72-
"webp": "image/webp",
73-
"woff": "font/woff",
74-
"woff2": "font/woff2",
75-
"xhtml": "application/xhtml+xml",
76-
"xls": "application/vnd.ms-excel",
77-
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
78-
"xml": "application/xml",
79-
"xul": "application/vnd.mozilla.xul+xml",
80-
"zip": "application/zip",
81-
"7z": "application/x-7z-compressed",
82-
}
8381

8482
@staticmethod
85-
def mime_type(filename):
83+
def from_file_name(filename):
8684
"""Return the mime type for the given filename. If not known, return "text/plain"."""
87-
return MIMEType._MIME_TYPES.get(filename.split(".")[-1], MIMEType.TEXT_PLAIN)
85+
attr_name = filename.split(".")[-1].upper()
86+
87+
if attr_name[0].isdigit(): attr_name = "_" + attr_name
88+
89+
return getattr(MIMEType, attr_name, MIMEType.TXT)

adafruit_httpserver/response.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(
3030
status: HTTPStatus = OK_200,
3131
body: str = "",
3232
headers: Dict[str, str] = None,
33-
content_type: str = MIMEType.TEXT_PLAIN,
33+
content_type: str = MIMEType.TXT,
3434
filename: Optional[str] = None,
3535
root_path: str = "",
3636
http_version: str = "HTTP/1.1"
@@ -53,7 +53,7 @@ def __init__(
5353
def _construct_response_bytes(
5454
http_version: str = "HTTP/1.1",
5555
status: HTTPStatus = OK_200,
56-
content_type: str = MIMEType.TEXT_PLAIN,
56+
content_type: str = MIMEType.TXT,
5757
content_length: Union[int, None] = None,
5858
headers: Dict[str, str] = None,
5959
body: str = "",
@@ -93,7 +93,7 @@ def send(self, conn: Union[SocketPool.Socket, socket.socket]) -> None:
9393
self._send_response(
9494
conn,
9595
status = NOT_FOUND_404,
96-
content_type = MIMEType.TEXT_PLAIN,
96+
content_type = MIMEType.TXT,
9797
body = f"{NOT_FOUND_404} {self.filename}",
9898
)
9999
else:
@@ -134,7 +134,7 @@ def _send_file_response(
134134
conn,
135135
self._construct_response_bytes(
136136
status = self.status,
137-
content_type = MIMEType.mime_type(filename),
137+
content_type = MIMEType.from_file_name(filename),
138138
content_length = file_length
139139
),
140140
)

0 commit comments

Comments
 (0)