Add automatic mimetype detection.
authorBernardo Innocenti <bernie@codewiz.org>
Sat, 26 Jan 2008 22:06:37 +0000 (09:06 +1100)
committerBernardo Innocenti <bernie@codewiz.org>
Sat, 26 Jan 2008 22:06:37 +0000 (09:06 +1100)
geekigeeki.py

index b34f152188b0544747471a796258d521f9eb0aba..764f3665de6fff91f4e773fc918cdae5adb65a7a 100755 (executable)
@@ -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)