From 1d1db50a9fa66b551152aa9353dd36b2aa870e30 Mon Sep 17 00:00:00 2001 From: Joe Steele Date: Sun, 1 Sep 2013 16:24:16 -0400 Subject: [PATCH] Don't always base64 encode in BinaryTempFileBody.writeTo Issue 5734 exemplifies the problem: receive a message with an attachment of type message/rfc822 that doesn't use base64 encoding for the body of the attached message. K-9 Mail incorrectly stores the attached message locally with its original headers but using base64 encoding for the body. A discrepancy thus exists between what the headers say about the encoding of the body versus the actual encoding used. This is obvious when attempting to view the attachment (either by using a compatible message viewer available on the device or by saving the attachment to a file and viewing the file contents). The process: When a message with an attached sub-message is received, Message.parse puts the attachment in a new MimeMessage with the attachment's body in a BinaryTempFileBody. LocalFolder.saveAttachment then calls Message.writeTo (which later calls BinaryTempFileBody.writeTo) to place the entire attachment (headers and body) in a new file that will become a LocalAttachmentBody. Until now, BinaryTempFileBody.writeTo could only save the message body using base64 encoding. This commit implements BinaryTempFileBody.setEncoding and assures that the body is written out with the same encoding that was found in its headers. --- .../k9/mail/internet/BinaryTempFileBody.java | 23 ++++++++++++++++--- .../fsck/k9/mail/internet/MimeUtility.java | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java b/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java index edfae939c..244998a70 100644 --- a/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java +++ b/src/com/fsck/k9/mail/internet/BinaryTempFileBody.java @@ -4,6 +4,8 @@ import com.fsck.k9.mail.Body; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.filter.Base64OutputStream; import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.codec.QuotedPrintableOutputStream; +import org.apache.james.mime4j.util.MimeUtil; import java.io.*; @@ -18,10 +20,16 @@ public class BinaryTempFileBody implements Body { private File mFile; + private String mEncoding = null; + public static void setTempDirectory(File tempDirectory) { mTempDirectory = tempDirectory; } + public void setEncoding(String encoding) { + mEncoding = encoding; + } + public BinaryTempFileBody() { if (mTempDirectory == null) { throw new @@ -44,13 +52,22 @@ public class BinaryTempFileBody implements Body { } public void writeTo(OutputStream out) throws IOException, MessagingException { + boolean closeStream = false; InputStream in = getInputStream(); + if (MimeUtil.isBase64Encoding(mEncoding)) { + out = (OutputStream) new Base64OutputStream(out); + closeStream = true; + } else if (MimeUtil.isQuotedPrintableEncoded(mEncoding)){ + out = new QuotedPrintableOutputStream(out, false); + closeStream = true; + } try { - Base64OutputStream base64Out = new Base64OutputStream(out); try { - IOUtils.copy(in, base64Out); + IOUtils.copy(in, out); } finally { - base64Out.close(); + if (closeStream) { + out.close(); + } } } finally { in.close(); diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index f231affb6..8605da2ea 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -1172,6 +1172,7 @@ public class MimeUtility { } BinaryTempFileBody tempBody = new BinaryTempFileBody(); + tempBody.setEncoding(contentTransferEncoding); OutputStream out = tempBody.getOutputStream(); try { IOUtils.copy(in, out);