From 71124eb2addbebba986a576bf817b78052b9c06c Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Apr 17 2018 13:01:38 +0000 Subject: Do not syntax highlight 'huge' files Signed-off-by: Patrick Uiterwijk --- diff --git a/pagure/templates/file.html b/pagure/templates/file.html index 00329d4..3cfa6dc 100644 --- a/pagure/templates/file.html +++ b/pagure/templates/file.html @@ -176,7 +176,11 @@ {% if output_type=='file' %} {% autoescape false %} - {{ content | format_loc }} + {% if huge %} + {{ content | e | format_loc }} + {% else %} + {{ content | format_loc }} + {% endif %} {% endautoescape %} {% elif output_type == 'markup' %}
diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 6e14d0c..dfdc5db 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -56,6 +56,7 @@ from pagure.utils import ( __get_file_in_tree, authenticated, login_required, + stream_template, ) from pagure.decorators import ( is_repo_admin, @@ -65,6 +66,11 @@ from pagure.decorators import ( _log = logging.getLogger(__name__) +# Number of characters to determine that a file is "huge" +# Huge files will not get syntax highlighting +HUGEFILE = 5000 + + def get_git_url_ssh(): """ Return the GIT SSH URL to be displayed in the UI based on the content of the configuration file. @@ -526,6 +532,11 @@ def view_file(repo, identifier, filename, username=None, namespace=None): safe = False readme_ext = None headers = {} + huge = False + + isbinary = False + if 'data' in dir(content): + isbinary = is_binary_string(content.data) if isinstance(content, pygit2.Blob): rawtext = str(flask.request.args.get('text')).lower() in ['1', 'true'] @@ -544,7 +555,7 @@ def view_file(repo, identifier, filename, username=None, namespace=None): elif ext in ('.rst', '.mk', '.md', '.markdown') and not rawtext: content, safe = pagure.doc_utils.convert_readme(content.data, ext) output_type = 'markup' - elif not is_binary_string(content.data): + elif 'data' in dir(content) and len(content.data) < HUGEFILE and not isbinary: file_content = None try: file_content = encoding_utils.decode( @@ -579,6 +590,11 @@ def view_file(repo, identifier, filename, username=None, namespace=None): output_type = 'file' else: output_type = 'binary' + elif not isbinary: + output_type = 'file' + huge = True + safe = False + content = content.data.decode('utf-8') else: output_type = 'binary' elif isinstance(content, pygit2.Commit): @@ -600,8 +616,8 @@ def view_file(repo, identifier, filename, username=None, namespace=None): if output_type == 'binary': headers['Content-Disposition'] = 'attachment' - return ( - flask.render_template( + return flask.Response(flask.stream_with_context(stream_template( + flask.current_app, 'file.html', select='tree', repo=repo, @@ -614,7 +630,8 @@ def view_file(repo, identifier, filename, username=None, namespace=None): readme=readme, readme_ext=readme_ext, safe=safe, - ), + huge=huge, + )), 200, headers ) diff --git a/pagure/utils.py b/pagure/utils.py index 057ddeb..0c9222a 100644 --- a/pagure/utils.py +++ b/pagure/utils.py @@ -389,3 +389,11 @@ def get_parent_repo_path(repo): parentpath = os.path.join(pagure_config['GIT_FOLDER'], repo.path) return parentpath + + +def stream_template(app, template_name, **context): + app.update_template_context(context) + t = app.jinja_env.get_template(template_name) + rv = t.stream(context) + rv.enable_buffering(5) + return rv