本文整理汇总了Python中mimetypes.guess_all_extensions方法的典型用法代码示例。如果您正苦于以下问题:Python mimetypes.guess_all_extensions方法的具体用法?Python mimetypes.guess_all_extensions怎么用?Python mimetypes.guess_all_extensions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mimetypes
的用法示例。
在下文中一共展示了mimetypes.guess_all_extensions方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getFileExtensions
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def getFileExtensions(mime):
if(not mimetypes.inited):
mimetypes.init()
initKnowMimetypes()
types = mime.split(";")
result = []
for t in types:
if(t):
res = mimetypes.guess_all_extensions(t)
#print("getting extensions for mime " + str(t) + " " + str(res))
result.extend(res)
if(len(result) == 0):
result.append(".*")
return result
示例2: _relay_msg_image
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def _relay_msg_image(self, msg, conv_id):
filename = os.path.basename(msg.file)
logger.info("Uploading Slack image '{}' to Hangouts - {}".format(filename, json.dumps(msg.file)))
for retry_count in range(3):
try:
logger.debug("Attempt {} at downloading file".format(retry_count+1))
# Retrieve the image content from Slack.
resp = yield from Base.slacks[self.team].get(msg.file)
name_ext = "." + filename.rsplit(".", 1).pop().lower()
# Check the file extension matches the MIME type.
mime_type = resp.content_type
mime_exts = mimetypes.guess_all_extensions(mime_type)
if name_ext.lower() not in [ext.lower() for ext in mime_exts]:
raise ValueError("MIME '{}' does not match extension '{}', we probably didn't get the right file." +
" Attempt [{}/3]"
.format(mime_type, name_ext, retry_count+1))
image = yield from resp.read()
image_id = yield from self.bot._client.upload_image(BytesIO(image), filename=filename)
yield from self._relay_msg(msg, conv_id, image_id)
break
except ValueError as err:
logger.error(err)
yield from asyncio.sleep(2)
示例3: get_extension
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def get_extension(self, response):
mtype = response.headers.get("Content-Type", "image/jpeg")
mtype = mtype.partition(";")[0]
if "/" not in mtype:
mtype = "image/" + mtype
if mtype in MIMETYPE_MAP:
return MIMETYPE_MAP[mtype]
exts = mimetypes.guess_all_extensions(mtype, strict=False)
if exts:
exts.sort()
return exts[-1][1:]
self.log.warning(
"No filename extension found for MIME type '%s'", mtype)
return "txt"
示例4: from_field
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def from_field(cls, file_field, *, user, connection=None):
name = file_field.name
exts = mimetypes.guess_all_extensions(file_field.content_type)
for ext in exts:
if name.endswith(ext):
break
else:
if exts:
name += exts[-1]
name = await cls.app.loop.run_in_executor(
None, image_storage.save, name, file_field.file)
image_uuid = image_storage.uuid(name)
return await cls.create(
uuid=image_uuid,
image=name,
mime_type=file_field.content_type,
created_at=utils.now(),
author_id=user.pk,
connection=connection
)
示例5: _check_mimetype
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def _check_mimetype(self):
"""
Compare mimetype (as determined by libmagic) to extension.
Determine whether the extension that are normally associated with
the mimetype include the file's actual extension.
"""
if not self.has_mimetype:
self.make_dangerous('File has no mimetype')
else:
if self.mimetype in Config.aliases:
mimetype = Config.aliases[self.mimetype]
else:
mimetype = self.mimetype
expected_extensions = mimetypes.guess_all_extensions(mimetype,
strict=False)
if mimetype in Config.aliases:
expected_extensions += mimetypes.guess_all_extensions(Config.aliases[mimetype], strict=False)
if expected_extensions:
if self.has_extension and self.extension not in expected_extensions:
self.make_dangerous('Extension does not match expected extensions ({}) for this mimetype'.format(expected_extensions))
示例6: _get_tikaresults
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def _get_tikaresults(results, fname):
# Tika mime type is under Content-Type key.
# Take the content type and make a guess of the
# mimetype using python's built in mimetypes lib.
tikadict = dict(results)
try:
content_types = tikadict.get(fname, {}).get('Content-Type')
if not isinstance(content_types, list):
content_types = [content_types]
tika_extensions = []
for ctype in content_types:
tika_extensions += mimetypes.guess_all_extensions(ctype)
return list(set(tika_extensions))
except AttributeError:
return []
示例7: get_filter
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def get_filter(cls, mimetype):
"""
Returns a filter string for the file dialog. The filter is based
on the mime type.
:param mimetype: path from which the filter must be derived.
:return: Filter string
"""
filters = ' '.join(
['*%s' % ext for ext in mimetypes.guess_all_extensions(mimetype)])
return '%s (%s)' % (mimetype, filters)
示例8: from_url
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def from_url(cls, url, *, user, connection=None):
async with client.ClientSession() as session:
async with session.get(url) as response:
if response.status != 200:
return
l = response.content_length
if not l or l > 2 ** 23:
return
content_type = response.content_type
if not content_type.startswith('image'):
return
content = BytesIO(await response.read())
filename = response.url.name
exts = mimetypes.guess_all_extensions(content_type)
for ext in exts:
if filename.endswith(ext):
break
else:
if exts:
filename += exts[-1]
name = await cls.app.loop.run_in_executor(
None, image_storage.save, filename, content)
image_uuid = image_storage.uuid(name)
return await cls.create(
uuid=image_uuid,
image=name,
mime_type=content_type,
created_at=utils.now(),
author_id=user.pk,
connection=connection
)
示例9: __init__
# 需要导入模块: import mimetypes [as 别名]
# 或者: from mimetypes import guess_all_extensions [as 别名]
def __init__(self, src_path, dst_path):
''' Init file object, set the mimetype '''
super(File, self).__init__(src_path, dst_path)
self.is_recursive = False
if not self.has_mimetype():
# No mimetype, should not happen.
self.make_dangerous()
if self.is_dangerous():
return
self.log_details.update({'maintype': self.main_type,
'subtype': self.sub_type,
'extension': self.extension})
# If the mimetype matches as text/*, it will be sent to LibreOffice, no need to cross check the mime/ext
if self.main_type == 'text':
return
# Check correlation known extension => actual mime type
if propertype.get(self.extension) is not None:
expected_mimetype = propertype.get(self.extension)
else:
expected_mimetype, encoding = mimetypes.guess_type(self.src_path, strict=False)
if aliases.get(expected_mimetype) is not None:
expected_mimetype = aliases.get(expected_mimetype)
is_known_extension = self.extension in mimetypes.types_map.keys()
if is_known_extension and expected_mimetype != self.mimetype:
self.log_details.update({'expected_mimetype': expected_mimetype})
self.make_dangerous()
# check correlation actual mime type => known extensions
if aliases.get(self.mimetype) is not None:
mimetype = aliases.get(self.mimetype)
else:
mimetype = self.mimetype
expected_extensions = mimetypes.guess_all_extensions(mimetype, strict=False)
if expected_extensions:
if len(self.extension) > 0 and self.extension not in expected_extensions:
self.log_details.update({'expected_extensions': expected_extensions})
self.make_dangerous()
else:
# there are no known extensions associated to this mimetype.
pass