Skip to content

Commit 800c8df

Browse files
committed
Fix filename created from mail attachments
Fixes issue2551118
1 parent c3e1249 commit 800c8df

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Fixed:
7272
Fixes issue2551114.
7373
- issue2551108 - fix handling of designator links when formatted
7474
as markdown links. (Reported by Cedric Krier; John Rouillard)
75+
- Fix filename created from mail attachments, fixes issue2551118
7576

7677
Features:
7778
- issue2550522 - Add 'filter' command to command-line

roundup/mailgw.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,23 @@ def text_as_attachment(self):
397397
return part.as_attachment()
398398
return None
399399

400+
def get_filename(self):
401+
""" Note: The get_filename of the Message class returns just the
402+
encoded header as transmitted via email. We make an attempt
403+
here to decode the information returned and return the real
404+
filename here.
405+
"""
406+
# This should really use super() but doesn't work with python2
407+
# because it seems that email.message.Message isn't a new-style
408+
# class in python2
409+
fn = email.message.Message.get_filename(self)
410+
if not fn :
411+
return fn
412+
h = []
413+
for x, t in decode_header(fn):
414+
h.append(x.decode(t) if t else x)
415+
return ''.join(h)
416+
400417
def as_attachment(self):
401418
"""Return this message as an attachment."""
402419
filename = self.get_filename()

test/test_mailgw.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,37 @@ def testNewIssueNoAuthorEmail(self):
697697
This is a file containing text
698698
in latin-1 format =E4=F6=FC=C4=D6=DC=DF
699699
700+
--uh56ypi7view24rr--
701+
'''
702+
703+
filename_msg = '''From: mary <[email protected]>
704+
705+
Message-Id: <followup_dummy_id>
706+
In-Reply-To: <dummy_test_message_id>
707+
Subject: [issue1] Testing...
708+
Content-Type: multipart/mixed; boundary="uh56ypi7view24rr"
709+
Content-Disposition: inline
710+
711+
--uh56ypi7view24rr
712+
Content-Type: text/plain; charset=us-ascii
713+
Content-Disposition: inline
714+
715+
Attach a file with non-ascii characters in it (encoded latin-1 should
716+
make it as-is through roundup due to Content-Type
717+
application/octet-stream)
718+
--
719+
Ralf Schlatterbeck email: [email protected]
720+
721+
--uh56ypi7view24rr
722+
Content-Type: application/octet-stream
723+
Content-Disposition: attachment;
724+
filename==?iso-8859-1?Q?20210312=5FM=FCnchen=5FRepor?=
725+
=?iso-8859-1?Q?t.pdf?=
726+
Content-Transfer-Encoding: quoted-printable
727+
728+
This is a file containing text
729+
in latin-1 format =E4=F6=FC=C4=D6=DC=DF
730+
700731
--uh56ypi7view24rr--
701732
'''
702733

@@ -845,6 +876,22 @@ def testOctetStreamTranscoding(self):
845876
names = {0 : 'testfile'}
846877
content = [b'''This is a file containing text
847878
in latin-1 format \xE4\xF6\xFC\xC4\xD6\xDC\xDF
879+
''']
880+
for n, id in enumerate (msg.files):
881+
f = self.db.file.getnode (id)
882+
self.assertEqual(f.name, names.get (n, 'unnamed'))
883+
self.assertEqual(f.binary_content, content [n])
884+
885+
def testFileAttachWithUmlaut(self):
886+
self.doNewIssue()
887+
self._handle_mail(self.filename_msg)
888+
messages = self.db.issue.get('1', 'messages')
889+
messages.sort()
890+
msg = self.db.msg.getnode (messages[-1])
891+
assert(len(msg.files) == 1)
892+
names = {0 : u'20210312_M\xfcnchen_Report.pdf'}
893+
content = [b'''This is a file containing text
894+
in latin-1 format \xE4\xF6\xFC\xC4\xD6\xDC\xDF
848895
''']
849896
for n, id in enumerate (msg.files):
850897
f = self.db.file.getnode (id)

0 commit comments

Comments
 (0)