diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 5180b7aba1..9ec68b2a83 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -744,12 +744,12 @@ class Core { long id = jargs.getLong(0); // Get attachment - EntityAttachment local = db.attachment().getAttachment(id); - if (local == null) - local = db.attachment().getAttachment(message.id, (int) id); // legacy - if (local == null) + EntityAttachment attachment = db.attachment().getAttachment(id); + if (attachment == null) + attachment = db.attachment().getAttachment(message.id, (int) id); // legacy + if (attachment == null) throw new IllegalArgumentException("Local attachment not found"); - if (local.available) + if (attachment.available) return; // Get message @@ -761,32 +761,8 @@ class Core { MessageHelper helper = new MessageHelper((MimeMessage) imessage); MessageHelper.MessageParts parts = helper.getMessageParts(); - // Match attachment by attributes - // Some servers order attachments randomly - boolean found = false; - List remotes = parts.getAttachments(); - for (int i = 0; i < remotes.size(); i++) { - EntityAttachment remote = remotes.get(i); - if (Objects.equals(remote.name, local.name) && - Objects.equals(remote.type, local.type) && - Objects.equals(remote.disposition, local.disposition) && - Objects.equals(remote.cid, local.cid) && - Objects.equals(remote.encryption, local.encryption) && - Objects.equals(remote.size, local.size)) { - found = true; - parts.downloadAttachment(context, i, local.id, local.name); - } - } - - if (!found) { - db.attachment().setError(local.id, "Attachment not found"); - if (!EntityFolder.DRAFTS.equals(folder.type)) { - Log.w("Attachment not found local=" + local); - for (EntityAttachment remote : remotes) - Log.w("Attachment remote=" + remote); - throw new IllegalArgumentException("Attachment not found"); - } - } + // Download attachment + parts.downloadAttachment(context, attachment); } static void onSynchronizeFolders(Context context, EntityAccount account, Store istore, State state) throws MessagingException { @@ -1725,31 +1701,11 @@ class Core { } } - List remotes = parts.getAttachments(); - - for (EntityAttachment local : attachments) - if (!local.available) - if (state.getNetworkState().isUnmetered() || (local.size != null && local.size < maxSize)) + for (EntityAttachment attachment : attachments) + if (!attachment.available) + if (state.getNetworkState().isUnmetered() || (attachment.size != null && attachment.size < maxSize)) try { - boolean found = false; - for (int i = 0; i < remotes.size(); i++) { - EntityAttachment remote = remotes.get(i); - if (Objects.equals(remote.name, local.name) && - Objects.equals(remote.type, local.type) && - Objects.equals(remote.disposition, local.disposition) && - Objects.equals(remote.cid, local.cid) && - Objects.equals(remote.encryption, local.encryption) && - Objects.equals(remote.size, local.size)) { - found = true; - parts.downloadAttachment(context, i, local.id, local.name); - } - } - - if (!found) { - Log.w("Attachment not found local=" + local); - for (EntityAttachment remote : remotes) - Log.w("Attachment remote=" + remote); - } + parts.downloadAttachment(context, attachment); } catch (Throwable ex) { Log.e(ex); } diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 52a53b1cd0..6c03348f49 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -3638,7 +3638,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. remote.sequence = ++sequence; remote.id = db.attachment().insertAttachment(remote); try { - parts.downloadAttachment(context, index, remote.id, remote.name); + parts.downloadAttachment(context, index, remote); } catch (Throwable ex) { Log.e(ex); } diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index bee2023ae7..5057a9723d 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -43,6 +43,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.Enumeration; import java.util.List; +import java.util.Objects; import java.util.Properties; import java.util.TimeZone; @@ -854,8 +855,32 @@ public class MessageHelper { return result; } - void downloadAttachment(Context context, int index, long id, String name) throws MessagingException, IOException { - Log.i("downloading attachment id=" + id); + void downloadAttachment(Context context, EntityAttachment local) throws IOException, MessagingException { + List remotes = getAttachments(); + + // Match attachment by attributes + // Some servers order attachments randomly + boolean found = false; + for (int i = 0; i < remotes.size(); i++) { + EntityAttachment remote = remotes.get(i); + if (Objects.equals(remote.name, local.name) && + Objects.equals(remote.disposition, local.disposition) && + Objects.equals(remote.cid, local.cid)) { + found = true; + downloadAttachment(context, i, local); + } + } + + if (!found) { + Log.w("Attachment not found local=" + local); + for (EntityAttachment remote : remotes) + Log.w("Attachment remote=" + remote); + throw new IllegalArgumentException("Attachment not found"); + } + } + + void downloadAttachment(Context context, int index, EntityAttachment local) throws MessagingException, IOException { + Log.i("downloading attachment id=" + local.id); DB db = DB.getInstance(context); @@ -863,8 +888,8 @@ public class MessageHelper { AttachmentPart apart = attachments.get(index); // Download attachment - File file = EntityAttachment.getFile(context, id, name); - db.attachment().setProgress(id, null); + File file = EntityAttachment.getFile(context, local.id, local.name); + db.attachment().setProgress(local.id, null); try (InputStream is = apart.part.getInputStream()) { long size = 0; long total = apart.part.getSize(); @@ -881,25 +906,25 @@ public class MessageHelper { int progress = (int) (size * 100 / total / 20 * 20); if (progress != lastprogress) { lastprogress = progress; - db.attachment().setProgress(id, progress); + db.attachment().setProgress(local.id, progress); } } } } // Store attachment data - db.attachment().setDownloaded(id, size); + db.attachment().setDownloaded(local.id, size); Log.i("Downloaded attachment size=" + size); } catch (FolderClosedIOException ex) { - db.attachment().setError(id, Helper.formatThrowable(ex)); + db.attachment().setError(local.id, Helper.formatThrowable(ex)); throw new FolderClosedException(ex.getFolder(), "downloadAttachment", ex); } catch (MessageRemovedIOException ex) { - db.attachment().setError(id, Helper.formatThrowable(ex)); + db.attachment().setError(local.id, Helper.formatThrowable(ex)); throw new MessagingException("downloadAttachment", ex); } catch (Throwable ex) { // Reset progress on failure - db.attachment().setError(id, Helper.formatThrowable(ex)); + db.attachment().setError(local.id, Helper.formatThrowable(ex)); throw ex; } }