Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f92da3af authored by Christian Frommeyer's avatar Christian Frommeyer
Browse files

Extracting local attachment classes from LocalStore to reduce file size.

parent 038fceab
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -107,9 +107,9 @@ import com.fsck.k9.mail.internet.MimeMultipart;
import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.mail.internet.TextBody;
import com.fsck.k9.mail.internet.TextBodyBuilder;
import com.fsck.k9.mail.store.local.LocalStore.LocalAttachmentBody;
import com.fsck.k9.mail.store.local.LocalStore.TempFileBody;
import com.fsck.k9.mail.store.local.LocalStore.TempFileMessageBody;
import com.fsck.k9.mail.store.local.LocalAttachmentBody;
import com.fsck.k9.mail.store.local.TempFileBody;
import com.fsck.k9.mail.store.local.TempFileMessageBody;
import com.fsck.k9.view.MessageWebView;

import org.apache.james.mime4j.codec.EncoderUtil;
+34 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.local;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.james.mime4j.util.MimeUtil;

import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeMessage;

public class AttachmentMessageBodyUtil {
    public static void writeTo(BinaryAttachmentBody body, OutputStream out) throws IOException,
            MessagingException {
        InputStream in = body.getInputStream();
        try {
            if (MimeUtil.ENC_7BIT.equalsIgnoreCase(body.getEncoding())) {
                /*
                 * If we knew the message was already 7bit clean, then it
                 * could be sent along without processing. But since we
                 * don't know, we recursively parse it.
                 */
                MimeMessage message = new MimeMessage(in, true);
                message.setUsing7bitTransport();
                message.writeTo(out);
            } else {
                IOUtils.copy(in, out);
            }
        } finally {
            in.close();
        }
    }
}
 No newline at end of file
+54 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.local;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.james.mime4j.codec.QuotedPrintableOutputStream;
import org.apache.james.mime4j.util.MimeUtil;

import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.filter.Base64OutputStream;

public abstract class BinaryAttachmentBody implements Body {
    protected String mEncoding;

    @Override
    public abstract InputStream getInputStream() throws MessagingException;

    @Override
    public void writeTo(OutputStream out) throws IOException, MessagingException {
        InputStream in = getInputStream();
        try {
            boolean closeStream = false;
            if (MimeUtil.isBase64Encoding(mEncoding)) {
                out = new Base64OutputStream(out);
                closeStream = true;
            } else if (MimeUtil.isQuotedPrintableEncoded(mEncoding)){
                out = new QuotedPrintableOutputStream(out, false);
                closeStream = true;
            }

            try {
                IOUtils.copy(in, out);
            } finally {
                if (closeStream) {
                    out.close();
                }
            }
        } finally {
            in.close();
        }
    }

    @Override
    public void setEncoding(String encoding) throws MessagingException {
        mEncoding = encoding;
    }

    public String getEncoding() {
        return mEncoding;
    }
}
 No newline at end of file
+37 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.local;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Application;
import android.net.Uri;

import com.fsck.k9.mail.MessagingException;

public class LocalAttachmentBody extends BinaryAttachmentBody {
    private Application mApplication;
    private Uri mUri;

    public LocalAttachmentBody(Uri uri, Application application) {
        mApplication = application;
        mUri = uri;
    }

    @Override
    public InputStream getInputStream() throws MessagingException {
        try {
            return mApplication.getContentResolver().openInputStream(mUri);
        } catch (FileNotFoundException fnfe) {
            /*
             * Since it's completely normal for us to try to serve up attachments that
             * have been blown away, we just return an empty stream.
             */
            return new ByteArrayInputStream(LocalStore.EMPTY_BYTE_ARRAY);
        }
    }

    public Uri getContentUri() {
        return mUri;
    }
}
 No newline at end of file
+49 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.store.local;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.james.mime4j.util.MimeUtil;

import android.app.Application;
import android.net.Uri;

import com.fsck.k9.mail.CompositeBody;
import com.fsck.k9.mail.MessagingException;

/**
 * A {@link LocalAttachmentBody} extension containing a message/rfc822 type body
 *
 */
public class LocalAttachmentMessageBody extends LocalAttachmentBody implements CompositeBody {

    public LocalAttachmentMessageBody(Uri uri, Application application) {
        super(uri, application);
    }

    @Override
    public void writeTo(OutputStream out) throws IOException, MessagingException {
        AttachmentMessageBodyUtil.writeTo(this, out);
    }

    @Override
    public void setUsing7bitTransport() throws MessagingException {
        /*
         * There's nothing to recurse into here, so there's nothing to do.
         * The enclosing BodyPart already called setEncoding(MimeUtil.ENC_7BIT).  Once
         * writeTo() is called, the file with the rfc822 body will be opened
         * for reading and will then be recursed.
         */

    }

    @Override
    public void setEncoding(String encoding) throws MessagingException {
        if (!MimeUtil.ENC_7BIT.equalsIgnoreCase(encoding)
                && !MimeUtil.ENC_8BIT.equalsIgnoreCase(encoding)) {
            throw new MessagingException(
                    "Incompatible content-transfer-encoding applied to a CompositeBody");
        }
        mEncoding = encoding;
    }
}
 No newline at end of file
Loading