[Moin-user] Adding mimetype support for raw output

John Marshall John.Marshall at ec.gc.ca
Fri Aug 14 17:05:45 EDT 2009


John Marshall wrote:
> 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.
>   
I have corrected the patch order (diff <orig> <new>) below.

John

-----

$ diff -u Page.py.orig Page.py
--- Page.py.orig        2009-08-12 13:39:48.000000000 -0400
+++ Page.py     2009-08-14 17:00:58.000000000 -0400
@@ -959,14 +959,17 @@
         pi['acl'] = security.AccessControlList(request.cfg, acl)
         return pi

-    def send_raw(self, content_disposition=None):
+    def send_raw(self, content_disposition=None, mimetype=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
-        request.setHttpHeader("Content-type: text/plain; charset=%s" % config.charset)
+        if mimetype:
+            request.setHttpHeader("Content-type: %s; charset=%s" % (mimetype, config.charset))
+        else:
+            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

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

+import mimetypes
+
 from MoinMoin.util import pysupport
 from MoinMoin import config, wikiutil
 from MoinMoin.Page import Page
@@ -233,7 +235,12 @@
         Page(request, pagename).send_page()
     else:
         rev = request.rev or 0
-        Page(request, pagename, rev=rev).send_raw()
+        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)

 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









More information about the Moin-user mailing list