def adjust_content_type(content_type, body=None, filename=None):
"""Adjust content type based on filename or body contents
"""
if filename and str(content_type) == 'application/octet-stream':
# check if our internal guess returns anything
guessed = _guess_type(filename)
if guessed:
return guessed
# our internal attempt didn't return anything, use mimetypes
guessed = mimetypes.guess_type(filename)[0]
if guessed:
main, sub = fix_content_type(
guessed, default=('application', 'octet-stream'))
content_type = ContentType(main, sub)
if content_type.main == 'image' and body:
sub = imghdr.what(None, body)
if sub:
content_type = ContentType('image', sub)
elif content_type.main == 'audio' and body:
sub = audio._whatsnd(body)
if sub:
content_type = ContentType('audio', sub)
return content_type
评论列表
文章目录