]> khamidou — GitHub mirrors - flanker.git/commitdiff
fix file name for inline attachments
authorSergey Obukhov <sergey.obykhov@mailgunhq.com>
Tue, 1 Dec 2015 01:31:03 +0000 (17:31 -0800)
committerSergey Obukhov <sergey.obykhov@mailgunhq.com>
Wed, 2 Dec 2015 00:50:19 +0000 (16:50 -0800)
flanker/mime/message/part.py
tests/mime/message/part_test.py

index 4e8c417d941ed9d05386e3b0d4b4bf4bb9309436..6af11eb8ba6261711777e9465c7d09f837d689b4 100644 (file)
@@ -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:
index abdce342fbd5180d357846c42ca63dfaf9bb8fc5..cc197b6203b8ddfc2aefe615d5c0635268c29c39 100644 (file)
@@ -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: <FN2JbofqmRT7zIMt8uzW>
+
+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())