MIME type utils method now compatible with multiple version of python-magic

This commit is contained in:
Fabio Manganiello 2019-06-21 13:40:45 +02:00
parent 4cd2e6949f
commit 3bd9bec660

View file

@ -224,7 +224,13 @@ def get_mime_type(resource):
with urllib.request.urlopen(resource) as response: with urllib.request.urlopen(resource) as response:
return response.info().get_content_type() return response.info().get_content_type()
else: else:
if hasattr(magic, 'detect_from_filename'):
mime = magic.detect_from_filename(resource) mime = magic.detect_from_filename(resource)
elif hasattr(magic, 'from_file'):
mime = magic.from_file(resource, mime=True)
else:
raise RuntimeError('The installed magic version provides neither detect_from_filename nor from_file')
if mime: if mime:
return mime.mime_type return mime.mime_type