Skip to content

Commit

Permalink
Merge pull request #76 from mailgun/rjones/content-type-override
Browse files Browse the repository at this point in the history
Override tricky content types.
  • Loading branch information
russjones committed Jan 26, 2015
2 parents e230882 + 77f7eb6 commit 45a30ca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
21 changes: 21 additions & 0 deletions flanker/mime/message/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


setup(name='flanker',
version='0.4.26',
version='0.4.27',
description='Mailgun Parsing Tools',
long_description=open('README.rst').read(),
classifiers=[],
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def skip_if_asked():
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()
Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/messages/bz2-attachment.eml
Original file line number Diff line number Diff line change
@@ -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--
9 changes: 8 additions & 1 deletion tests/mime/message/part_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 45a30ca

Please sign in to comment.