# HG changeset patch # User Thierry Florac # Date 1495324216 -7200 # Node ID 47dd5b37fab5ea182d95377d72a8723079ea94f5 # Parent bd1f5624dce86f4c41987d4f464cc89099e2d94e Added support for several 'libmagic' packages diff -r bd1f5624dce8 -r 47dd5b37fab5 src/pyams_file/file.py --- a/src/pyams_file/file.py Tue Dec 27 15:40:04 2016 +0100 +++ b/src/pyams_file/file.py Sun May 21 01:50:16 2017 +0200 @@ -196,13 +196,21 @@ def get_magic_content_type(input): - """Get content-type based on magic library as *bytes*""" + """Get content-type based on magic library as *bytes* + + As libmagic bindings are provided via several 'magic' packages, we try them in order + """ if magic is not None: if hasattr(input, 'seek'): input.seek(0) if hasattr(input, 'read'): input = input.read() - return magic.from_buffer(input, mime=True) + if hasattr(magic, 'detect_from_content'): + result = magic.detect_from_content(input) + if result: + return result.mime_type.encode() + elif hasattr(magic, 'from_buffer'): + return magic.from_buffer(input, mime=True) else: return None