|
29 | 29 | import com.flowcrypt.email.R; |
30 | 30 | import com.flowcrypt.email.api.email.JavaEmailConstants; |
31 | 31 | import com.flowcrypt.email.api.email.model.AttachmentInfo; |
| 32 | +import com.flowcrypt.email.api.email.protocol.ImapProtocolUtil; |
32 | 33 | import com.flowcrypt.email.api.email.protocol.OpenStoreHelper; |
33 | 34 | import com.flowcrypt.email.database.dao.source.AccountDao; |
34 | 35 | import com.flowcrypt.email.database.dao.source.AccountDaoSource; |
|
56 | 57 | import javax.mail.Part; |
57 | 58 | import javax.mail.Session; |
58 | 59 | import javax.mail.Store; |
| 60 | +import javax.mail.internet.InternetHeaders; |
59 | 61 |
|
60 | 62 | /** |
61 | 63 | * This service will be use to download email attachments. To start load an attachment just run service via the intent |
@@ -464,7 +466,8 @@ public void run() { |
464 | 466 | imapFolder.open(Folder.READ_ONLY); |
465 | 467 |
|
466 | 468 | javax.mail.Message message = imapFolder.getMessageByUID(attachmentInfo.getUid()); |
467 | | - Part attachment = getAttachmentPart(message, attachmentInfo.getId()); |
| 469 | + Part attachment = getAttachmentPartById(accountDao, imapFolder, message.getMessageNumber(), message, |
| 470 | + attachmentInfo.getId()); |
468 | 471 |
|
469 | 472 | if (attachment != null) { |
470 | 473 | InputStream input = attachment.getInputStream(); |
@@ -567,28 +570,48 @@ private File prepareAttachmentFile() { |
567 | 570 | /** |
568 | 571 | * Get {@link Part} which has an attachment with such attachment id. |
569 | 572 | * |
570 | | - * @param part The parent part. |
| 573 | + * @param accountDao The object which contains information about an email account. |
| 574 | + * @param imapFolder The {@link IMAPFolder} which contains the parent message; |
| 575 | + * @param messageNumber This number will be used for fetching {@link Part} details; |
| 576 | + * @param part The parent part. |
571 | 577 | * @return {@link Part} which has attachment or null if message doesn't have such attachment. |
572 | 578 | * @throws MessagingException |
573 | 579 | * @throws IOException |
574 | 580 | */ |
575 | | - private Part getAttachmentPart(Part part, String attachmentId) throws MessagingException, IOException { |
| 581 | + private Part getAttachmentPartById(AccountDao accountDao, IMAPFolder imapFolder, int messageNumber, Part |
| 582 | + part, String attachmentId) |
| 583 | + throws MessagingException, IOException { |
576 | 584 | if (part != null && part.isMimeType(JavaEmailConstants.MIME_TYPE_MULTIPART)) { |
577 | 585 | Multipart multiPart = (Multipart) part.getContent(); |
578 | 586 | int numberOfParts = multiPart.getCount(); |
579 | 587 | String[] headers; |
580 | 588 | for (int partCount = 0; partCount < numberOfParts; partCount++) { |
581 | 589 | BodyPart bodyPart = multiPart.getBodyPart(partCount); |
582 | 590 | if (bodyPart.isMimeType(JavaEmailConstants.MIME_TYPE_MULTIPART)) { |
583 | | - Part innerPart = getAttachmentPart(bodyPart, attachmentId); |
| 591 | + Part innerPart = getAttachmentPartById(accountDao, imapFolder, messageNumber, bodyPart, |
| 592 | + attachmentId); |
584 | 593 | if (innerPart != null) { |
585 | 594 | return innerPart; |
586 | 595 | } |
587 | | - } else if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) |
588 | | - && (headers = bodyPart.getHeader(JavaEmailConstants.HEADER_X_ATTACHMENT_ID)) != null |
589 | | - && headers.length > 0 |
590 | | - && attachmentId.equals(headers[0])) { |
591 | | - return bodyPart; |
| 596 | + } else if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) { |
| 597 | + InputStream inputStream = ImapProtocolUtil.getHeaderStream(accountDao, imapFolder, |
| 598 | + messageNumber, partCount + 1); |
| 599 | + |
| 600 | + if (inputStream == null) { |
| 601 | + throw new MessagingException("Failed to fetch headers"); |
| 602 | + } |
| 603 | + |
| 604 | + InternetHeaders internetHeaders = new InternetHeaders(inputStream); |
| 605 | + headers = internetHeaders.getHeader(JavaEmailConstants.HEADER_CONTENT_ID); |
| 606 | + |
| 607 | + if (headers == null) { |
| 608 | + //try to receive custom Gmail attachments header X-Attachment-Id |
| 609 | + headers = internetHeaders.getHeader(JavaEmailConstants.HEADER_X_ATTACHMENT_ID); |
| 610 | + } |
| 611 | + |
| 612 | + if (headers != null && attachmentId.equals(headers[0])) { |
| 613 | + return bodyPart; |
| 614 | + } |
592 | 615 | } |
593 | 616 | } |
594 | 617 | return null; |
|
0 commit comments