Refactoring attachments

pull/157/head
M66B 5 years ago
parent 171eb97e55
commit 0e3e4ddcf2

@ -744,12 +744,12 @@ class Core {
long id = jargs.getLong(0); long id = jargs.getLong(0);
// Get attachment // Get attachment
EntityAttachment local = db.attachment().getAttachment(id); EntityAttachment attachment = db.attachment().getAttachment(id);
if (local == null) if (attachment == null)
local = db.attachment().getAttachment(message.id, (int) id); // legacy attachment = db.attachment().getAttachment(message.id, (int) id); // legacy
if (local == null) if (attachment == null)
throw new IllegalArgumentException("Local attachment not found"); throw new IllegalArgumentException("Local attachment not found");
if (local.available) if (attachment.available)
return; return;
// Get message // Get message
@ -761,32 +761,8 @@ class Core {
MessageHelper helper = new MessageHelper((MimeMessage) imessage); MessageHelper helper = new MessageHelper((MimeMessage) imessage);
MessageHelper.MessageParts parts = helper.getMessageParts(); MessageHelper.MessageParts parts = helper.getMessageParts();
// Match attachment by attributes // Download attachment
// Some servers order attachments randomly parts.downloadAttachment(context, attachment);
boolean found = false;
List<EntityAttachment> 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");
}
}
} }
static void onSynchronizeFolders(Context context, EntityAccount account, Store istore, State state) throws MessagingException { static void onSynchronizeFolders(Context context, EntityAccount account, Store istore, State state) throws MessagingException {
@ -1725,31 +1701,11 @@ class Core {
} }
} }
List<EntityAttachment> remotes = parts.getAttachments(); for (EntityAttachment attachment : attachments)
if (!attachment.available)
for (EntityAttachment local : attachments) if (state.getNetworkState().isUnmetered() || (attachment.size != null && attachment.size < maxSize))
if (!local.available)
if (state.getNetworkState().isUnmetered() || (local.size != null && local.size < maxSize))
try { try {
boolean found = false; parts.downloadAttachment(context, attachment);
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);
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }

@ -3638,7 +3638,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
remote.sequence = ++sequence; remote.sequence = ++sequence;
remote.id = db.attachment().insertAttachment(remote); remote.id = db.attachment().insertAttachment(remote);
try { try {
parts.downloadAttachment(context, index, remote.id, remote.name); parts.downloadAttachment(context, index, remote);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }

@ -43,6 +43,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import java.util.TimeZone; import java.util.TimeZone;
@ -854,8 +855,32 @@ public class MessageHelper {
return result; return result;
} }
void downloadAttachment(Context context, int index, long id, String name) throws MessagingException, IOException { void downloadAttachment(Context context, EntityAttachment local) throws IOException, MessagingException {
Log.i("downloading attachment id=" + id); List<EntityAttachment> 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); DB db = DB.getInstance(context);
@ -863,8 +888,8 @@ public class MessageHelper {
AttachmentPart apart = attachments.get(index); AttachmentPart apart = attachments.get(index);
// Download attachment // Download attachment
File file = EntityAttachment.getFile(context, id, name); File file = EntityAttachment.getFile(context, local.id, local.name);
db.attachment().setProgress(id, null); db.attachment().setProgress(local.id, null);
try (InputStream is = apart.part.getInputStream()) { try (InputStream is = apart.part.getInputStream()) {
long size = 0; long size = 0;
long total = apart.part.getSize(); long total = apart.part.getSize();
@ -881,25 +906,25 @@ public class MessageHelper {
int progress = (int) (size * 100 / total / 20 * 20); int progress = (int) (size * 100 / total / 20 * 20);
if (progress != lastprogress) { if (progress != lastprogress) {
lastprogress = progress; lastprogress = progress;
db.attachment().setProgress(id, progress); db.attachment().setProgress(local.id, progress);
} }
} }
} }
} }
// Store attachment data // Store attachment data
db.attachment().setDownloaded(id, size); db.attachment().setDownloaded(local.id, size);
Log.i("Downloaded attachment size=" + size); Log.i("Downloaded attachment size=" + size);
} catch (FolderClosedIOException ex) { } 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); throw new FolderClosedException(ex.getFolder(), "downloadAttachment", ex);
} catch (MessageRemovedIOException ex) { } catch (MessageRemovedIOException ex) {
db.attachment().setError(id, Helper.formatThrowable(ex)); db.attachment().setError(local.id, Helper.formatThrowable(ex));
throw new MessagingException("downloadAttachment", ex); throw new MessagingException("downloadAttachment", ex);
} catch (Throwable ex) { } catch (Throwable ex) {
// Reset progress on failure // Reset progress on failure
db.attachment().setError(id, Helper.formatThrowable(ex)); db.attachment().setError(local.id, Helper.formatThrowable(ex));
throw ex; throw ex;
} }
} }

Loading…
Cancel
Save