Edgewall Software

Ticket #7078: trac-0.11.0-get-attachments-as-zipfile.patch

File trac-0.11.0-get-attachments-as-zipfile.patch, 2.9 KB (added by mark@…, 4 years ago)

Patch adapted for trac 0.11.0

  • trac/attachment.py

     
    2222import shutil 
    2323import time 
    2424import unicodedata 
     25import zipfile 
     26import tempfile 
    2527 
    2628from genshi.builder import tag 
    2729 
     
    375377 
    376378        parent = parent_realm(id=parent_id) 
    377379         
     380        if action == 'getzip': 
     381            self._do_getzip(req, parent) 
     382            return 'attachment.html', data, None 
     383 
    378384        # Link the attachment page to parent resource 
    379385        parent_name = get_resource_name(self.env, parent) 
    380386        parent_url = get_resource_url(self.env, parent, req.href) 
     
    517523 
    518524    # Internal methods 
    519525 
     526    def _do_getzip(self, req, parent): 
     527        db = self.env.get_db_cnx() 
     528 
     529        p_id = parent.id 
     530        p_type = parent.realm 
     531 
     532        zfname = 'Ticket%s.zip' % p_id 
     533 
     534        zfpath = os.path.join(tempfile.gettempdir(), zfname) 
     535 
     536        zf = zipfile.ZipFile(zfpath, 'w') 
     537        for attachment in Attachment.select(self.env, p_type, p_id, db): 
     538#            name = attachment.filename.encode('utf8') 
     539            name = attachment.filename.encode('cp437') 
     540            zf.write(attachment.path, name) 
     541            info = zf.getinfo(name) 
     542            # Set bit 11 indicate that filename and comment are in UTF-8, see http://www.pkware.com/documents/casestudies/APPNOTE.TXT 
     543#            info.flag_bits |= 1 << 11 
     544        zf.close() 
     545 
     546        req.send_header('Content-Disposition', 'attachment; filename=%s' % zfname) 
     547        mime_type = 'application/octet-stream' 
     548         
     549        req.send_file(zfpath, mime_type) 
     550         
     551        os.remove(zfpath) 
     552 
    520553    def _do_save(self, req, attachment): 
    521554        req.perm(attachment.resource).require('ATTACHMENT_CREATE') 
    522555 
  • trac/templates/macros.html

     
    208208  -     'alist' is an AttachmentList object (see attachment.py) 
    209209  --> 
    210210  <py:def function="attach_file_form(alist)"> 
    211     <form py:if="alist.can_create" method="get" action="${alist.attach_href}" id="attachfile"> 
    212       <div> 
     211    <div> 
     212    <form py:if="alist.can_create" method="get" action="${alist.attach_href}" id="attachfile" style="display:inline;"> 
    213213        <input type="hidden" name="action" value="new" /> 
    214214        <input type="submit" name="attachfilebutton" value="${_('Attach file')}" /> 
    215       </div> 
    216215    </form> 
     216    <form py:if="alist.attachments" method="get" action="${alist.attach_href}" id="getattachmentsaszip" style="display:inline;"> 
     217        <input type="hidden" name="action" value="getzip" /> 
     218        <input type="submit" name="getattachmentsaszipbutton" value="${_('Save all as ZIP-file')}" /> 
     219    </form> 
     220    </div> 
    217221  </py:def> 
    218222 
    219223  <!--!  Display a generic "progress bar", for use in roadmap and milestone.