changeset 4959:18474c316dd7

mimetype support for Page.send_raw() and "raw" action (thanks to John Marshall for the patch) Usage: ...?action=raw&mimetype=what/ever The mimetype given is minimally checked using a simple regex: o type is usually quite short o subtype can be quite long to support "tree"s (looking at RFC2045 and /etc/mime.types)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 25 Aug 2009 16:39:07 +0200
parents 897840f96acc
children 2a024d3e418f 3e9398438b56
files MoinMoin/Page.py MoinMoin/action/__init__.py
diffstat 2 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/Page.py	Sat Aug 22 15:11:20 2009 +0200
+++ b/MoinMoin/Page.py	Tue Aug 25 16:39:07 2009 +0200
@@ -961,14 +961,15 @@
         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).
+            Supplied mimetype overrides default text/plain.
         """
         request = self.request
-        request.mimetype = 'text/plain'
+        request.mimetype = mimetype or 'text/plain'
         if self.exists():
             # use the correct last-modified value from the on-disk file
             # to ensure cacheability where supported. Because we are sending
--- a/MoinMoin/action/__init__.py	Sat Aug 22 15:11:20 2009 +0200
+++ b/MoinMoin/action/__init__.py	Tue Aug 25 16:39:07 2009 +0200
@@ -27,6 +27,8 @@
     @license: GNU GPL, see COPYING for details.
 """
 
+import re
+
 from MoinMoin.util import pysupport
 from MoinMoin import config, wikiutil
 from MoinMoin.Page import Page
@@ -228,13 +230,18 @@
 
 # Builtin Actions ------------------------------------------------------------
 
+MIMETYPE_CRE = re.compile('[a-zA-Z0-9.+\-]{1,100}/[a-zA-Z0-9.+\-]{1,100}')
+
 def do_raw(pagename, request):
     """ send raw content of a page (e.g. wiki markup) """
     if not request.user.may.read(pagename):
         Page(request, pagename).send_page()
     else:
         rev = request.rev or 0
-        Page(request, pagename, rev=rev).send_raw()
+        mimetype = request.values.get('mimetype', None)
+        if mimetype and not MIMETYPE_CRE.match(mimetype):
+            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, mimetype=u'text/html'):
     """ show a page, either current revision or the revision given by "rev=" value.