blob: d6fa978f48f8bc0ee88656fc9b84badc91d3862a [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Issue Tracker code to serve out issue attachments.
7
8Summary of page classes:
9 AttachmentPage: Serve the content of an attachment w/ the appropriate
10 MIME type.
11 IssueAttachmentDeletion: Form handler for deleting attachments.
12"""
13from __future__ import print_function
14from __future__ import division
15from __future__ import absolute_import
16
17import base64
18import logging
19import os
20import re
21import urllib
22
23import webapp2
24
25from google.appengine.api import app_identity
26from google.appengine.api import images
27
28from framework import exceptions
29from framework import framework_constants
30from framework import framework_helpers
31from framework import gcs_helpers
32from framework import permissions
33from framework import servlet
34from framework import urls
35from tracker import attachment_helpers
36from tracker import tracker_helpers
37from tracker import tracker_views
38
39
40# This will likely appear blank or as a broken image icon in the browser.
41NO_PREVIEW_ICON = ''
42NO_PREVIEW_MIME_TYPE = 'image/png'
43
44
45class AttachmentPage(servlet.Servlet):
46 """AttachmentPage serves issue attachments."""
47
48 def GatherPageData(self, mr):
49 """Parse the attachment ID from the request and serve its content.
50
51 Args:
52 mr: commonly used info parsed from the request.
53
54 Returns: dict of values used by EZT for rendering the page.
55 """
56 if mr.signed_aid != attachment_helpers.SignAttachmentID(mr.aid):
57 webapp2.abort(400, 'Please reload the issue page')
58
59 try:
60 attachment, _issue = tracker_helpers.GetAttachmentIfAllowed(
61 mr, self.services)
62 except exceptions.NoSuchIssueException:
63 webapp2.abort(404, 'issue not found')
64 except exceptions.NoSuchAttachmentException:
65 webapp2.abort(404, 'attachment not found')
66 except exceptions.NoSuchCommentException:
67 webapp2.abort(404, 'comment not found')
68
69 if not attachment.gcs_object_id:
70 webapp2.abort(404, 'attachment data not found')
71
72 bucket_name = app_identity.get_default_gcs_bucket_name()
73
74 gcs_object_id = attachment.gcs_object_id
75
76 logging.info('attachment id %d is %s', mr.aid, gcs_object_id)
77
78 # By default GCS will return images and attachments displayable inline.
79 if mr.thumb:
80 # Thumbnails are stored in a separate obj always displayed inline.
81 gcs_object_id = gcs_object_id + '-thumbnail'
82 elif not mr.inline:
83 # Downloads are stored in a separate obj with disposiiton set.
84 filename = attachment.filename
85 if not framework_constants.FILENAME_RE.match(filename):
86 logging.info('bad file name: %s' % attachment.attachment_id)
87 filename = 'attachment-%d.dat' % attachment.attachment_id
88 if gcs_helpers.MaybeCreateDownload(
89 bucket_name, gcs_object_id, filename):
90 gcs_object_id = gcs_object_id + '-download'
91
92 url = gcs_helpers.SignUrl(bucket_name, gcs_object_id)
93 self.redirect(url, abort=True)