Simplify mimetype handling
authorBernie Innocenti <bernie@codewiz.org>
Wed, 8 Apr 2009 21:21:11 +0000 (23:21 +0200)
committerBernie Innocenti <bernie@codewiz.org>
Wed, 8 Apr 2009 21:21:11 +0000 (23:21 +0200)
geekigeeki.py

index f3ce1dcb3268d452e29c997c5a698040a15b58cf..cc0373621ab6e4c11da560c253d9383cc40a9948 100755 (executable)
@@ -742,15 +742,11 @@ class Page:
             print "</div>"
         send_footer()
 
-    def send_raw(self, mimetype='text/plain'):
-        if self.can_read():
-            body = self.get_raw_body()
-            emit_header(mimetype)
-            print body
-        else:
+    def send_raw(self, mimetype='text/plain', args=[]):
+        if not self.can_read():
             send_title(None, msg_text='Read access denied by ACLs', msg_type='notice')
+            return
 
-    def send_image(self, mimetype, args=[]):
         if 'maxwidth' in args:
             import subprocess
             emit_header(mimetype)
@@ -758,7 +754,9 @@ class Page:
             subprocess.check_call(['gm', 'convert', self._filename(),
                 '-scale', args['maxwidth'].value + ' >', '-'])
         else:
-            self.send_raw(mimetype)
+            body = self.get_raw_body()
+            emit_header(mimetype)
+            print body
 
     def _write_file(self, data):
         tmp_filename = self._tmp_filename()
@@ -809,18 +807,13 @@ def main():
     else:
         query = query_string()
         if file_re.match(query):
-            if word_re.match(query):
-                Page(query).format()
+            # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
+            from mimetypes import MimeTypes
+            mimetype, encoding = MimeTypes().guess_type(query)
+            if mimetype:
+                Page(query).send_raw(mimetype=mimetype, args=form)
             else:
-                from mimetypes import MimeTypes
-                mimetype, encoding = MimeTypes().guess_type(query)
-                if mimetype:
-                    if mimetype.startswith('image/'):
-                        Page(query).send_image(mimetype=mimetype, args=form)
-                    else:
-                        Page(query).send_raw(mimetype=mimetype)
-                else:
-                    Page(query).format()
+                Page(query).format()
         else:
             send_httperror("403 Forbidden", query)