From: Sergey Obukhov Date: Tue, 1 Dec 2015 01:31:03 +0000 (-0800) Subject: fix file name for inline attachments X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=af05df03c40bcf50fcc904c783ae1ead7f70f939;p=flanker.git fix file name for inline attachments --- diff --git a/flanker/mime/message/part.py b/flanker/mime/message/part.py index 4e8c417..6af11eb 100644 --- a/flanker/mime/message/part.py +++ b/flanker/mime/message/part.py @@ -261,7 +261,7 @@ class RichPartMixin(object): file_name = ctype.params.get('name', '') or ctype.params.get('filename', '') value, params = self.content_disposition - if value == 'attachment': + if value in ['attachment', 'inline']: file_name = params.get('filename', '') or file_name # filenames can be presented as tuples, like: diff --git a/tests/mime/message/part_test.py b/tests/mime/message/part_test.py index abdce34..cc197b6 100644 --- a/tests/mime/message/part_test.py +++ b/tests/mime/message/part_test.py @@ -16,6 +16,7 @@ from tests import (BILINGUAL, BZ2_ATTACHMENT, ENCLOSED, TORTURE, TORTURE_PART, SPAM_BROKEN_CTYPE, BOUNCE, NDN, NO_CTYPE, RELATIVE, MULTI_RECEIVED_HEADERS, OUTLOOK_EXPRESS) from tests.mime.message.scanner_test import TORTURE_PARTS, tree_to_string +from flanker.mime import recover def readonly_immutability_test(): @@ -411,6 +412,7 @@ def content_types_test(): eq_('image/png', attachment.detected_content_type) eq_('png', attachment.detected_subtype) eq_('image', attachment.detected_format) + eq_('mailgun.png', attachment.detected_file_name) ok_(not attachment.is_body()) part = scan(BZ2_ATTACHMENT) @@ -421,6 +423,53 @@ def content_types_test(): ok_(not attachment.is_body()) +def test_attachments(): + """Content-Type and file name are properly detected + """ + # pdf attachment, file name in Content-Disposition + data = """Content-Type: application/octet-stream; name="J_S III_W-2.pdf" +Content-Disposition: attachment; filename*="J_S III_W-2.pdf" +Content-Transfer-Encoding: base64 + +YmxhaGJsYWhibGFo +""" + part = scan(data) + eq_('application/pdf', part.detected_content_type) + eq_('J_S III_W-2.pdf', part.detected_file_name) + + # no attachment + data = """Content-Type: text; charset=ISO-8859-1 + +some plain text here +""" + part = scan(data) + eq_('text/plain', part.detected_content_type) + eq_('', part.detected_file_name) + + # inline attachment, file name in Content-Disposition + data = """Content-Type: image/png +Content-Transfer-Encoding: base64 +Content-Disposition: inline; filename=2.png +Content-ID: + +YmxhaGJsYWhibGFo +""" + part = scan(data) + eq_('image/png', part.detected_content_type) + eq_('2.png', part.detected_file_name) + + # broken mime + data = """Content-Type: Нельзя это распарсить адекватно" +Content-Disposition: Да и это тоже +Content-Transfer-Encoding: base64 + +YmxhaGJsYWhibGFo +""" + part = recover(data) + eq_('text/plain', part.detected_content_type) + eq_('', part.detected_file_name) + + def test_is_body(): part = scan(IPHONE) ok_(part.parts[0].is_body())