[Moin-user] Adding mimetype support for raw output

John Marshall John.Marshall at ec.gc.ca
Wed Aug 12 16:58:03 EDT 2009


Hi,

Note: I am assuming that this cannot already be done some other way.

The following 2 diffs allow MM (v1.8.2) to return a raw page with a
specified mimetype. If mimetype is not specified OR the mimetype is
not recognized, the original behavior remains. If mimetype is
specified AND is a recognized mimetype, then it is used to generate
the appropriate "Content-Type" in the response.

For example, an HTTP GET of:

	http://a.b.c/xyz?action=raw&mimetype=text/css

returns the raw page in an HTTP response with:

	Content-Type: text/css; charset=...

My use case: I wanted to provide user specified CSS via a wiki page.
This change makes it possible in a simple and clear way. The suggested
approach on the moinmo.in wiki does not (no longer?) work as advertised
(i.e., text/plain is returned as the mimetype).

Comments welcome.

Thanks,
John

----------
$diff -u action/__init__.py action/__init__.py.orig
--- action/__init__.py  2009-08-12 16:27:40.000000000 -0400
+++ action/__init__.py.orig     2009-08-12 16:35:54.000000000 -0400
@@ -25,8 +25,6 @@
     @license: GNU GPL, see COPYING for details.
 """

-import mimetypes
-
 from MoinMoin.util import pysupport
 from MoinMoin import config, wikiutil
 from MoinMoin.Page import Page
@@ -235,12 +233,7 @@
         Page(request, pagename).send_page()
     else:
         rev = request.rev or 0
-        mimetype = request.args.get("mimetype", [ None ])[0]
-        if mimetype:
-            if not mimetypes.guess_extension(mimetype):
-                # unrecognized
-                mimetype = None
-        Page(request, pagename, rev=rev).send_raw(mimetype=mimetype)
+        Page(request, pagename, rev=rev).send_raw()

 def do_show(pagename, request, content_only=0, count_hit=1, cacheable=1, print_mode=0):
     """ show a page, either current revision or the revision given by rev form value.
----------
$diff -u Page.py Page.py.orig
--- Page.py     2009-08-12 16:24:29.000000000 -0400
+++ Page.py.orig        2009-08-12 13:39:48.000000000 -0400
@@ -959,17 +959,14 @@
         pi['acl'] = security.AccessControlList(request.cfg, acl)
         return pi

-    def send_raw(self, content_disposition=None,mimetype=None):
+    def send_raw(self, content_disposition=None):
         """ Output the raw page data (action=raw).
             With no content_disposition, the browser usually just displays the
             data on the screen, with content_disposition='attachment', it will
             offer a dialogue to save it to disk (used by Save action).
         """
         request = self.request
-        if mimetype:
-            request.setHttpHeader("Content-type: %s; charset=%s" % (mimetype, config.charset))
-        else:
-            request.setHttpHeader("Content-type: text/plain; charset=%s" % config.charset)
+        request.setHttpHeader("Content-type: text/plain; charset=%s" % config.charset)
         if self.exists():
             # use the correct last-modified value from the on-disk file
             # to ensure cacheability where supported. Because we are sending





More information about the Moin-user mailing list