def file_is(file_description, fmt):
"""Get if file stored in `file_path` is a `fmt` document.
:file_path: Full path for a `fmt` file or a buffer containing `fmt` data.
:returns: True if is `fmt` and False otherwise
"""
import magic
logger.debug("Checking filetype")
if isinstance(file_description, str):
# This means that the file_description is a string
result = re.match(
r".*%s.*" % fmt, magic.from_file(file_description, mime=True),
re.IGNORECASE
)
if result:
logger.debug(
"File %s appears to be of type %s" % (file_description, fmt)
)
elif isinstance(file_description, bytes):
# Suppose that file_description is a buffer
result = re.match(
r".*%s.*" % fmt, magic.from_buffer(file_description, mime=True)
)
if result:
logger.debug(
"Buffer appears to be of type %s" % (fmt)
)
return True if result else False
评论列表
文章目录