|
|
|
@ -707,12 +707,12 @@ class Core {
|
|
|
|
|
long id = jargs.getLong(0);
|
|
|
|
|
|
|
|
|
|
// Get attachment
|
|
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(id);
|
|
|
|
|
if (attachment == null)
|
|
|
|
|
attachment = db.attachment().getAttachment(message.id, (int) id); // legacy
|
|
|
|
|
if (attachment == null)
|
|
|
|
|
EntityAttachment local = db.attachment().getAttachment(id);
|
|
|
|
|
if (local == null)
|
|
|
|
|
local = db.attachment().getAttachment(message.id, (int) id); // legacy
|
|
|
|
|
if (local == null)
|
|
|
|
|
throw new IllegalArgumentException("Attachment not found");
|
|
|
|
|
if (attachment.available)
|
|
|
|
|
if (local.available)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Get message
|
|
|
|
@ -720,28 +720,32 @@ class Core {
|
|
|
|
|
if (imessage == null)
|
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
|
|
// Get message parts
|
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage);
|
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
|
|
// Match attachment by attributes
|
|
|
|
|
// Some servers order attachments randomly
|
|
|
|
|
boolean found = false;
|
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
|
for (EntityAttachment a : attachments) {
|
|
|
|
|
if (Objects.equals(a.name, attachment.name) &&
|
|
|
|
|
Objects.equals(a.type, attachment.type) &&
|
|
|
|
|
Objects.equals(a.disposition, attachment.disposition) &&
|
|
|
|
|
Objects.equals(a.cid, attachment.cid) &&
|
|
|
|
|
Objects.equals(a.encryption, attachment.encryption) &&
|
|
|
|
|
Objects.equals(a.size, attachment.size)) {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// Download attachment
|
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage);
|
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
parts.downloadAttachment(context, a);
|
|
|
|
|
parts.downloadAttachment(context, i, local.id, local.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!found && !EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
|
throw new IllegalArgumentException("Attachment not found");
|
|
|
|
|
if (!found) {
|
|
|
|
|
db.attachment().setError(local.id, "Attachment not found");
|
|
|
|
|
if (!EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
|
throw new IllegalArgumentException("Attachment not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateMessageSize(context, message.id);
|
|
|
|
|
}
|
|
|
|
@ -1597,11 +1601,22 @@ class Core {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (EntityAttachment attachment : attachments)
|
|
|
|
|
if (!attachment.available)
|
|
|
|
|
if (state.getNetworkState().isUnmetered() || (attachment.size != null && attachment.size < maxSize))
|
|
|
|
|
List<EntityAttachment> remotes = parts.getAttachments();
|
|
|
|
|
|
|
|
|
|
for (EntityAttachment local : attachments)
|
|
|
|
|
if (!local.available)
|
|
|
|
|
if (state.getNetworkState().isUnmetered() || (local.size != null && local.size < maxSize))
|
|
|
|
|
try {
|
|
|
|
|
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))
|
|
|
|
|
parts.downloadAttachment(context, i, local.id, local.name);
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
}
|
|
|
|
|