From 77f7eb60154aa562df3301219eb24a38777dc981 Mon Sep 17 00:00:00 2001 From: Russell Jones Date: Mon, 19 Jan 2015 16:40:33 -0800 Subject: [PATCH] Added support for overriding heuristic content type discovery for tricky content types like bzip2. --- flanker/mime/message/part.py | 21 +++++++++++++++++++++ setup.py | 2 +- tests/__init__.py | 1 + tests/fixtures/messages/bz2-attachment.eml | 21 +++++++++++++++++++++ tests/mime/message/part_test.py | 9 ++++++++- 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/messages/bz2-attachment.eml diff --git a/flanker/mime/message/part.py b/flanker/mime/message/part.py index 9e248df..166d920 100644 --- a/flanker/mime/message/part.py +++ b/flanker/mime/message/part.py @@ -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): diff --git a/setup.py b/setup.py index 5f92404..d4135a3 100644 --- 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=[], diff --git a/tests/__init__.py b/tests/__init__.py index 7378f68..a666e42 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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 index 0000000..f83a70d --- /dev/null +++ b/tests/fixtures/messages/bz2-attachment.eml @@ -0,0 +1,21 @@ +Content-Type: multipart/mixed; boundary="9ca4f9e3b740436bb761d3f9aae5fae9" +Mime-Version: 1.0 +From: Alice +To: Bob +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-- diff --git a/tests/mime/message/part_test.py b/tests/mime/message/part_test.py index 4b7a54b..b70e387 100644 --- a/tests/mime/message/part_test.py +++ b/tests/mime/message/part_test.py @@ -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) -- 2.43.0