From 5eff7b964ee0557e9a5d12fcf4b15d06555a0ce5 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Thu, 21 Jan 2021 16:01:37 +0100 Subject: db/message: guess a better content type for octet-stream parts Same as what is done for Attachment. --- alot/db/message.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'alot/db') 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 @@ -180,15 +186,31 @@ class _MimeTree: def raw_data(self): 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): -- cgit v1.2.3