diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-01-21 16:01:37 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-01-21 16:04:54 +0100 |
commit | 5eff7b964ee0557e9a5d12fcf4b15d06555a0ce5 (patch) | |
tree | 29239cbc6fdd7665697f0c9fc3697dc582db77c3 | |
parent | 5652807c915a6266ed9e6ad8d347634c64909d40 (diff) |
db/message: guess a better content type for octet-stream parts
Same as what is done for Attachment.
-rw-r--r-- | alot/db/message.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/alot/db/message.py b/alot/db/message.py index cb72b2ad..a8590d9f 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -106,6 +106,12 @@ class _MessageHeaders: class _MimeTree: _part = None + # content-type: (maintype, subtype) + # may differ from that indicated in _part, + # e.g. when it reports octet-stream, but we guess + # something more specific + _ctype_val = None + is_signed = False is_encrypted = False sig_valid = None @@ -181,14 +187,30 @@ class _MimeTree: return self._part.as_bytes() @property + def _ctype(self): + if self._ctype_val is None: + ctype = self._part.get_content_type() + + # replace underspecified mime description by a better guess + if ctype in ['octet/stream', 'application/octet-stream', + 'application/octetstream']: + ctype = helper.guess_mimetype(self._part.get_content()) + logging.debug('Overriding octet-stream content type to %s', ctype) + + maintype, _, subtype = ctype.partition('/') + self._ctype_val = (maintype, subtype) + + return self._ctype_val + + @property def content_type(self): - return self._part.get_content_type() + return '/'.join(self._ctype) @property def content_maintype(self): - return self._part.get_content_maintype() + return self._ctype[0] @property def content_subtype(self): - return self._part.get_content_subtype() + return self._ctype[1] @property def filename(self): |