def download(slug_candidate): global download_count if not helpers.constant_time_compare(slug.encode('ascii'), slug_candidate.encode('ascii')): abort(404) # each download has a unique id download_id = download_count download_count += 1 # prepare some variables to use inside generate() function below # which is outsie of the request context shutdown_func = request.environ.get('werkzeug.server.shutdown') path = request.path # tell GUI the download started add_request(REQUEST_DOWNLOAD, path, { 'id':download_id }) dirname = os.path.dirname(zip_filename) basename = os.path.basename(zip_filename) def generate(): chunk_size = 102400 # 100kb fp = open(zip_filename, 'rb') done = False while not done: chunk = fp.read(102400) if chunk == '': done = True else: yield chunk # tell GUI the progress downloaded_bytes = fp.tell() percent = round((1.0 * downloaded_bytes / zip_filesize) * 100, 2); sys.stdout.write("\r{0}, {1}% ".format(helpers.human_readable_filesize(downloaded_bytes), percent)) sys.stdout.flush() add_request(REQUEST_PROGRESS, path, { 'id':download_id, 'bytes':downloaded_bytes }) fp.close() sys.stdout.write("\n") # download is finished, close the server if not stay_open: print strings._("closing_automatically") if shutdown_func is None: raise RuntimeError('Not running with the Werkzeug Server') shutdown_func() r = Response(generate()) r.headers.add('Content-Length', zip_filesize) r.headers.add('Content-Disposition', 'attachment', filename=basename) # guess content type (content_type, _) = mimetypes.guess_type(basename, strict=False) if content_type is not None: r.headers.add('Content-Type', content_type) return r
def shutdown(shutdown_slug_candidate): if not helpers.constant_time_compare(shutdown_slug.encode('ascii'), shutdown_slug_candidate.encode('ascii')): abort(404) # shutdown the flask service func = request.environ.get('werkzeug.server.shutdown') if func is None: raise RuntimeError('Not running with the Werkzeug Server') func() return ""
def shutdown(shutdown_slug_candidate): if not helpers.constant_time_compare( shutdown_slug.encode('ascii'), shutdown_slug_candidate.encode('ascii')): abort(404) # shutdown the flask service func = request.environ.get('werkzeug.server.shutdown') if func is None: raise RuntimeError('Not running with the Werkzeug Server') func() return ""
def index(slug_candidate): if not helpers.constant_time_compare(slug.encode('ascii'), slug_candidate.encode('ascii')): abort(404) add_request(REQUEST_LOAD, request.path) return render_template_string( open('{0}/index.html'.format(helpers.get_onionshare_dir())).read(), slug=slug, file_info=file_info, filename=os.path.basename(zip_filename).decode("utf-8"), filesize=zip_filesize, filesize_human=helpers.human_readable_filesize(zip_filesize), strings=strings.strings )
def index(slug_candidate): if not helpers.constant_time_compare(slug.encode('ascii'), slug_candidate.encode('ascii')): abort(404) add_request(REQUEST_LOAD, request.path) return render_template_string( open(helpers.get_html_path('index.html')).read(), slug=slug, file_info=file_info, filename=os.path.basename(zip_filename).decode("utf-8"), filesize=zip_filesize, filesize_human=helpers.human_readable_filesize(zip_filesize), strings=strings.strings)
def download(slug_candidate): global download_count if not helpers.constant_time_compare(slug.encode('ascii'), slug_candidate.encode('ascii')): abort(404) # each download has a unique id download_id = download_count download_count += 1 # prepare some variables to use inside generate() function below # which is outside of the request context shutdown_func = request.environ.get('werkzeug.server.shutdown') path = request.path # tell GUI the download started add_request(REQUEST_DOWNLOAD, path, {'id': download_id}) dirname = os.path.dirname(zip_filename) basename = os.path.basename(zip_filename) def generate(): chunk_size = 102400 # 100kb fp = open(zip_filename, 'rb') done = False canceled = False while not done: chunk = fp.read(chunk_size) if chunk == '': done = True else: try: yield chunk # tell GUI the progress downloaded_bytes = fp.tell() percent = (1.0 * downloaded_bytes / zip_filesize) * 100 # suppress stdout platform on OSX (#203) if helpers.get_platform() != 'Darwin': sys.stdout.write("\r{0:s}, {1:.2f}% ".format( helpers.human_readable_filesize(downloaded_bytes), percent)) sys.stdout.flush() add_request(REQUEST_PROGRESS, path, { 'id': download_id, 'bytes': downloaded_bytes }) except: # looks like the download was canceled done = True canceled = True # tell the GUI the download has canceled add_request(REQUEST_CANCELED, path, {'id': download_id}) fp.close() if helpers.get_platform() != 'Darwin': sys.stdout.write("\n") # download is finished, close the server if not stay_open and not canceled: print strings._("closing_automatically") if shutdown_func is None: raise RuntimeError('Not running with the Werkzeug Server') shutdown_func() r = Response(generate()) r.headers.add('Content-Length', zip_filesize) r.headers.add('Content-Disposition', 'attachment', filename=basename) # guess content type (content_type, _) = mimetypes.guess_type(basename, strict=False) if content_type is not None: r.headers.add('Content-Type', content_type) return r
def slug_wrapper(slug_candidate, *args, **kwargs): if not helpers.constant_time_compare(slug.encode('ascii'),slug_candidate.encode('ascii')): abort(404) return f(*args, **kwargs)