def get_content_type(self):
"""Get MIME content file type (example: image/jpeg).
@return: file content type.
"""
file_type = None
if HAVE_MAGIC:
try:
ms = magic.open(magic.MAGIC_MIME)
ms.load()
file_type = ms.file(self.file_path)
except:
try:
file_type = magic.from_file(self.file_path, mime=True)
except:
pass
finally:
try:
ms.close()
except:
pass
if file_type is None:
try:
args = ["file", "-b", "--mime-type", self.file_path]
file_type = subprocess.check_output(args).strip()
except:
pass
return file_type
评论列表
文章目录