diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index 6ece4fe04f..ceaf9a4a1b 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -1312,11 +1312,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB db.attachment().deleteAttachment(a.id); // Add decrypted attachments + attachments = parts.getAttachments(); int sequence = db.attachment().getAttachmentSequence(id); - for (EntityAttachment a : parts.getAttachments()) { + for (int index = 0; index < attachments.size(); index++) { + EntityAttachment a = attachments.get(index); a.message = id; a.sequence = ++sequence; a.id = db.attachment().insertAttachment(a); + parts.downloadAttachment(context, index, a.id); } db.message().setMessageStored(id, new Date().getTime()); diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 985fcf8512..cfadb27f94 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -675,7 +675,7 @@ class Core { // Download attachment MessageHelper helper = new MessageHelper((MimeMessage) imessage); MessageHelper.MessageParts parts = helper.getMessageParts(); - parts.downloadAttachment(context, db, attachment.id, sequence); + parts.downloadAttachment(context, sequence - 1, attachment.id); } private static long append(IMAPStore istore, IMAPFolder ifolder, MimeMessage imessage) throws MessagingException { @@ -1411,7 +1411,7 @@ class Core { for (EntityAttachment attachment : attachments) if (!attachment.available) if (!metered || (attachment.size != null && attachment.size < maxSize)) - if (!parts.downloadAttachment(context, db, attachment.id, attachment.sequence)) + if (!parts.downloadAttachment(context, attachment.sequence - 1, attachment.id)) break; } } diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 0a036c1446..0285d05117 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -691,18 +691,20 @@ public class MessageHelper { return result; } - boolean downloadAttachment(Context context, DB db, long id, int sequence) throws IOException { + boolean downloadAttachment(Context context, int index, long id) { + Log.i("downloading attchment id=" + id + " seq=" + index); // Attachments of drafts might not have been uploaded yet - if (sequence > attachments.size()) { - Log.w("Attachment unavailable sequence=" + sequence + " size=" + attachments.size()); + if (index > attachments.size()) { + Log.w("Attachment unavailable sequence=" + index + " size=" + attachments.size()); return false; } // Get data - AttachmentPart apart = attachments.get(sequence - 1); + AttachmentPart apart = attachments.get(index); File file = EntityAttachment.getFile(context, id); // Download attachment + DB db = DB.getInstance(context); db.attachment().setProgress(id, null); try (InputStream is = apart.part.getInputStream()) { long size = 0;