Attachment 'Thumbnail_action.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Thumbnail Action
   4     Version 0.2
   5 
   6     ?action=Thumbnail&w=<width>&h=<height>&target=<attachment>
   7     ?action=Thumbnail&h=<height>&target=<attachment>
   8     ?action=Thumbnail&w=<width>&target=<attachment>
   9     ?action=Thumbnail&target=<attachment>
  10 
  11     Displays a thumbnail of an image attachment.
  12     If height or width are missing, the missing dimension is calculated using the image's aspect ratio.
  13     If both height and width are missing, they default to 64x64.
  14 
  15     @copyright: 2007 by Kenneth Bull
  16     @license: GNU GPL, see COPYING for details.
  17 
  18 """
  19 
  20 import os, mimetypes, shutil
  21 from subprocess import Popen, PIPE, call
  22 from MoinMoin import util, wikiutil
  23 from MoinMoin.Page import Page
  24 from MoinMoin.util import filesys, timefuncs#, MoinMoinNoFooter
  25 from MoinMoin.action.AttachFile import _access_file, getAttachDir, error_msg
  26 
  27 convert = 'C:/Progra~1/ImageMagick-6.3.4-Q16/convert'
  28 cached = True
  29 
  30 def getThumbnailUrl(pagename, filename, request, width=None, height=None):
  31     return "%s/%s?action=%s%s%s&target=%s" % (
  32         request.getScriptname(),
  33         wikiutil.quoteWikinameURL(pagename),
  34         action_name,
  35         width and ("&w=%d" % width) or "",
  36         height and ("&h=%d" % height) or "",
  37         filename,
  38         )
  39 
  40 def getFilename(pagename, filename, width=None, height=None, request):
  41     if (width is None) and (height is None):
  42         width = 64
  43         height = 64
  44     thumbnail = "thumbnail_%(fpath)s_%(size)s.png" % {'fpath': wikiutil.quoteWikinameFS(filename), 'size': size}
  45     thumbpath = Page(request, pagename).getPagePath("thumbnails", check_create=False)
  46     thumbpath = os.path.join(thumbpath, thumbnail)
  47     return thumbpath
  48 
  49 def execute(pagename, request):
  50     _ = request.getText
  51     
  52     if not request.user.may.read(pagename):
  53         error_msg(pagename, request, _('You are not allowed to view thumbnails of attachments from this page.'))
  54         return
  55         
  56     filename, fpath = _access_file(pagename, request)
  57     if not filename: return # error msg already sent in _access_file
  58 
  59     timestamp = timefuncs.formathttpdate(int(os.path.getmtime(fpath)))
  60     if request.if_modified_since == timestamp:
  61         request.http_headers(["Status: 304 Not modified"])
  62         request.setResponseCode(304)
  63 
  64     else:
  65     	fpath = fpath.replace("\"", "\\\"");
  66     	(w, h) = (0, 0)
  67         if request.form.get('w', [''])[0] and request.form.get('w', [''])[0].strip().isdigit():
  68             w = int(request.form.get('w', [''])[0].strip())
  69         if request.form.get('h', [''])[0] and request.form.get('h', [''])[0].strip().isdigit():
  70             h = int(request.form.get('h', [''])[0].strip())
  71         size = w and (h and ("%(w)dx%(h)d" % {'w': w, 'h': h}) or ("%d" % w)) or h and ("x%d" % h) or "64x64"
  72 
  73         thumbnail = "thumbnail_%(fpath)s_%(size)s.png" % {'fpath': wikiutil.quoteWikinameFS(filename), 'size': size}
  74 
  75         if cached:
  76             thumbpath = Page(request, pagename).getPagePath("thumbnails", check_create=True)
  77             thumbpath = os.path.join(thumbpath, thumbnail)
  78 
  79             if not os.path.isfile(thumbpath) or int(os.path.getmtime(fpath)) > int(os.path.getmtime(thumbpath)):
  80                 cmd = "%(convert)s \"%(fpath)s\" -thumbnail %(size)s -background transparent -gravity center -extent %(size)s png:\"%(thumbpath)s\"" % \
  81                       {'convert': convert, 'fpath': fpath, 'size': size, 'thumbpath': thumbpath}
  82                 pipe = Popen(cmd, shell=True, bufsize=8192, stderr=PIPE)
  83                 pipe.wait()
  84                 if not os.path.isfile(thumbpath):
  85                     error_msg(pagename, request, _("Unable to generate thumbnail.\n%(error)s\n%(cmd)s\n") % {'error': pipe.stderr.read(), 'cmd': cmd})
  86                     pipe.stderr.close();
  87                     return
  88                 pipe.stderr.close();
  89             
  90             request.http_headers([
  91                 'Content-Type: image/png',
  92                 'Last-Modified: %s' % timestamp,
  93                 'Content-Length: %d' % os.path.getsize(thumbpath),
  94                 'Content-Disposition: inline; filename="%s"' % thumbnail,
  95             ])
  96 
  97             # send data
  98             shutil.copyfileobj(open(thumbpath, 'rb'), request, 8192)
  99 
 100         else:    	
 101             pipe = Popen("%(convert)s \"%(fpath)s\" -thumbnail %(size)s -background transparent -gravity center -extent %(size)s png:-" % {'convert': convert, 'fpath': fpath, 'size': size},
 102                          shell=True, bufsize=8192, stdout=PIPE)
 103             buf = ''
 104             while pipe.poll() == None:
 105                 buf += pipe.stdout.read()
 106             pipe.stdout.close()
 107             
 108             if (pipe.poll() != 0):
 109                 error_msg(pagename, request, _("Unable to generate thumbnail."))
 110                 return
 111                 
 112             request.http_headers([
 113                 'Content-Type: image/png',
 114                 'Last-Modified: %s' % timestamp,
 115                 'Content-Length: %d' % len(buf),
 116                 'Content-Disposition: inline; filename="%s"' % thumbnail
 117             ])
 118 
 119             # send data
 120             request.write(buf)
 121 
 122     #raise MoinMoinNoFooter

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-08-27 06:16:11, 0.5 KB) [[attachment:Clear.py]]
  • [get | view] (2007-08-28 23:30:49, 6.8 KB) [[attachment:Editor.py]]
  • [get | view] (2007-08-31 16:21:54, 6.5 KB) [[attachment:ImageBrowser.inc]]
  • [get | view] (2007-08-17 18:41:06, 23.6 KB) [[attachment:ImageBrowser.zip]]
  • [get | view] (2007-08-27 06:15:30, 12.1 KB) [[attachment:ImageBrowserContent.py]]
  • [get | view] (2007-08-17 20:07:21, 51.1 KB) [[attachment:ImageBrowserContent.zip]]
  • [get | view] (2007-08-27 22:38:27, 51.5 KB) [[attachment:ImageBrowserContent_0-2.zip]]
  • [get | view] (2007-08-28 19:24:15, 51.5 KB) [[attachment:ImageBrowserContent_0-3.zip]]
  • [get | view] (2007-08-30 21:15:21, 51.5 KB) [[attachment:ImageBrowserContent_0-4.zip]]
  • [get | view] (2007-08-30 22:05:48, 3.5 KB) [[attachment:ImageBrowserGenerator.zip]]
  • [get | view] (2007-08-17 18:41:18, 28.6 KB) [[attachment:ImageBrowserNoLoad.zip]]
  • [get | view] (2007-08-10 22:00:23, 23.1 KB) [[attachment:MacroMarket-ImageBrowser.zip]]
  • [get | view] (2007-08-14 18:43:08, 23.4 KB) [[attachment:MacroMarket-ImageBrowserNoLoad.zip]]
  • [get | view] (2007-08-28 23:30:19, 2.9 KB) [[attachment:SimpleInclude.py]]
  • [get | view] (2007-08-28 23:30:27, 0.2 KB) [[attachment:Test.py]]
  • [get | view] (2007-08-28 23:29:51, 0.9 KB) [[attachment:Thumbnail.py]]
  • [get | view] (2007-08-28 23:29:31, 5.2 KB) [[attachment:Thumbnail_action.py]]
  • [get | view] (2007-08-17 18:26:12, 160.3 KB) [[attachment:imagebrowser.png]]
  • [get | view] (2007-08-17 17:13:09, 39.0 KB) [[attachment:imagebrowsercontent.png]]
  • [get | view] (2007-08-17 17:55:36, 37.2 KB) [[attachment:imagebrowsercontent_closed.png]]
  • [get | view] (2007-08-22 16:42:34, 491.5 KB) [[attachment:imgbrwsr_images.zip]]
  • [get | view] (2007-08-28 23:28:39, 3.8 KB) [[attachment:imgbrwsrgen.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.