Ticket #2974: pdfattachment.patch
| File pdfattachment.patch, 3.2 KB (added by cboos, 3 years ago) |
|---|
-
trac/attachment.py
79 79 path = property(_get_path) 80 80 81 81 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) 84 89 85 90 def parent_href(self, req): 86 91 return req.href(self.parent_type, self.parent_id) … … 250 255 # IRequestHandler methods 251 256 252 257 def match_request(self, req): 253 match = re.match(r'^/ attachment/(ticket|wiki)(?:[/:](.*))?$',258 match = re.match(r'^/(raw|txt)?attachment/(ticket|wiki)(?:[/:](.*))?$', 254 259 req.path_info) 255 260 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 258 266 return True 259 267 260 268 def process_request(self, req): 261 269 parent_type = req.args.get('type') 262 270 path = req.args.get('path') 271 format = req.args.get('format') 263 272 if not parent_type or not path: 264 273 raise HTTPBadRequest('Bad request') 265 274 if not parent_type in ['ticket', 'wiki']: … … 272 281 segments = path.split('/') 273 282 parent_id = '/'.join(segments[:-1]) 274 283 filename = segments[-1] 284 if not format and filename.endswith('.html'): 285 filename = filename[:-5] 275 286 if len(segments) == 1 or not filename: 276 287 raise HTTPBadRequest('Bad request') 277 288 attachment = Attachment(self.env, parent_type, parent_id, filename) … … 299 310 self._render_form(req, attachment) 300 311 else: 301 312 add_link(req, 'up', parent_link, parent_text) 302 self._render_view(req, attachment )313 self._render_view(req, attachment, format) 303 314 304 315 add_stylesheet(req, 'common/css/code.css') 305 316 return 'attachment.cs', None … … 393 404 req.hdf['attachment'] = {'mode': 'new', 394 405 'author': util.get_reporter_id(req)} 395 406 396 def _render_view(self, req, attachment ):407 def _render_view(self, req, attachment, format): 397 408 perm_map = {'ticket': 'TICKET_VIEW', 'wiki': 'WIKI_VIEW'} 398 409 req.perm.assert_permission(perm_map[attachment.parent_type]) 399 410 … … 420 431 mime_type = mimeview.get_mimetype(attachment.filename, str_data) 421 432 422 433 # Eventually send the file directly 423 format = req.args.get('format')424 434 if format in ('raw', 'txt'): 425 435 if not self.render_unsafe_content and not binary: 426 436 # Force browser to download HTML/SVG/etc pages that may
