Example #1
0
                                'palette': {'filename': current_conversion.filename,
                                                'colors': colors}}

                    log_upload(current_conversion, time.time() - start_time)
            except Exception, e:
                trace = traceback.format_exc()

                logging.error('Exception while tyring to upload.')
                logging.error(e.message)
                logging.error(trace)

                email_message = e.message + '\r\n\r\n' + trace

                support_email('Upload Failed', email_message)

                response = {'message': "Something bad happened, we're looking at it."}

                self.response.status = 500
        else:
            response = {'message': "Something bad happened, we're looking at it."}

            logging.error(self.request.params.multi)

            if len(self.request.params.multi) > 0:
                support_email('Upload Failed', str(self.request.params.multi))

            self.response.status = 500

        self.response.out.write(simplejson.dumps(response))

Example #2
0
    def get(self):

        # noinspection PyBroadException
        try:
            matches = re.match(
                r"/download/(?P<hash>[0-9a-z]+)/(?P<filename>[-\w^&'@{}[\],$=!#().%+~ ]+).(?P<extension>txt|csv|xlsx|xls|json|tsv|xml|html|pdf)",
                self.request.path)

            if matches:
                file_hash = matches.group("hash")
                filename = matches.group("filename")
                extension = matches.group("extension")

                download_id = file_hash + '.' + extension

                self.response.set_cookie(download_id)

                start_time = time.time()

                current_conversion = self.get_conversion_from_hash(file_hash)

                if file_hash == 'sample':
                    path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/error.html')
                    self.response.out.write(template.render(path, {'status': '???',
                                                                   'message': "Upload a valid .ics file and we'll<br/>convert it to .csv, .xlsx, .html or .pdf."}))

                    self.response.status = 200
                else:
                    if current_conversion:
                        events = current_conversion.get_events()

                        self.response.headers['Content-Transfer-Encoding'] = 'binary'
                        self.response.headers['Accept-Range'] = 'bytes'
                        self.response.headers['Content-Length'] = str(current_conversion.file_size)
                        self.response.headers['Content-Encoding'] = 'binary'
                        self.response.headers['Content-Disposition'] = 'attachment; filename=' + filename + '.' + extension

                        if extension == 'txt':
                            self.response.headers['Content-Type'] = 'application/txt;charset=utf-8'
                            output_content = generate_txt_content(events)

                            log_download(current_conversion, time.time() - start_time, extension)

                            logging.info('Downloaded "' + filename + '.' + extension + '" with ' + str(current_conversion.event_count) + ' events.')

                            output_content = output_content.replace('\n', '\r\n')

                            self.response.out.write(output_content)
                        else:
                            if current_conversion.paid_date:
                                if extension == 'xls':
                                    self.response.headers['Content-Type'] = 'application/xls'
                                    output_content = generate_xls_content(events)

                                if extension == 'xlsx':
                                    self.response.headers['Content-Type'] = 'application/xlsx'
                                    output_content = generate_xlsx_content(events)

                                if extension == 'json':
                                    self.response.headers['Content-Type'] = 'application/json'
                                    output_content = generate_json_content(events)

                                if extension == 'tsv':
                                    self.response.headers['Content-Type'] = 'application/tsv'
                                    output_content = generate_tsv_content(events)

                                if extension == 'csv':
                                    self.response.headers['Content-Type'] = 'application/csv'
                                    output_content = generate_csv_content(events)

                                if extension == 'xml':
                                    self.response.headers['Content-Type'] = 'application/xml'
                                    output_content = generate_xml_content(events)

                                if extension == 'html':
                                    self.response.headers['Content-Type'] = 'application/html'
                                    output_content = generate_html_content(events, filename + '.' + extension, current_conversion.event_count)

                                if extension == 'pdf':
                                    self.response.headers['Content-Type'] = 'application/pdf'
                                    output_content = generate_pdf_content(events, filename + '.' + extension, current_conversion.event_count)

                                log_download(current_conversion, time.time() - start_time, extension)
                                logging.info('Downloaded "' + filename + '.' + extension + '" with ' + str(current_conversion.event_count) + ' events.')

                                self.response.out.write(output_content)
                            else:
                                support_email('Download Failed',
                                              "Trying to download file type (" + extension + ") which hasn't been paid for, hash: " + file_hash)

                                logging.warn(
                                    "Trying to download file type (" + extension + ") which hasn't been paid for, hash: " + file_hash)

                                path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/error.html')
                                self.response.out.write(template.render(path, {'status': '404',
                                                                               'message': "We don't have what you're looking for."}))

                                self.response.status = 404
                    else:
                        support_email('Download Failed', 'Could not find hash: ' + file_hash)

                        logging.warn('Could not find hash: ' + file_hash)

                        path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/error.html')
                        self.response.out.write(template.render(path, {'status': '404',
                                                                       'message': "We don't have what you're looking for."}))

                        self.response.status = 404
            else:
                matches = re.match(
                    r"/download/(?P<hash>[0-9a-z]+)",
                    self.request.path)

                if matches:
                    logging.warn('Could not parse path: ' + self.request.path + ' redirecting.')

                    file_hash = matches.group("hash")

                    self.redirect("/" + file_hash)
                else:
                    support_email('Download Failed', 'Could not parse path: ' + self.request.path)

                    logging.warn('Could not parse path: ' + self.request.path)

                    path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/error.html')
                    self.response.out.write(template.render(path, {'status': '404',
                                                                   'message': "We don't have what you're looking for."}))

                    self.response.status = 404
        except DeadlineExceededError, e:
            trace = traceback.format_exc()

            logging.error(e.message)
            logging.error(trace)

            email_message = self.request.path + '\r\n\r\n' + e.message + '\r\n\r\n' + trace

            support_email('DeadlineExceededError', email_message)

            path = os.path.join(os.path.join(os.path.dirname(__file__), 'html'), '../templates/error.html')
            self.response.out.write(template.render(path, {'status': '500',
                                                           'message': "Something bad happened, we're looking at it."}))

            self.response.status = 500