aiidalab / aiidalab-widgets-base

Reusable widgets for AiiDAlab applications
MIT License
7 stars 17 forks source link

FolderDataViewer chokes on binary files #350

Closed danielhollas closed 5 months ago

danielhollas commented 2 years ago

I was trying to browse the content if the Retrieved folder of an ORCA calculation in the NodeTreeWidget, but when I tried to open and download a binary wavefunction file, I got a Unicode decoding error.

image

Here's part of the stack trace

~/apps/aiidalab-widgets-base/aiidalab_widgets_base/viewers.py in change_file_view(self, change)
   1057     def change_file_view(self, change=None):  # pylint: disable=unused-argument
   1058         with self._folder.open(self.files.value) as fobj:
-> 1059             self.text.value = fobj.read()
   1060 
   1061     def download(self, change=None):  # pylint: disable=unused-argument

/opt/conda/lib/python3.7/codecs.py in decode(self, input, final)
    320         # decode input (taking the buffer into account)
    321         data = self.buffer + input
--> 322         (result, consumed) = self._buffer_decode(data, self.errors, final)
    323         # keep undecoded input until the next call
    324         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 8: invalid start byte

And here's the stack trace if I try to download the file

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
~/apps/aiidalab-widgets-base/aiidalab_widgets_base/viewers.py in download(self, change)
   1064 
   1065         payload = base64.b64encode(
-> 1066             self._folder.get_object_content(self.files.value).encode()
   1067         ).decode()
   1068         javas = Javascript(

~/.local/lib/python3.7/site-packages/aiida/orm/nodes/node.py in get_object_content(self, path, mode, key)
    545             warnings.warn("from v2.0 only the modes 'r' and 'rb' will be accepted", AiidaDeprecationWarning)  # pylint: disable=no-member
    546 
--> 547         return self._repository.get_object_content(path, mode)
    548 
    549     def put_object_from_tree(

~/.local/lib/python3.7/site-packages/aiida/orm/utils/_repository.py in get_object_content(self, key, mode)
    136         """
    137         with self.open(key, mode=mode) as handle:
--> 138             return handle.read()
    139 
    140     def put_object_from_tree(self, path, key=None, contents_only=True, force=False):

/opt/conda/lib/python3.7/codecs.py in decode(self, input, final)
    320         # decode input (taking the buffer into account)
    321         data = self.buffer + input
--> 322         (result, consumed) = self._buffer_decode(data, self.errors, final)
    323         # keep undecoded input until the next call
    324         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 8: invalid start byte

We need to handle these errors and do the appropriate thing for binary filles.

csadorf commented 2 years ago

Thanks for reporting!!