skuroda/PackageResourceViewer

Unable to view image (binary) files

Opened this issue · 4 comments

Trying to use PackageResourceViewer to open a image (binary) file currently fails.

failed to load image

Tested in ST 3125 on Windows 7 x64.

Steps to reproduce:

  1. Open Command Palette
  2. Type PRV: O and select Package Resource Viewer: Open Resource
  3. Select Theme - Default
  4. Select a .png file, for example arrow_down.png
  5. See an error instead of the image: Failed to load image

The error in the console is:

Traceback (most recent call last):
  File "package_resource_viewer in C:\Users\Keith\AppData\Roaming\Sublime Text 3\Installed Packages\PackageResourceViewer.sublime-package", line 108, in package_file_callback
  File "package_resource_viewer in C:\Users\Keith\AppData\Roaming\Sublime Text 3\Installed Packages\PackageResourceViewer.sublime-package", line 137, in open_file
  File "package_resources in C:\Users\Keith\AppData\Roaming\Sublime Text 3\Installed Packages\PackageResourceViewer.sublime-package", line 29, in get_resource
  File "package_resources in C:\Users\Keith\AppData\Roaming\Sublime Text 3\Installed Packages\PackageResourceViewer.sublime-package", line 42, in _get_resource
  File "C:\Program Files\Sublime Text 3\sublime.py", line 190, in load_resource
    s = sublime_api.load_resource(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
decode image failed

I believe this is because load_binary_resource must be used instead of load_resource. Maybe it will be necessary to check the value of the binary_file_patterns preference in the Preferences.sublime-settings file, and check each pattern against the resource file name with fnmatch to decide which API call to use.

Thanks for reporting the issue! I'll try to take a look. I've been pretty busy and haven't been able to keep the plugins updated. As always PR are welcome :)

I've had a go at this, but it looks like it is not possible to get ST to display an image that does not exist on the filesystem. Specifically, the view object only allows plugins to insert text (i.e. str type), not a bytearray.

I therefore think we will have to use a workaround. I have some ideas:

  1. extract the image and delete it again when the user closes the tab. (I was originally thinking only to delete it if it wasn't manually saved by the user, so that it would stay as an override file in that circumstance, but the save command is actually disabled for image preview in ST3.)
  2. take the idea of https://github.com/bordaigorl/sublime-non-text-files and open the image in the default application, i.e. external to ST. Again it would require extracting it, but now we can't tell when it was closed to know when to delete it, so IMO it's not a good solution.
  3. use ST3's phantom functionality to display the image - ST supports using a datauri in the HTML image src (i.e. base64 encoded), as seen here: https://github.com/SublimeText/LaTeXTools/blob/6c540b005df6f02336d1e347c160dc24c390b474/st_preview/preview_math.py#L312

What do you think? I heard that ST2 doesn't support image preview, so probably we're looking at an ST3 specific implementation anyway.

Thanks for taking a look at this, I'll take a closer look at the links you've provided. Yes, as far as I know, I'll have to create a branch for ST2 and ST3. No big deal there though, just something I need to do (for multiple of my plugins). It's great to see more functionality being added to ST3 though. Definitely felt like it stalled there for a while.