diff MoinMoin/action/AttachFile.py @ 801:1f8976e01c3a

fix wrong import, make more use of MimeType class
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 19 May 2006 20:18:45 +0200
parents 28de47f4ff1a
children 7b2e550c9660
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Fri May 19 19:17:12 2006 +0200
+++ b/MoinMoin/action/AttachFile.py	Fri May 19 20:18:45 2006 +0200
@@ -633,16 +633,14 @@
     import shutil
 
     filename, fpath = _access_file(pagename, request)
-    if not filename: return # error msg already sent in _access_file
+    if not filename:
+        return # error msg already sent in _access_file
 
-    # get mimetype
-    type, enc = wikiutil.guess_type(filename)
-    if not type:
-        type = "application/octet-stream"
+    mt = wikiutil.MimeType(filename=filename)
 
     # send header
     request.http_headers([
-        "Content-Type: %s" % type,
+        "Content-Type: %s" % mt.content_type(),
         "Content-Length: %d" % os.path.getsize(fpath),
         # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
         # There is no solution that is compatible to IE except stripping non-ascii chars
@@ -766,24 +764,23 @@
 
     request.write('<h2>' + _("Attachment '%(filename)s'") % {'filename': filename} + '</h2>')
 
-    type, enc = wikiutil.guess_type(filename)
-    if type:
-        if type[:5] == 'image':
-            timestamp = htdocs_access(request) and "?%s" % time.time() or ''
-            request.write('<img src="%s%s" alt="%s">' % (
-                getAttachUrl(pagename, filename, request, escaped=1), timestamp, wikiutil.escape(filename, 1)))
-            return
-        elif type[:4] == 'text':
-            # TODO: should use formatter here!
-            request.write("<pre>")
-            # Try to decode file contents. It may return junk, but we
-            # don't have enough information on attachments.
-            content = open(fpath, 'r').read()
-            content = wikiutil.decodeUnknownInput(content)
-            content = wikiutil.escape(content)
-            request.write(content)
-            request.write("</pre>")
-            return
+    mt = wikiutil.MimeType(filename=filename)
+    if mt.major == 'image':
+        timestamp = htdocs_access(request) and "?%s" % time.time() or ''
+        request.write('<img src="%s%s" alt="%s">' % (
+            getAttachUrl(pagename, filename, request, escaped=1), timestamp, wikiutil.escape(filename, 1)))
+        return
+    elif mt.major == 'text':
+        # TODO: should use formatter here!
+        request.write("<pre>")
+        # Try to decode file contents. It may return junk, but we
+        # don't have enough information on attachments.
+        content = open(fpath, 'r').read()
+        content = wikiutil.decodeUnknownInput(content)
+        content = wikiutil.escape(content)
+        request.write(content)
+        request.write("</pre>")
+        return
 
     package = packages.ZipPackage(request, fpath)
     if package.isPackage():