From: Bernardo Innocenti Date: Sat, 26 Jan 2008 22:06:37 +0000 (+1100) Subject: Add automatic mimetype detection. X-Git-Tag: v3.0~34 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=99acd9376fc3cdaf9dfe9218c78aede47c54968c;p=geekigeeki.git Add automatic mimetype detection. --- diff --git a/geekigeeki.py b/geekigeeki.py index b34f152..764f366 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -28,8 +28,9 @@ from os import path, environ # Regular expression defining a WikiWord # (but this definition is also assumed in other places) -file_re = re.compile(r"^\b([A-Za-z0-9_\.\-/]+)\b$") word_re = re.compile(r"^\b((([A-Z][a-z]+){2,}/)*([A-Z][a-z]+){2,})\b$") +# FIXME: we accept stuff like foo/../bar and we shouldn't +file_re = re.compile(r"^\b([A-Za-z0-9_\-][A-Za-z0-9_\.\-/]*)\b$") img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg)$", re.IGNORECASE) url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$") @@ -755,11 +756,10 @@ try: if file_re.match(query): if word_re.match(query): Page(query).send_page() - elif img_re.match(query): - #FIXME: use correct mime type - Page(query).send_raw(mimetype='image/jpeg') else: - Page(query).send_raw(mimetype='application/octet-stream') + from mimetypes import MimeTypes + type, encoding = MimeTypes().guess_type(query) + Page(query).send_raw(mimetype=type) else: print "Status: 404 Not Found" send_title(None, msg_text='Can\'t work out query: ' + query)