]> khamidou — GitHub mirrors - flanker.git/commitdiff
Added support for overriding heuristic content type discovery for tricky
authorRussell Jones <rjones@mailgunhq.com>
Tue, 20 Jan 2015 00:40:33 +0000 (16:40 -0800)
committerRussell Jones <rjones@mailgunhq.com>
Mon, 26 Jan 2015 21:02:24 +0000 (21:02 +0000)
content types like bzip2.

flanker/mime/message/part.py
setup.py
tests/__init__.py
tests/fixtures/messages/bz2-attachment.eml [new file with mode: 0644]
tests/mime/message/part_test.py

index 9e248dfb37c25f1b76b44d9e79837a5c1d65fb08..166d920a43530d22de57504b32a592c7f66897d9 100644 (file)
@@ -91,6 +91,12 @@ 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(
@@ -110,6 +116,21 @@ def adjust_content_type(content_type, body=None, filename=None):
     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):
index 5f924041bd1d19c701fb0dd6625bd196f776df30..d4135a39de796240ccd9a4f8e62ed2116c0fef3f 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ from setuptools import setup, find_packages
 
 
 setup(name='flanker',
-      version='0.4.26',
+      version='0.4.27',
       description='Mailgun Parsing Tools',
       long_description=open('README.rst').read(),
       classifiers=[],
index 7378f685ae2595e3b7573e1542865d2fa353c74d..a666e428b5cc6f4406dbd43cd30c17abad71a7c5 100644 (file)
@@ -64,6 +64,7 @@ QUOTED_PRINTABLE = open(
     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()
diff --git a/tests/fixtures/messages/bz2-attachment.eml b/tests/fixtures/messages/bz2-attachment.eml
new file mode 100644 (file)
index 0000000..f83a70d
--- /dev/null
@@ -0,0 +1,21 @@
+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--
index 4b7a54b4667977eeb797cc6296c5341cdc0c1b1f..b70e3878f5d1fd6780bb3f22039d6eb42eeb5468 100644 (file)
@@ -9,7 +9,7 @@ from flanker.mime.create import multipart, text
 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,
@@ -389,6 +389,13 @@ def content_types_test():
     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)