diff options
author | Justus Winter <4winter@informatik.uni-hamburg.de> | 2011-12-25 03:18:54 +0100 |
---|---|---|
committer | Justus Winter <4winter@informatik.uni-hamburg.de> | 2011-12-25 03:36:00 +0100 |
commit | 609ab8064e77e5fbb5c54d1c4f20a8fed86f1f85 (patch) | |
tree | 1ba116eda21c0894c9f377c6c9c0c9396e850122 | |
parent | 34b5741c5e953adcaf367cad6167fa0944bd95e5 (diff) |
Avoid reading the file twice if we have to guess the content type
-rw-r--r-- | alot/helper.py | 19 |
1 files changed, 6 insertions, 13 deletions
diff --git a/alot/helper.py b/alot/helper.py index 8b28fa37..b12b7cb5 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -273,26 +273,19 @@ def guess_mimetype_of_path(path): def mimewrap(path, filename=None, ctype=None): - ctype = ctype or guess_mimetype_of_path(path) + content = open(path, 'rb').read() + ctype = ctype or guess_mimetype(content) maintype, subtype = ctype.split('/', 1) if maintype == 'text': - fp = open(path) # Note: we should handle calculating the charset - part = MIMEText(fp.read(), _subtype=subtype) - fp.close() + part = MIMEText(content, _subtype=subtype) elif maintype == 'image': - fp = open(path, 'rb') - part = MIMEImage(fp.read(), _subtype=subtype) - fp.close() + part = MIMEImage(content, _subtype=subtype) elif maintype == 'audio': - fp = open(path, 'rb') - part = MIMEAudio(fp.read(), _subtype=subtype) - fp.close() + part = MIMEAudio(content, _subtype=subtype) else: - fp = open(path, 'rb') part = MIMEBase(maintype, subtype) - part.set_payload(fp.read()) - fp.close() + part.set_payload(content) # Encode the payload using Base64 email.encoders.encode_base64(part) # Set the filename parameter |