本文整理汇总了Python中magic.from_buffer方法的典型用法代码示例。如果您正苦于以下问题:Python magic.from_buffer方法的具体用法?Python magic.from_buffer怎么用?Python magic.from_buffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类magic
的用法示例。
在下文中一共展示了magic.from_buffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: upload_audio
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def upload_audio(self, file_id, mime):
if not self.file_store:
return None, "No file store available"
filedata = self.download_file(file_id)
if filedata is None:
return None, "teleboto Faild to download file"
if mime is None:
mime = magic.from_buffer(filedata, mime=True).decode('utf-8')
ext = mimetypes.guess_extension(mime)
if ext is None:
raise Exception("Failed to guess ext from mime: %s" % mime)
filename = "voice" + ext
url = self.file_store.upload_file(filedata, filename, filetype="audio")
if url is None:
return None, "Failed to upload Document"
return url, None
示例2: get_filetype
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def get_filetype(data):
"""There are two versions of python-magic floating around, and annoyingly, the interface
changed between versions, so we try one method and if it fails, then we try the other.
NOTE: you may need to alter the magic_file for your system to point to the magic file."""
if sys.modules.has_key('magic'):
try:
ms = magic.open(magic.MAGIC_NONE)
ms.load()
return ms.buffer(data)
except:
try:
return magic.from_buffer(data)
except magic.MagicException:
magic_custom = magic.Magic(magic_file='C:\windows\system32\magic')
return magic_custom.from_buffer(data)
return ''
示例3: run
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def run(self, directory):
for root, dirs, files in os.walk(directory, followlinks=True):
for name in files:
filename = os.path.join(root, name)
try:
file_type = magic.from_buffer(open(filename).read(1024))
except:
log("Error reading file %s: %s" % (filename, str(sys.exc_info()[1])))
continue
if is_executable(file_type):
md5_hash = md5(open(filename, "rb").read()).hexdigest()
if not self.is_file_indexed(md5_hash):
self.do_run(filename, file_type)
else:
log("File already indexed %s" % name)
#-------------------------------------------------------------------------------
示例4: _get_filetype
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def _get_filetype(self, data):
"""Gets filetype, uses libmagic if available.
@param data: data to be analyzed.
@return: file type or None.
"""
if not HAVE_MAGIC:
return None
try:
ms = magic.open(magic.MAGIC_NONE)
ms.load()
file_type = ms.buffer(data)
except:
try:
file_type = magic.from_buffer(data)
except Exception:
return None
finally:
try:
ms.close()
except:
pass
return file_type
示例5: get_type
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def get_type(data):
try:
ms = magic.open(magic.MAGIC_NONE)
ms.load()
file_type = ms.buffer(data)
except:
try:
file_type = magic.from_buffer(data)
except:
return ''
finally:
try:
ms.close()
except:
pass
return file_type
示例6: do_import
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def do_import(self, results, url):
response = requests.get(url, proxies=yeti_config.proxy)
content_type = magic.from_buffer(response.content, mime=True)
if content_type == "text/html":
import_html(results, response.content)
self.save_as_pdf(results, url)
else:
target = AttachedFile.from_content(
StringIO(response.content), url, content_type)
results.investigation.update(import_document=target)
try:
method = ImportMethod.objects.get(acts_on=content_type)
method.do_import(results, target.filepath)
except:
raise ValueError(
"unsupported file type: '{}'".format(content_type))
示例7: extract_comments_from_str
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def extract_comments_from_str(code, mime=None):
"""Extracts and returns comments from the given source string.
Args:
code: String containing code to extract comments from.
mime: Optional MIME type for code (str). Note some MIME types accepted
don't comply with RFC2045. If not given, an attempt to deduce the
MIME type will occur.
Returns:
Python list of parsers.common.Comment in the order that they appear in
the source code.
Raises:
UnsupportedError: If code is of an unsupported MIME type.
"""
if not mime:
mime = magic.from_buffer(code, mime=True)
if isinstance(mime, bytes):
mime = mime.decode('utf-8')
if mime not in MIME_MAP:
raise UnsupportedError('Unsupported MIME type %s' % mime)
try:
parser = MIME_MAP[mime]
return parser.extract_comments(code)
except common.Error as e:
raise ParseError(str(e))
示例8: iterHashes
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def iterHashes(self):
'''
Iterate over all the files in the archive, and yield 2-tuples
containing the internal-path and the internal-item-data as a dict
'''
items = list(self.getFileList())
items.sort()
for item in items:
if item not in self.hashedFiles:
fp = self.open(item)
cont = fp.read()
ret = hf.getHashDict(item, cont)
ret['cont'] = cont
ret['type'] = fix_mime(magic.from_buffer(cont, mime=True))
self.hashedFiles[item] = ret
yield item, self.hashedFiles[item]
示例9: file_type
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def file_type(buf):
import magic
return magic.from_buffer(buf, mime=True)
示例10: load_index
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def load_index(self):
try:
resp = self.boto.get_object(Bucket=self.bucket, Key=self.index_path())
body = resp["Body"].read()
content_type = magic.from_buffer(body, mime=True)
if content_type in ("application/json", "text/plain"):
logger.debug("Detected %s encoding for index", content_type)
return json.loads(body.decode("utf-8"))
# the magic/file command reports gzip differently depending on its version
elif content_type in ("application/x-gzip", "application/gzip"):
logger.debug("Detected gzip encoding for index")
body = gzip.decompress(body)
return json.loads(body.decode("utf-8"))
elif content_type in ("application/zlib",):
logger.debug("Detected zlib encoding for index")
body = zlib.decompress(body)
return json.loads(body.decode("utf-8"))
# Older versions of Ubuntu and some versions of MAC cannot
# do not detect the file type correctly. In this case we need
# to try both gzip and zlib decompression
elif content_type in ("application/octet-stream",):
logger.debug("Cannot detect encoding for index - trying all")
body = utils.try_decompress(body)
return json.loads(body.decode("utf-8"))
elif content_type == "application/x-empty":
return {}
else:
raise ValueError("Unknown content type for index", content_type)
except (ClientError):
return {}
示例11: _get_file_magic_name
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def _get_file_magic_name(self, buffer):
"""
Return the filetype guessed for a buffer
:param buffer: bytes
:return: str of filetype
"""
default = "Unknown"
ftype = None
try:
# Magic is optional
import magic
except ImportError:
return default
try:
# There are several implementations of magic,
# unfortunately all called magic
# We use this one: https://github.com/ahupp/python-magic/
getattr(magic, "MagicException")
except AttributeError:
# Looks like no magic was installed
return default
try:
ftype = magic.from_buffer(buffer[:1024])
except magic.MagicError as e:
log.exception("Error getting the magic type!")
return default
if not ftype:
return default
else:
return self._patch_magic(buffer, ftype)
示例12: is_packet_capture
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def is_packet_capture(bytes):
"""
:param bytes: raw bytes
:return: True is valid pcap or pcapng file
"""
result = from_buffer(bytes)
valid = "pcap-ng" in result or "tcpdump" in result or "NetMon" in result or 'pcap capture file' in result
return valid
示例13: sync_avatar_from_ldap
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def sync_avatar_from_ldap(self, user: UserProfile, ldap_user: _LDAPUser) -> None:
if 'avatar' in settings.AUTH_LDAP_USER_ATTR_MAP:
# We do local imports here to avoid import loops
from io import BytesIO
from zerver.lib.actions import do_change_avatar_fields
from zerver.lib.upload import upload_avatar_image
avatar_attr_name = settings.AUTH_LDAP_USER_ATTR_MAP['avatar']
if avatar_attr_name not in ldap_user.attrs: # nocoverage
# If this specific user doesn't have e.g. a
# thumbnailPhoto set in LDAP, just skip that user.
return
ldap_avatar = ldap_user.attrs[avatar_attr_name][0]
avatar_changed = is_avatar_new(ldap_avatar, user)
if not avatar_changed:
# Don't do work to replace the avatar with itself.
return
io = BytesIO(ldap_avatar)
# Structurally, to make the S3 backend happy, we need to
# provide a Content-Type; since that isn't specified in
# any metadata, we auto-detect it.
content_type = magic.from_buffer(copy.deepcopy(io).read()[0:1024], mime=True)
if content_type.startswith("image/"):
upload_avatar_image(io, user, user, content_type=content_type)
do_change_avatar_fields(user, UserProfile.AVATAR_FROM_USER)
# Update avatar hash.
user.avatar_hash = user_avatar_content_hash(ldap_avatar)
user.save(update_fields=["avatar_hash"])
else:
logging.warning("Could not parse %s field for user %s",
avatar_attr_name, user.id)
示例14: __init__
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def __init__(self):
for key in self.magicsignatures.keys():
self.magicsignatures[key] = re.compile(self.magicsignatures[key])
for key in self.signatures.keys():
self.signatures[key] = re.compile(self.signatures[key])
try:
import magic
self.getfmime = magic.from_file
self.getbmime = magic.from_buffer
except:
print "python-magic is not installed (or wrong version)"
self.gotmagic = False
示例15: save_file
# 需要导入模块: import magic [as 别名]
# 或者: from magic import from_buffer [as 别名]
def save_file(uploaded_file, filename=None):
value = "FILE:{}".format(stream_sha256(uploaded_file))
mime_type = magic.from_buffer(uploaded_file.read(100), mime=True)
uploaded_file.seek(0)
body = AttachedFile.from_upload(uploaded_file, force_mime=mime_type)
f = observables.File.get_or_create(
value=value, body=body, mime_type=mime_type)
if not filename:
filename = uploaded_file.filename
if filename not in f.filenames:
f.filenames.append(filename)
return f.save()