1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

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.
This commit is contained in:
Joe Steele 2013-09-01 16:24:16 -04:00
parent de23a0e3e1
commit 1d1db50a9f
2 changed files with 21 additions and 3 deletions

View File

@ -4,6 +4,8 @@ import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.filter.Base64OutputStream; import com.fsck.k9.mail.filter.Base64OutputStream;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.james.mime4j.codec.QuotedPrintableOutputStream;
import org.apache.james.mime4j.util.MimeUtil;
import java.io.*; import java.io.*;
@ -18,10 +20,16 @@ public class BinaryTempFileBody implements Body {
private File mFile; private File mFile;
private String mEncoding = null;
public static void setTempDirectory(File tempDirectory) { public static void setTempDirectory(File tempDirectory) {
mTempDirectory = tempDirectory; mTempDirectory = tempDirectory;
} }
public void setEncoding(String encoding) {
mEncoding = encoding;
}
public BinaryTempFileBody() { public BinaryTempFileBody() {
if (mTempDirectory == null) { if (mTempDirectory == null) {
throw new throw new
@ -44,13 +52,22 @@ public class BinaryTempFileBody implements Body {
} }
public void writeTo(OutputStream out) throws IOException, MessagingException { public void writeTo(OutputStream out) throws IOException, MessagingException {
boolean closeStream = false;
InputStream in = getInputStream(); 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 { try {
Base64OutputStream base64Out = new Base64OutputStream(out);
try { try {
IOUtils.copy(in, base64Out); IOUtils.copy(in, out);
} finally { } finally {
base64Out.close(); if (closeStream) {
out.close();
}
} }
} finally { } finally {
in.close(); in.close();

View File

@ -1172,6 +1172,7 @@ public class MimeUtility {
} }
BinaryTempFileBody tempBody = new BinaryTempFileBody(); BinaryTempFileBody tempBody = new BinaryTempFileBody();
tempBody.setEncoding(contentTransferEncoding);
OutputStream out = tempBody.getOutputStream(); OutputStream out = tempBody.getOutputStream();
try { try {
IOUtils.copy(in, out); IOUtils.copy(in, out);