Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated imghdr, switch to stable email.mime API #265

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions flanker/_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ def decode_quoted_printable(val):
return email.quoprimime.header_decode(val)


def detect_audio_type(val):
return audio._whatsnd(val)


def make_message_id():
return make_msgid()

Expand Down
21 changes: 14 additions & 7 deletions flanker/mime/message/part.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import base64
import imghdr
import logging
import mimetypes
import quopri
from contextlib import closing
from os import path
from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio

import six
from six.moves import StringIO
Expand Down Expand Up @@ -113,14 +114,20 @@ def adjust_content_type(content_type, body=None, filename=None):
if six.PY3 and isinstance(body, six.text_type):
image_preamble = image_preamble.encode('utf-8', 'ignore')

sub = imghdr.what(None, image_preamble)
if sub:
content_type = ContentType('image', sub)
try:
mime = MIMEImage(image_preamble)
except TypeError:
pass
else:
content_type = ContentType(*mime.get_content_type().split('/'))

elif content_type.main == 'audio' and body:
sub = _email.detect_audio_type(body)
if sub:
content_type = ContentType('audio', sub)
try:
mime = MIMEAudio(body)
except TypeError:
pass
else:
content_type = ContentType(*mime.get_content_type().split('/'))

return content_type

Expand Down