Attachment 'AttachFile-1.3.5.patch'

Download

   1 --- AttachFile.py	2005-08-05 12:41:38.000000000 +0200
   2 +++ ../AttachFile.py	2005-08-05 13:32:09.960520928 +0200
   3 @@ -20,10 +20,11 @@
   4  
   5      @copyright: 2001 by Ken Sugino (sugino@mediaone.net)
   6      @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>
   7 +    @copyright: 2005 ReimarBauer added zip file extension based on patch for 1.3.4 and DesktopEdition 1.3.4-2 by AlexanderSchremmer
   8      @license: GNU GPL, see COPYING for details.
   9  """
  10  
  11 -import os, mimetypes, time, urllib
  12 +import os, mimetypes, time, urllib, zipfile
  13  from MoinMoin import config, user, util, wikiutil
  14  from MoinMoin.Page import Page
  15  from MoinMoin.util import MoinMoinNoFooter, filesys, web
  16 @@ -203,7 +204,8 @@
  17          label_get = _("get")
  18          label_edit = _("edit")
  19          label_view = _("view")
  20 -
  21 +        label_unzip = _("unzip")
  22 +	
  23          for file in files:
  24              fsize = float(os.stat(os.path.join(attach_dir,file).encode(config.charset))[6]) # in byte
  25              fsize = "%.1f" % (fsize / 1024)
  26 @@ -218,6 +220,7 @@
  27                          'urlfile': urlfile, 'label_del': label_del,
  28                          'base': base, 'label_edit': label_edit,
  29                          'label_view': label_view,
  30 +                        'label_unzip': label_unzip,
  31                          'get_url': get_url, 'label_get': label_get,
  32                          'file': wikiutil.escape(file), 'fsize': fsize,
  33                          'pagename': pagename}
  34 @@ -232,6 +235,11 @@
  35              else:
  36                  viewlink = '<a href="%(baseurl)s/%(urlpagename)s?action=%(action)s&amp;do=view&amp;target=%(urlfile)s">%(label_view)s</a>' % parmdict
  37  
  38 +            if (zipfile.is_zipfile(os.path.join(attach_dir,file).encode(config.charset)) and
  39 +                request.user.may.read(pagename) and request.user.may.delete(pagename)
  40 +                and request.user.may.write(pagename)):
  41 +                viewlink += ' | <a href="%(baseurl)s/%(urlpagename)s?action=%(action)s&amp;do=unzip&amp;target=%(urlfile)s">%(label_unzip)s</a>' % parmdict
  42 +		
  43              parmdict['viewlink'] = viewlink
  44              parmdict['del_link'] = del_link
  45              str = str + ('<li>[%(del_link)s'
  46 @@ -419,6 +427,11 @@
  47              get_file(pagename, request)
  48          else:
  49              msg = _('You are not allowed to get attachments from this page.')
  50 +    elif request.form['do'][0] == 'unzip':
  51 +        if request.user.may.delete(pagename) and request.user.may.read(pagename) and request.user.may.write(pagename):
  52 +            unzip_file(pagename, request)
  53 +        else:
  54 +            msg = _('You are not allowed to unzip attachments of this page.')
  55      elif request.form['do'][0] == 'view':
  56          if request.user.may.read(pagename):
  57              view_file(pagename, request)
  58 @@ -586,6 +599,71 @@
  59      shutil.copyfileobj(open(fpath, 'rb'), request, 8192)
  60  
  61      raise MoinMoinNoFooter
  62 +    
  63 +def unzip_file(pagename, request):
  64 +    _ = request.getText
  65 +    valid_pathname = lambda name: (name.find('/') == -1) and (name.find('\\') == -1)
  66 +
  67 +    filename, fpath = _access_file(pagename, request)
  68 +    if not filename: return # error msg already sent in _access_file
  69 +
  70 +    attachment_path = getAttachDir(request, pagename)
  71 +    single_file_size = 2.0 * 1000**2
  72 +    attachments_file_space = 200.0 * 1000**2
  73 +
  74 +    files = _get_files(request, pagename)
  75 +
  76 +    msg = ""
  77 +    if files:
  78 +        fsize = 0.0
  79 +        for file in files:
  80 +            fsize += float(os.stat(getFilename(request, pagename, file))[6]) # in byte
  81 +
  82 +        available_attachments_file_space = attachments_file_space - fsize
  83 +
  84 +        if zipfile.is_zipfile(fpath):
  85 +            zf = zipfile.ZipFile(fpath)
  86 +            sum_size_over_all_valid_files = 0.0
  87 +            for name in zf.namelist():
  88 +                if valid_pathname(name):
  89 +                    sum_size_over_all_valid_files += zf.getinfo(name).file_size
  90 +
  91 +            if sum_size_over_all_valid_files < available_attachments_file_space:
  92 +                valid_name = False
  93 +                for name in zf.namelist():
  94 +                    if valid_pathname(name):
  95 +                        zi = zf.getinfo(name)
  96 +                        if zi.file_size < single_file_size:
  97 +                            new_file = getFilename(request, pagename, name)
  98 +                            if not os.path.exists(new_file):
  99 +                                outfile = open(new_file, 'wb')
 100 +                                outfile.write(zf.read(name))
 101 +                                outfile.close()
 102 +                                # it's not allowed to zip a zip file so it is dropped
 103 +                                if zipfile.is_zipfile(new_file):
 104 +                                    os.unlink(new_file)
 105 +                                else:
 106 +                                    valid_name = True
 107 +                                    os.chmod(new_file, 0666 & config.umask)
 108 +                                    _addLogEntry(request, 'ATTNEW', pagename, new_file)
 109 +
 110 +                if valid_name:
 111 +                    msg=_("Attachment '%(filename)s' unzipped.") % {'filename': filename}
 112 +                else:
 113 +                    msg=_("Attachment '%(filename)s' not unzipped because the "
 114 +                          "files are too big, .zip files only, exist already or "
 115 +                          "reside in folders.") % {'filename': filename}
 116 +            else:
 117 +                msg=_("Attachment '%(filename)s' could not be unzipped because"
 118 +                      " the resulting files would be too large (%(space)d kB"
 119 +                      " missing).") % {'filename': filename,
 120 +                    'space': (sum_size_over_all_valid_files -
 121 +                              available_attachments_file_space) / 1000}
 122 +        else:
 123 +            msg = _('The file %(target) is not a .zip file.' % target)
 124 +
 125 +    upload_form(pagename, request, msg=wikiutil.escape(msg))
 126 +    
 127  
 128  def send_viewfile(pagename, request):
 129      _ = request.getText

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] (2005-08-05 11:29:52, 28.4 KB) [[attachment:AttachFile-1.3.5-rb.py]]
  • [get | view] (2005-08-05 11:28:28, 5.8 KB) [[attachment:AttachFile-1.3.5.patch]]
  • [get | view] (2005-11-23 21:34:41, 7.4 KB) [[attachment:AttachFile-1.3.5.unzipwithsubdir.patch]]
  • [get | view] (2005-11-23 22:32:32, 4.0 KB) [[attachment:AttachFile-1.5.zipsubdir.patch]]
  • [get | view] (2005-04-08 08:23:38, 28.1 KB) [[attachment:AttachFile.py]]
  • [get | view] (2005-04-30 20:43:59, 0.5 KB) [[attachment:UploadDir.zip]]
  • [get | view] (2005-04-08 08:40:55, 9.5 KB) [[attachment:patch_AttachFile.py]]
  • [get | view] (2005-04-07 20:25:41, 4.3 KB) [[attachment:unzip1.png]]
  • [get | view] (2005-04-07 20:25:54, 5.3 KB) [[attachment:unzip2.png]]
  • [get | view] (2005-04-07 20:26:07, 3.3 KB) [[attachment:unzip3a.png]]
  • [get | view] (2005-04-07 20:26:24, 17.3 KB) [[attachment:unzip3b.png]]
  • [get | view] (2005-04-07 20:26:37, 5.4 KB) [[attachment:unzip4.png]]
  • [get | view] (2005-04-07 20:26:49, 4.6 KB) [[attachment:unzip5.png]]
  • [get | view] (2005-04-07 20:27:01, 4.8 KB) [[attachment:unzip6.png]]
  • [get | view] (2005-04-07 20:27:15, 5.2 KB) [[attachment:unzip7.png]]
 All files | Selected Files: delete move to page copy to page

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