content types like bzip2.
"""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(
return content_type
+def _guess_type(filename):
+ """
+ Internal content type guesser. This is used to hard code certain tricky content-types
+ that heuristic content type checker get wrong.
+ """
+
+ if filename.endswith(".bz2"):
+ return ContentType("application", "x-bzip2")
+
+ if filename.endswith(".gz"):
+ return ContentType("application", "x-gzip")
+
+ return None
+
+
class Body(object):
def __init__(
self, content_type, body, charset=None, disposition=None, filename=None):
setup(name='flanker',
- version='0.4.26',
+ version='0.4.27',
description='Mailgun Parsing Tools',
long_description=open('README.rst').read(),
classifiers=[],
fixture_file("messages/quoted-printable.eml")).read()
TEXT_ONLY = open(fixture_file("messages/text-only.eml")).read()
MAILGUN_PIC = open(fixture_file("messages/mailgun-pic.eml")).read()
+BZ2_ATTACHMENT = open(fixture_file("messages/bz2-attachment.eml")).read()
AOL_FBL = open(fixture_file("messages/complaints/aol.eml")).read()
YAHOO_FBL = open(fixture_file("messages/complaints/yahoo.eml")).read()
--- /dev/null
+Content-Type: multipart/mixed; boundary="9ca4f9e3b740436bb761d3f9aae5fae9"
+Mime-Version: 1.0
+From: Alice <alice@example.com>
+To: Bob <bob@example.com>
+Subject: hello
+
+--9ca4f9e3b740436bb761d3f9aae5fae9
+Mime-Version: 1.0
+Content-Type: text/plain; charset="ascii"
+Content-Transfer-Encoding: 7bit
+
+hello, world
+
+--9ca4f9e3b740436bb761d3f9aae5fae9
+Mime-Version: 1.0
+Content-Type: application/octet-stream; name="foo.txt.bz2"
+Content-Disposition: attachment; filename="foo.txt.bz2"
+Content-Transfer-Encoding: base64
+
+QlpoOTFBWSZTWQ0xSP4AAAFBgAAQMAAQACAAIZpoM00XPF3JFOFCQDTFI/g=
+--9ca4f9e3b740436bb761d3f9aae5fae9--
from flanker.mime.message.scanner import scan
from flanker.mime.message.errors import EncodingError, DecodingError
from flanker.mime.message.part import encode_transfer_encoding
-from tests import (BILINGUAL, ENCLOSED, TORTURE, TORTURE_PART,
+from tests import (BILINGUAL, BZ2_ATTACHMENT, ENCLOSED, TORTURE, TORTURE_PART,
ENCLOSED_BROKEN_ENCODING, EIGHT_BIT, QUOTED_PRINTABLE,
TEXT_ONLY, ENCLOSED_BROKEN_BODY, RUSSIAN_ATTACH_YAHOO,
MAILGUN_PIC, MAILGUN_PNG, MULTIPART, IPHONE,
eq_('image', attachment.detected_format)
ok_(not attachment.is_body())
+ part = scan(BZ2_ATTACHMENT)
+ attachment = part.parts[1]
+ eq_('application/x-bzip2', attachment.detected_content_type)
+ eq_('x-bzip2', attachment.detected_subtype)
+ eq_('application', attachment.detected_format)
+ ok_(not attachment.is_body())
+
def test_is_body():
part = scan(IPHONE)