Edgewall Software

Ticket #2974: pdfattachment.patch

File pdfattachment.patch, 3.2 KB (added by cboos, 3 years ago)

Possible fix for the issue, patch on source:trunk@3288 (take 2)

  • trac/attachment.py

     
    7979    path = property(_get_path) 
    8080 
    8181    def href(self, req, *args, **dict): 
    82         return req.href.attachment(self.parent_type, self.parent_id, 
    83                                    self.filename, *args, **dict) 
     82        base, filename = 'attachment', self.filename 
     83        if 'format' in dict: 
     84            base = dict.pop('format') + base 
     85        else: # HTML preview 
     86            filename += '.html' 
     87        return req.href(base, self.parent_type, self.parent_id, filename, 
     88                        *args, **dict) 
    8489 
    8590    def parent_href(self, req): 
    8691        return req.href(self.parent_type, self.parent_id) 
     
    250255    # IRequestHandler methods 
    251256 
    252257    def match_request(self, req): 
    253         match = re.match(r'^/attachment/(ticket|wiki)(?:[/:](.*))?$', 
     258        match = re.match(r'^/(raw|txt)?attachment/(ticket|wiki)(?:[/:](.*))?$', 
    254259                         req.path_info) 
    255260        if match: 
    256             req.args['type'] = match.group(1) 
    257             req.args['path'] = match.group(2).replace(':', '/') 
     261            req.args['type'] = match.group(2) 
     262            req.args['path'] = match.group(3).replace(':', '/') 
     263            format = match.group(1) 
     264            if format: 
     265                req.args['format'] = format 
    258266            return True 
    259267 
    260268    def process_request(self, req): 
    261269        parent_type = req.args.get('type') 
    262270        path = req.args.get('path') 
     271        format = req.args.get('format') 
    263272        if not parent_type or not path: 
    264273            raise HTTPBadRequest('Bad request') 
    265274        if not parent_type in ['ticket', 'wiki']: 
     
    272281            segments = path.split('/') 
    273282            parent_id = '/'.join(segments[:-1]) 
    274283            filename = segments[-1] 
     284            if not format and filename.endswith('.html'): 
     285                filename = filename[:-5] 
    275286            if len(segments) == 1 or not filename: 
    276287                raise HTTPBadRequest('Bad request') 
    277288            attachment = Attachment(self.env, parent_type, parent_id, filename) 
     
    299310            self._render_form(req, attachment) 
    300311        else: 
    301312            add_link(req, 'up', parent_link, parent_text) 
    302             self._render_view(req, attachment) 
     313            self._render_view(req, attachment, format) 
    303314 
    304315        add_stylesheet(req, 'common/css/code.css') 
    305316        return 'attachment.cs', None 
     
    393404        req.hdf['attachment'] = {'mode': 'new', 
    394405                                 'author': util.get_reporter_id(req)} 
    395406 
    396     def _render_view(self, req, attachment): 
     407    def _render_view(self, req, attachment, format): 
    397408        perm_map = {'ticket': 'TICKET_VIEW', 'wiki': 'WIKI_VIEW'} 
    398409        req.perm.assert_permission(perm_map[attachment.parent_type]) 
    399410 
     
    420431            mime_type = mimeview.get_mimetype(attachment.filename, str_data) 
    421432 
    422433            # Eventually send the file directly 
    423             format = req.args.get('format') 
    424434            if format in ('raw', 'txt'): 
    425435                if not self.render_unsafe_content and not binary: 
    426436                    # Force browser to download HTML/SVG/etc pages that may