pythonpython-magic

Why is magic.from_buffer returning None?


Here's what I get when I call magic.from_buffer:

>>> import urllib2
>>> data = urllib2.urlopen('http://www.in.gov/judiciary/opinions/previous/wpd/05040501.bed.doc').read()
>>> len(data)
29696
>>> from lib import magic
>>> magic.from_buffer(data, mime=True)

At this point, I should be provided with application/msword, but instead I get nothing from the last call. What am I missing?

This works on my dev machine, but fails on my server. I'm fairly baffled.


Solution

  • I'm not sure the reason for the issue (could be the version), but I've been able to workaround it with something like:

    mime = magic.from_buffer(data, mime=True)
    if mime == None:
        # Workaround for issue with libmagic1==5.09-2 in Ubuntu 12.04. Fixed in libmagic 5.11-2.
        file_str = magic.from_buffer(data)
        if file_str.startswith('Composite Document File V2 Document'):
            mime = 'application/msword'
    

    Not great, but gets the job done until it's possible to upgrade the server and get a new version of libmagic.