From 609ab8064e77e5fbb5c54d1c4f20a8fed86f1f85 Mon Sep 17 00:00:00 2001 From: Justus Winter <4winter@informatik.uni-hamburg.de> Date: Sun, 25 Dec 2011 03:18:54 +0100 Subject: Avoid reading the file twice if we have to guess the content type --- alot/helper.py | 19 ++++++------------- 1 file 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 -- cgit v1.2.3