summaryrefslogtreecommitdiff
path: root/alot
diff options
context:
space:
mode:
authorJustus Winter <4winter@informatik.uni-hamburg.de>2011-12-25 03:18:54 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2011-12-25 15:18:18 +0000
commitd7494900d7406aba93db2954137f6748279a64a1 (patch)
treeaa1fdf66a9a28acee2d3a8582c7b639ab9a2002d /alot
parent6de4310b1f81692a7aa0ace95affae82c2b18e22 (diff)
Avoid reading the file twice if we have to guess the content type
Diffstat (limited to 'alot')
-rw-r--r--alot/helper.py19
1 files changed, 6 insertions, 13 deletions
diff --git a/alot/helper.py b/alot/helper.py
index 9a15bb5d..61a0ff37 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -287,26 +287,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