Search in sources :

Example 1 with SizeAware

use of com.fsck.k9.mail.internet.SizeAware in project k-9 by k9mail.

the class LocalFolder method leafPartToContentValues.

private File leafPartToContentValues(ContentValues cv, Part part, Body body) throws MessagingException, IOException {
    AttachmentViewInfo attachment = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part);
    cv.put("display_name", attachment.displayName);
    String encoding = getTransferEncoding(part);
    if (!(body instanceof SizeAware)) {
        throw new IllegalStateException("Body needs to implement SizeAware");
    }
    SizeAware sizeAwareBody = (SizeAware) body;
    long fileSize = sizeAwareBody.getSize();
    File file = null;
    int dataLocation;
    if (fileSize > MAX_BODY_SIZE_FOR_DATABASE) {
        dataLocation = DataLocation.ON_DISK;
        file = writeBodyToDiskIfNecessary(part);
        long size = decodeAndCountBytes(file, encoding, fileSize);
        cv.put("decoded_body_size", size);
    } else {
        dataLocation = DataLocation.IN_DATABASE;
        byte[] bodyData = getBodyBytes(body);
        cv.put("data", bodyData);
        long size = decodeAndCountBytes(bodyData, encoding, bodyData.length);
        cv.put("decoded_body_size", size);
    }
    cv.put("data_location", dataLocation);
    cv.put("encoding", encoding);
    cv.put("content_id", part.getContentId());
    return file;
}
Also used : SizeAware(com.fsck.k9.mail.internet.SizeAware) File(java.io.File)

Example 2 with SizeAware

use of com.fsck.k9.mail.internet.SizeAware in project k-9 by k9mail.

the class MessageCryptoHelper method getDataSourceForEncryptedOrInlineData.

private OpenPgpDataSource getDataSourceForEncryptedOrInlineData() {
    return new OpenPgpApi.OpenPgpDataSource() {

        @Override
        public Long getSizeForProgress() {
            Part part = currentCryptoPart.part;
            CryptoPartType cryptoPartType = currentCryptoPart.type;
            Body body;
            if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) {
                Multipart multipartEncryptedMultipart = (Multipart) part.getBody();
                BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1);
                body = encryptionPayloadPart.getBody();
            } else if (cryptoPartType == CryptoPartType.PGP_INLINE) {
                body = part.getBody();
            } else {
                throw new IllegalStateException("part to stream must be encrypted or inline!");
            }
            if (body instanceof SizeAware) {
                return ((SizeAware) body).getSize();
            }
            return null;
        }

        @Override
        @WorkerThread
        public void writeTo(OutputStream os) throws IOException {
            try {
                Part part = currentCryptoPart.part;
                CryptoPartType cryptoPartType = currentCryptoPart.type;
                if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) {
                    Multipart multipartEncryptedMultipart = (Multipart) part.getBody();
                    BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1);
                    Body encryptionPayloadBody = encryptionPayloadPart.getBody();
                    encryptionPayloadBody.writeTo(os);
                } else if (cryptoPartType == CryptoPartType.PGP_INLINE) {
                    String text = MessageExtractor.getTextFromPart(part);
                    os.write(text.getBytes());
                } else {
                    throw new IllegalStateException("part to stream must be encrypted or inline!");
                }
            } catch (MessagingException e) {
                Timber.e(e, "MessagingException while writing message to crypto provider");
            }
        }
    };
}
Also used : MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) SizeAware(com.fsck.k9.mail.internet.SizeAware) MessagingException(com.fsck.k9.mail.MessagingException) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body)

Aggregations

SizeAware (com.fsck.k9.mail.internet.SizeAware)2 Body (com.fsck.k9.mail.Body)1 BodyPart (com.fsck.k9.mail.BodyPart)1 MessagingException (com.fsck.k9.mail.MessagingException)1 Multipart (com.fsck.k9.mail.Multipart)1 Part (com.fsck.k9.mail.Part)1 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)1 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)1 TextBody (com.fsck.k9.mail.internet.TextBody)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)1