1# Copyright (c) 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import re
7import sys
8import webapp2
9import uuid
10
11from perf_insights import trace_info
12
13sys.path.append('third_party')
14import cloudstorage as gcs
15
16default_retry_params = gcs.RetryParams(initial_delay=0.2,
17                                       max_delay=5.0,
18                                       backoff_factor=2,
19                                       max_retry_period=15)
20gcs.set_default_retry_params(default_retry_params)
21
22
23class UploadPage(webapp2.RequestHandler):
24
25  def get(self):
26    self.response.out.write("""
27          <html><body>
28            <head><title>Performance Insights - Trace Uploader</title></head>
29            <form action="/upload" enctype="multipart/form-data" method="post">
30              <div><input type="file" name="trace"/></div>
31              <div><input type="submit" value="Upload"></div>
32            </form><hr>
33          </body></html>""")
34
35  def post(self):
36    trace_uuid = str(uuid.uuid4())
37    bucket_name = ('/performance-insights/' + trace_uuid)
38    gcs_file = gcs.open(bucket_name,
39                        'w',
40                        content_type='application/octet-stream',
41                        options={},
42                        retry_params=default_retry_params)
43    gcs_file.write(self.request.get('trace'))
44    gcs_file.close()
45
46    trace_object = trace_info.TraceInfo(id=trace_uuid)
47    trace_object.prod = self.request.get('prod')
48    trace_object.remote_addr = os.environ["REMOTE_ADDR"]
49    tags_string = self.request.get('tags')
50    if re.match('^[a-zA-Z0-9,]+$', tags_string): # ignore non alpha-numeric tags
51      trace_object.tags = tags_string.split(',')
52    trace_object.user_agent = self.request.headers.get('User-Agent')
53    trace_object.ver = self.request.get('product_version')
54    trace_object.put()
55
56    self.response.write(trace_uuid)
57
58app = webapp2.WSGIApplication([('/upload', UploadPage)])
59