Skip to content

Bug: Using fabio._openimage with an IO buffer. #612

@mattgebert

Description

@mattgebert

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! :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions