Support `io.BytesIO` as input
jams2 opened this issue ยท 7 comments
Hi,
would you be open to a pull request adding support for io.BytesIO as input? We use BytesIO to ease creating file-like objects for images in testing, but this is incompatible with filetype (TypeError: Unsupported type as file input: <class '_io.BytesIO'>), as there is no corresponding handling in utils.get_bytes.
Thanks
@jams2 I have fix this in fix: restore reader position after retrieving signature bytes
Hi @ferstar,
Although this fix addresses this specific issue, not all file-like objects inherit from io.BufferedIOBase; for instance, all file-like objects used in Django.
We have been using this package for a long time to determine uploaded file types, but after upgrading to version 1.1.0, we received the same TypeError. As a workaround, we just read the first 262 bytes and pass them to the guess function and after the new version is released, we can just pass the underlying file object. However, it will be just a specific workaround for django.
Can I ask about the reasoning behind this commit? Since the try clause didn't seek to the start position, I can see that it could introduce unexpected side-effects, but isn't it more useful to check if the object has read, tell, and seek attributes instead of checking inheritance from io.BufferedIOBase?
Thanks
@pooriazmn Thanks for pointing this out. Attribute judgment for read/seek/tell should be a better choice to be compatible with more object types. I have fixed it.
It should be working on v1.2.0, now available on PyPI:
https://pypi.org/project/filetype/
Thanks for pushing it into PyPI, @h2non ! It works perfectly. Cheers