src/pyams_file/file.py
changeset 34 47dd5b37fab5
parent 21 43c056a88f08
child 35 57b510d3cbe4
equal deleted inserted replaced
33:bd1f5624dce8 34:47dd5b37fab5
   194 class AudioFile(File):
   194 class AudioFile(File):
   195     """Audio file persistent object"""
   195     """Audio file persistent object"""
   196 
   196 
   197 
   197 
   198 def get_magic_content_type(input):
   198 def get_magic_content_type(input):
   199     """Get content-type based on magic library as *bytes*"""
   199     """Get content-type based on magic library as *bytes*
       
   200     
       
   201     As libmagic bindings are provided via several 'magic' packages, we try them in order
       
   202     """
   200     if magic is not None:
   203     if magic is not None:
   201         if hasattr(input, 'seek'):
   204         if hasattr(input, 'seek'):
   202             input.seek(0)
   205             input.seek(0)
   203         if hasattr(input, 'read'):
   206         if hasattr(input, 'read'):
   204             input = input.read()
   207             input = input.read()
   205         return magic.from_buffer(input, mime=True)
   208         if hasattr(magic, 'detect_from_content'):
       
   209             result = magic.detect_from_content(input)
       
   210             if result:
       
   211                 return result.mime_type.encode()
       
   212         elif hasattr(magic, 'from_buffer'):
       
   213             return magic.from_buffer(input, mime=True)
   206     else:
   214     else:
   207         return None
   215         return None
   208 
   216 
   209 
   217 
   210 def FileFactory(data):
   218 def FileFactory(data):