changeset 16:22e4dacd1d37

arnica_slides: viewing thumbnails and webnails from cache dir
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 22 May 2008 18:20:12 +0200
parents 46267a5a1451
children cb6ad4cfdc5e
files data/plugin/action/arnica_slides.py
diffstat 1 files changed, 47 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/action/arnica_slides.py	Thu May 22 18:17:31 2008 +0200
+++ b/data/plugin/action/arnica_slides.py	Thu May 22 18:20:12 2008 +0200
@@ -10,11 +10,11 @@
 """
 Dependencies = []
 import os
-from MoinMoin import  wikiutil
+from MoinMoin import  caching, config, wikiutil
 from MoinMoin.Page import Page
 from MoinMoin.action import AttachFile
 from MoinMoin.formatter.text_html import Formatter
-from MoinMoin.util import filesys
+from MoinMoin.util import filesys, timefuncs
 from MoinMoin.action import ActionBase
 
 try:
@@ -346,7 +346,10 @@
         _ = self.request.getText
         request = self.request
         pagename = self.pagename
-
+        page = Page(request, pagename)
+        arena_dir = caching.get_arena_dir(request, page, 'item')
+        
+        
         if not Image:
             msg = _('The action %(action)s needs python imaging library (PIL) installed' % {'action': action_name})
             request.theme.add_msg(msg, "error")
@@ -356,7 +359,8 @@
         request.formatter = Formatter(request)
         attachment_path = AttachFile.getAttachDir(request, self.pagename)
         command = request.form.get('do', ['none'])[0]
-
+        target = request.form.get('target', [None])[0]
+        
         if command == 'VS':
             web = {}
             images = request.form.get('target', [''])[0]
@@ -415,8 +419,11 @@
                 thumbfile = "tmp.thumbnail_%(file)s.jpg"  % {"file": filename}
                 webnail = "tmp.webnail_%(file)s.jpg"  % {"file": filename}
 
-            thumbf = os.path.join(attachment_path, thumbfile)
-            webf = os.path.join(attachment_path, webnail)
+            thumbf = os.path.join(arena_dir, thumbfile)
+            webf = os.path.join(arena_dir, webnail)
+            cache_web = caching.CacheEntry(request, page, webnail, scope='item', use_pickle=True)        
+            cache_thumb= caching.CacheEntry(request, page, thumbfile, scope='item', use_pickle=True) 
+
             infile = os.path.join(attachment_path, target)
 
             msg = None
@@ -425,6 +432,9 @@
                 if os.path.exists(infile + '.bak'):
                     os.unlink("%(file)s.bak" % {"file": infile})
                 filesys.rename(infile, "%(file)s.bak" % {"file": infile})
+                cache_web.remove()
+                cache_thumb.remove()
+                
                 msg = _('%(target)s deleted, backup in place' % {
                       'target':target})
             # rotate left
@@ -436,6 +446,37 @@
             elif command == 'RR':
                 self.image_rotate(infile, webf, thumbf, 270, img_type)
                 msg = _('%(target)s rotated to right 90 degrees' % {'target':target})
+                
+            elif command == 'view':
+                if not target:
+                    return 
+                if not request.user.may.read(pagename):
+                    return _('You are not allowed to view attachments of this page.')
+                
+                
+                fpath = os.path.join(arena_dir, target)
+                if not os.path.exists(fpath):
+                    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'))
             else:
                 # fallback
                 msg = _('action not implemented: %s') % (command, )