Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File encoding not properly detected causing internal server error in previewer. #197

Open
lnielsen opened this issue Jan 18, 2024 · 0 comments

Comments

@lnielsen
Copy link
Member

lnielsen commented Jan 18, 2024

Package version (if known): v2.1.0

Describe the bug

Encoding of file content not properly detected causing the preview to break.

The error only occurs in some positional placements as the detect_encoding() only reads first 1024 bytes.

Proposed solution is to default to UTF8 even if detect_encoding comes up with ascii.

Steps to Reproduce

Example file causing the error is attached when uploading it and trying to preview it.

Expected behavior

As minimum a message should say the file is not previewable, instead of an internal server error.

Screenshots (if applicable)

Screenshot 2024-01-18 at 11 48 39

Additional context

Stacktrace

Markdown previewer:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1769: ordinal not in range(128)
  File "flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "flask/app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "flask/app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
  File "flask/app.py", line 1799, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "invenio_app_rdm/records_ui/views/decorators.py", line 111, in view
    return f(**kwargs)
  File "invenio_app_rdm/records_ui/views/decorators.py", line 127, in view
    return f(**kwargs)
  File "invenio_app_rdm/records_ui/views/decorators.py", line 200, in view
    return f(**kwargs)
  File "invenio_app_rdm/records_ui/views/decorators.py", line 261, in view
    return f(**kwargs)
  File "invenio_app_rdm/records_ui/views/records.py", line 287, in record_file_preview
    return plugin.preview(fileobj)
  File "invenio_previewer/extensions/mistune.py", line 39, in preview
    content=render(file),
  File "invenio_previewer/extensions/mistune.py", line 25, in render
    result = mistune.markdown(fp.read().decode(encoding))

JSON previewer:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 18515: ordinal not in range(128)
  File "flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "flask/app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "flask/app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
  File "flask/app.py", line 1799, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "invenio_app_rdm/records_ui/views/decorators.py", line 111, in view
    return f(**kwargs)
  File "invenio_app_rdm/records_ui/views/decorators.py", line 127, in view
    return f(**kwargs)
  File "invenio_app_rdm/records_ui/views/decorators.py", line 200, in view
    return f(**kwargs)
  File "invenio_app_rdm/records_ui/views/decorators.py", line 261, in view
    return f(**kwargs)
  File "invenio_app_rdm/records_ui/views/records.py", line 287, in record_file_preview
    return plugin.preview(fileobj)
  File "invenio_previewer/extensions/json_prismjs.py", line 57, in preview
    content=render(file),
  File "invenio_previewer/extensions/json_prismjs.py", line 26, in render
    file_content = fp.read().decode(encoding)

File

The file includes a µ on line 365.

ifdo-v2.0.0.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant