Search in sources :

Example 1 with AttachmentInfoExtractor

use of com.fsck.k9.message.extractors.AttachmentInfoExtractor in project k-9 by k9mail.

the class AttachmentResolver method buildCidToAttachmentUriMap.

@VisibleForTesting
static Map<String, Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor, Part rootPart) {
    HashMap<String, Uri> result = new HashMap<>();
    Stack<Part> partsToCheck = new Stack<>();
    partsToCheck.push(rootPart);
    while (!partsToCheck.isEmpty()) {
        Part part = partsToCheck.pop();
        Body body = part.getBody();
        if (body instanceof Multipart) {
            Multipart multipart = (Multipart) body;
            for (Part bodyPart : multipart.getBodyParts()) {
                partsToCheck.push(bodyPart);
            }
        } else {
            try {
                String contentId = part.getContentId();
                if (contentId != null) {
                    AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part);
                    result.put(contentId, attachmentInfo.internalUri);
                }
            } catch (MessagingException e) {
                Timber.e(e, "Error extracting attachment info");
            }
        }
    }
    return Collections.unmodifiableMap(result);
}
Also used : Multipart(com.fsck.k9.mail.Multipart) HashMap(java.util.HashMap) MessagingException(com.fsck.k9.mail.MessagingException) Part(com.fsck.k9.mail.Part) Uri(android.net.Uri) Body(com.fsck.k9.mail.Body) Stack(java.util.Stack) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 2 with AttachmentInfoExtractor

use of com.fsck.k9.message.extractors.AttachmentInfoExtractor in project k-9 by k9mail.

the class AttachmentResolverTest method buildCidMap__onTwoPart__shouldReturnBothUris.

@Test
public void buildCidMap__onTwoPart__shouldReturnBothUris() throws Exception {
    Multipart multipartBody = MimeMultipart.newInstance();
    Part multipartPart = new MimeBodyPart(multipartBody);
    BodyPart subPart1 = new MimeBodyPart();
    BodyPart subPart2 = new MimeBodyPart();
    multipartBody.addBodyPart(subPart1);
    multipartBody.addBodyPart(subPart2);
    subPart1.setHeader(MimeHeader.HEADER_CONTENT_ID, "cid-1");
    subPart2.setHeader(MimeHeader.HEADER_CONTENT_ID, "cid-2");
    when(attachmentInfoExtractor.extractAttachmentInfo(subPart1)).thenReturn(new AttachmentViewInfo(null, null, AttachmentViewInfo.UNKNOWN_SIZE, ATTACHMENT_TEST_URI_1, false, subPart1, true));
    when(attachmentInfoExtractor.extractAttachmentInfo(subPart2)).thenReturn(new AttachmentViewInfo(null, null, AttachmentViewInfo.UNKNOWN_SIZE, ATTACHMENT_TEST_URI_2, false, subPart2, true));
    Map<String, Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart);
    assertEquals(2, result.size());
    assertEquals(ATTACHMENT_TEST_URI_1, result.get("cid-1"));
    assertEquals(ATTACHMENT_TEST_URI_2, result.get("cid-2"));
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Uri(android.net.Uri) Test(org.junit.Test)

Example 3 with AttachmentInfoExtractor

use of com.fsck.k9.message.extractors.AttachmentInfoExtractor in project k-9 by k9mail.

the class AttachmentResolverTest method buildCidMap__onMultipartWithNoParts__shouldReturnEmptyMap.

