fabio icon indicating copy to clipboard operation
fabio copied to clipboard

Bug: Using `fabio._openimage` with an IO buffer.

Open mattgebert opened this issue 5 months ago • 1 comments

The Problem:

If I use a ByteIO buffer to serve fabio at TIF file (i.e. "marccd/tif"), then the fabio.openimage._openimage function uses actual_filename as the buffer, and the original buffer as filename when serving to do_magic (which uses the header bytes of the file to decypher the filetype).

do_magic then fails at a filename.split call, which assumes that filename is only a string.

The use case:

I have files I want to serve and then process over a web server - I'd rather not be required to save them into local storage before pipelining the file stream into fabio.

Possible solutions:

  1. Add another optional argument in _openimage for a buffer, then filename can be used if a buffer is provided for a string name. Ideally we modify openimage to be able to handle buffers too.

  2. Looks to me like all the python interfaces for buffers have a name attribute. However this looks pretty precarious and I don't believe is documented on the io documentation, but this could be used i.e.:

     if hasattr(filename, "seek") and hasattr(filename, "read"):
             # Data stream without filename
             filename.seek(0)
             data = filename.read()
             actual_filename = BytesIO(data)
             if hasattr(filename, name) and filename.name:
                 filename = filename.name
             # Back to the location before the read
             filename.seek(0)
    
  3. (easiest?) Modify the logic in do_magic when considering elif format_type == "marccd/tif":, to not assume filename is string.

I'm happy to have a go at contributing if you don't mind and think any fix is worthwhile - keen to contribute more to open source scientific projects! :)

mattgebert avatar Jul 17 '25 05:07 mattgebert