ahupp / python-magic

A python wrapper for libmagic
Other
2.6k stars 280 forks source link

Default / unknown mimetype? #252

Closed jtlz2 closed 2 years ago

jtlz2 commented 2 years ago

I am using this excellent module as follows:

import magic
from io import BytesIO
inferred_mimetype = magic.from_buffer(BytesIO(bytes_data).read(2048), mime=True)

If bytes_data is unrecognisable, or mangled, what is returned by .from_buffer()?

What possible exceptions are thrown, or Nones returned?

What is the default?

Thank you so much

ahupp commented 2 years ago

The simple but not very useful answer is that python-magic is a thin wrapper around libmagic, so does whatever libmagic does :)

When from_buffer(..., mime=True) cannot identify the format it returns 'application/octet-stream'.

magic.from_buffer() could throw because:

a) libmagic is not found, this will produce an ImportError b) magic_load cannot find any magic database files, this will produce a MagicException

magic.from_buffer will never return None because a null return from from_buffer is turned into an exception.

In theory magic_buffer could fail but the docs are not descriptive about when/if this could happen; I've not see it except for a bug in libmagic 5.09.

Hope this helps!