changeset 65:4a0ef5c802c8

arnica_slides: factored out send_cache_file
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Wed, 04 Jun 2008 20:14:20 +0200
parents ea6139631185
children 7d866c4324f4
files data/plugin/action/arnica_slides.py
diffstat 1 files changed, 35 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/action/arnica_slides.py	Wed Jun 04 19:51:10 2008 +0200
+++ b/data/plugin/action/arnica_slides.py	Wed Jun 04 20:14:20 2008 +0200
@@ -29,6 +29,40 @@
 
 action_name = __name__.split('.')[-1]
 
+def send_cache_file(request, pagename, arena_dir, target):
+    msg = None
+    if not request.user.may.read(pagename):
+        msg = _('You are not allowed to view attachments of this page.')
+
+    fpath = os.path.join(arena_dir, target)
+    if not target or not os.path.exists(fpath) or not wikiutil.isPicture(fpath):
+        msg = _("Attachment '%(filename)s' does not exist!") % {"filename": target}
+
+    if msg:
+        request.theme.add_msg(msg, "error")
+        Page(request, pagename).send_page()
+        return
+
+    timestamp = timefuncs.formathttpdate(int(os.path.getmtime(fpath)))
+    if request.if_modified_since == timestamp:
+        request.emit_http_headers(["Status: 304 Not modified"])
+    else:
+        mt = wikiutil.MimeType(filename=target)
+        content_type = mt.content_type()
+        mime_type = mt.mime_type()
+
+        content_dispo = 'inline'
+        filename_enc = target.encode(config.charset)
+
+        request.emit_http_headers([
+            'Content-Type: %s' % content_type,
+            'Last-Modified: %s' % timestamp,
+            'Content-Length: %d' % os.path.getsize(fpath),
+            'Content-Disposition: %s; filename="%s"' % (content_dispo, filename_enc),
+                                 ])
+        # send data
+        request.send_file(open(fpath, 'rb'))
+
 def image_rotate(file_name, webf, thumbf, angle, img_type):
     """ rotates the image and dependent images
 
@@ -392,38 +426,7 @@
 
         # view sends a cached attachment (thumbnail or webnail) to the browser
         elif command == 'view':
-            if not request.user.may.read(pagename):
-                msg = _('You are not allowed to view attachments of this page.')
-
-            fpath = os.path.join(arena_dir, target)
-            if not target or not os.path.exists(fpath) or not wikiutil.isPicture(fpath):
-                msg = _("Attachment '%(filename)s' does not exist!") % {"filename": target}
-
-            if msg:
-                request.theme.add_msg(msg, "error")
-                self.page.send_page()
-                return
-
-            timestamp = timefuncs.formathttpdate(int(os.path.getmtime(fpath)))
-            if request.if_modified_since == timestamp:
-                request.emit_http_headers(["Status: 304 Not modified"])
-            else:
-                mt = wikiutil.MimeType(filename=target)
-                content_type = mt.content_type()
-                mime_type = mt.mime_type()
-
-                content_dispo = 'inline'
-                filename_enc = target.encode(config.charset)
-
-                request.emit_http_headers([
-                    'Content-Type: %s' % content_type,
-                    'Last-Modified: %s' % timestamp,
-                    'Content-Length: %d' % os.path.getsize(fpath),
-                    'Content-Disposition: %s; filename="%s"' % (content_dispo, filename_enc),
-                     ])
-
-                # send data
-                request.send_file(open(fpath, 'rb'))
+            send_cache_file(request, pagename, arena_dir, target)
 
         elif request.user.may.delete(pagename) and request.request_method == 'POST':
         # only users which are allowed to delete should use this tool