From c29d7c6e5ba098b6ac03e917c757f6257a522c11 Mon Sep 17 00:00:00 2001 From: Cornelius Ludmann Date: Wed, 2 Aug 2017 13:28:43 +0200 Subject: [PATCH] Decode base64 encoded e-mail attachment filename. According to RFC 2231 the filename of e-mail attachments can be encoded, e.g. with base64 (for example, the Brother ADS2800W Scanner does this). Here an example how it looks like: Content-Disposition: attachment; filename="=?UTF-8?B?QlJXMjg1NjVBOEI5RkQyXzIwMTcwNTIzXzIzMDMzN18wMDAzMDMucGRm?=" Signed-off-by: Cornelius Ludmann --- mayan/apps/sources/models.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mayan/apps/sources/models.py b/mayan/apps/sources/models.py index 6f71a40165..5b28336e08 100644 --- a/mayan/apps/sources/models.py +++ b/mayan/apps/sources/models.py @@ -617,6 +617,12 @@ class EmailBaseModel(IntervalBaseModel): if raw_filename: filename = collapse_rfc2231_value(raw_filename) + + # Decode base64 encoded filename + # https://stackoverflow.com/a/21859258/1364435 + if decode_header(filename)[0][1] is not None: + filename = str(decode_header(filename)[0][0]).decode(decode_header(filename)[0][1]) + else: filename = _('attachment-%i') % counter counter += 1