Skip to content

Commit

Permalink
txt-preview: improvements (#180)
Browse files Browse the repository at this point in the history
* enable horizontal scrolling
* add option to truncate .txt file preview after PREVIEWER_TXT_MAX_BYTES
* avoid possible invalid encoding errors

Co-authored-by: Guillaume Viger <[email protected]>
  • Loading branch information
dfdan and fenekku authored Aug 11, 2023
1 parent cd63d8d commit a8aa2c8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#text-previewer {
padding: 0.5em;
}

3 changes: 3 additions & 0 deletions invenio_previewer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
PREVIEWER_MAX_IMAGE_SIZE_BYTES = 0.5 * 1024 * 1024
"""Maximum file size in bytes for image files."""

PREVIEWER_TXT_MAX_BYTES = 1 * 1024 * 1024
"""Maximum number of .txt file bytes to preview before truncated."""

PREVIEWER_ZIP_MAX_FILES = 1000
"""Max number of files showed in the ZIP previewer."""

Expand Down
8 changes: 5 additions & 3 deletions invenio_previewer/extensions/txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@

"""Text rendering."""

from flask import render_template
from flask import current_app, render_template

from ..proxies import current_previewer
from ..utils import detect_encoding

previewable_extensions = ["txt"]
max_bytes = current_app.config.get('PREVIEWER_TXT_MAX_BYTES', -1)


def render(file):
"""Render HTML from txt file content."""
with file.open() as fp:
encoding = detect_encoding(fp, default="utf-8")
return fp.read().decode(encoding)
return fp.read(max_bytes).decode(encoding, errors="ignore")


def can_preview(file):
Expand All @@ -37,5 +38,6 @@ def preview(file):
file=file,
content=render(file),
js_bundles=current_previewer.js_bundles,
css_bundles=current_previewer.css_bundles,
css_bundles=['txt_css.css'],
truncated=max_bytes < file.size,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
{%- extends config.PREVIEWER_ABSTRACT_TEMPLATE %}

{% block panel %}
<div class="ui container">
<div class="ui grid column">
<div class="column">
<pre>{{ content }}</pre>
</div>
</div>
<div id="text-previewer">
<pre>{{ content }}</pre>
{% if truncated %}
<hr /><div class="banner">{{ _('Preview of large file truncated') }}</div>
{% endif %}
</div>
{% endblock %}
1 change: 1 addition & 0 deletions invenio_previewer/webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
'bottom_css': './scss/invenio_previewer/bottom.scss',
'simple_image_css':
'./scss/invenio_previewer/simple_image.scss',
'txt_css': './scss/invenio_previewer/txt.scss',
},
dependencies={
'd3': '^3.5.17',
Expand Down
18 changes: 18 additions & 0 deletions tests/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,24 @@ def test_simple_image_extension(app, webassets, record):
assert 'class="previewer-simple-image"' in res.get_data(as_text=True)


def test_txt_extension(app, webassets, record):
"""Text .txt file viewer."""
create_file(record, 'test1.txt', BytesIO(b'test content foobar'))

with app.test_client() as client:
res = client.get(preview_url(record['control_number'], 'test1.txt'))
assert "<pre>test content foobar</pre>" in res.get_data(as_text=True)

max_file_size = app.config.get(
'PREVIEWER_TXT_MAX_BYTES', 1 * 1024 * 1024)
too_large_string = '1' * (max_file_size + 1)
create_file(record, 'test2.txt', BytesIO(b(too_large_string)))

with app.test_client() as client:
res = client.get(preview_url(record['control_number'], 'test1.txt'))
assert "file truncated" in res.get_data(as_text=True)


def test_view_macro_file_list(app):
"""Test file list macro."""
with app.test_request_context():
Expand Down

0 comments on commit a8aa2c8

Please sign in to comment.