@Test
public void buildCidMap__onMultipartWithNoParts__shouldReturnEmptyMap() throws Exception {
    Multipart multipartBody = MimeMultipart.newInstance();
    Part multipartPart = new MimeBodyPart(multipartBody);
    Map<String, Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart);
    assertTrue(result.isEmpty());
}
Also used : MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) Multipart(com.fsck.k9.mail.Multipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Uri(android.net.Uri) Test(org.junit.Test)

Example 4 with AttachmentInfoExtractor

use of com.fsck.k9.message.extractors.AttachmentInfoExtractor in project k-9 by k9mail.

the class LocalFolder method loadMessagePart.

private void loadMessagePart(LocalMessage message, Map<Long, Part> partById, Cursor cursor) throws MessagingException {
    long id = cursor.getLong(0);
    long parentId = cursor.getLong(2);
    String mimeType = cursor.getString(3);
    long size = cursor.getLong(4);
    byte[] header = cursor.getBlob(6);
    int dataLocation = cursor.getInt(9);
    String serverExtra = cursor.getString(15);
    // TODO we don't currently cache much of the part data which is computed with AttachmentInfoExtractor,
    // TODO might want to do that at a later point?
    // String displayName = cursor.getString(5);
    // int type = cursor.getInt(1);
    // boolean inlineAttachment = (type == MessagePartType.HIDDEN_ATTACHMENT);
    final Part part;
    if (id == message.getMessagePartId()) {
        part = message;
    } else {
        Part parentPart = partById.get(parentId);
        if (parentPart == null) {
            throw new IllegalStateException("Parent part not found");
        }
        String parentMimeType = parentPart.getMimeType();
        if (MimeUtility.isMultipart(parentMimeType)) {
            BodyPart bodyPart = new LocalBodyPart(getAccountUuid(), message, id, size);
            ((Multipart) parentPart.getBody()).addBodyPart(bodyPart);
            part = bodyPart;
        } else if (MimeUtility.isMessage(parentMimeType)) {
            Message innerMessage = new LocalMimeMessage(getAccountUuid(), message, id);
            parentPart.setBody(innerMessage);
            part = innerMessage;
        } else {
            throw new IllegalStateException("Parent is neither a multipart nor a message");
        }
        parseHeaderBytes(part, header);
    }
    partById.put(id, part);
    part.setServerExtra(serverExtra);
    if (MimeUtility.isMultipart(mimeType)) {
        byte[] preamble = cursor.getBlob(11);
        byte[] epilogue = cursor.getBlob(12);
        String boundary = cursor.getString(13);
        MimeMultipart multipart = new MimeMultipart(mimeType, boundary);
        part.setBody(multipart);
        multipart.setPreamble(preamble);
        multipart.setEpilogue(epilogue);
    } else if (dataLocation == DataLocation.IN_DATABASE) {
        String encoding = cursor.getString(7);
        byte[] data = cursor.getBlob(10);
        Body body = new BinaryMemoryBody(data, encoding);
        part.setBody(body);
    } else if (dataLocation == DataLocation.ON_DISK) {
        String encoding = cursor.getString(7);
        File file = localStore.getAttachmentFile(Long.toString(id));
        if (file.exists()) {
            Body body = new FileBackedBody(file, encoding);
            part.setBody(body);
        }
    }
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Multipart(com.fsck.k9.mail.Multipart) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Body(com.fsck.k9.mail.Body) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) File(java.io.File)

Example 5 with AttachmentInfoExtractor

use of com.fsck.k9.message.extractors.AttachmentInfoExtractor in project k-9 by k9mail.

the class AttachmentResolver method createFromPart.

@WorkerThread
public static AttachmentResolver createFromPart(Part part) {
    AttachmentInfoExtractor attachmentInfoExtractor = AttachmentInfoExtractor.getInstance();
    Map<String, Uri> contentIdToAttachmentUriMap = buildCidToAttachmentUriMap(attachmentInfoExtractor, part);
    return new AttachmentResolver(contentIdToAttachmentUriMap);
}
Also used : AttachmentInfoExtractor(com.fsck.k9.message.extractors.AttachmentInfoExtractor) Uri(android.net.Uri) WorkerThread(android.support.annotation.WorkerThread)

Aggregations

Uri (android.net.Uri)7 Part (com.fsck.k9.mail.Part)6 BodyPart (com.fsck.k9.mail.BodyPart)5 Multipart (com.fsck.k9.mail.Multipart)5 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)5 Test (org.junit.Test)5 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)4 Body (com.fsck.k9.mail.Body)2 AttachmentInfoExtractor (com.fsck.k9.message.extractors.AttachmentInfoExtractor)2 Context (android.content.Context)1 Nullable (android.support.annotation.Nullable)1 VisibleForTesting (android.support.annotation.VisibleForTesting)1 WorkerThread (android.support.annotation.WorkerThread)1 Message (com.fsck.k9.mail.Message)1 MessagingException (com.fsck.k9.mail.MessagingException)1 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)1 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)1 AttachmentViewInfo (com.fsck.k9.mailstore.AttachmentViewInfo)1 DeferredFileBody (com.fsck.k9.mailstore.DeferredFileBody)1 HtmlSanitizer (com.fsck.k9.message.html.HtmlSanitizer)1