Always download attachments by properties

pull/156/head
M66B 6 years ago
parent 8cfbbdd4bb
commit 89adda5091

@ -1350,20 +1350,21 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
Helper.writeText(m.getFile(context), parts.getHtml(context)); Helper.writeText(m.getFile(context), parts.getHtml(context));
// Remove previously decrypted attachments // Remove previously decrypted attachments
for (EntityAttachment a : attachments) for (EntityAttachment local : attachments)
if (a.encryption == null) if (local.encryption == null)
db.attachment().deleteAttachment(a.id); db.attachment().deleteAttachment(local.id);
// Add decrypted attachments
attachments = parts.getAttachments();
int sequence = db.attachment().getAttachmentSequence(id); int sequence = db.attachment().getAttachmentSequence(id);
for (int index = 0; index < attachments.size(); index++) {
EntityAttachment a = attachments.get(index); // Add decrypted attachments
a.message = id; List<EntityAttachment> remotes = parts.getAttachments();
a.sequence = ++sequence; for (int index = 0; index < remotes.size(); index++) {
a.id = db.attachment().insertAttachment(a); EntityAttachment remote = remotes.get(index);
remote.message = id;
remote.sequence = ++sequence;
remote.id = db.attachment().insertAttachment(remote);
try { try {
parts.downloadAttachment(context, a); parts.downloadAttachment(context, index, remote.id, remote.name);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }

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

@ -81,6 +81,10 @@ public class EntityAttachment {
} }
File getFile(Context context) { File getFile(Context context) {
return getFile(context, id, name);
}
static File getFile(Context context, long id, String name) {
File dir = new File(context.getFilesDir(), "attachments"); File dir = new File(context.getFilesDir(), "attachments");
if (!dir.exists()) if (!dir.exists())
dir.mkdir(); dir.mkdir();
@ -130,4 +134,10 @@ public class EntityAttachment {
} else } else
return false; return false;
} }
@NonNull
@Override
public String toString() {
return (this.name + " type=" + this.type + " disposition=" + this.disposition + " cid=" + this.cid + " size=" + this.size);
}
} }

@ -848,17 +848,17 @@ public class MessageHelper {
return result; return result;
} }
void downloadAttachment(Context context, EntityAttachment attachment) throws MessagingException, IOException { void downloadAttachment(Context context, int index, long id, String name) throws MessagingException, IOException {
Log.i("downloading attachment id=" + attachment.id); Log.i("downloading attachment id=" + id);
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
// Get data // Get data
AttachmentPart apart = attachments.get(attachment.sequence - 1); AttachmentPart apart = attachments.get(index);
// Download attachment // Download attachment
File file = attachment.getFile(context); File file = EntityAttachment.getFile(context, id, name);
db.attachment().setProgress(attachment.id, null); db.attachment().setProgress(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();
@ -871,19 +871,19 @@ public class MessageHelper {
// Update progress // Update progress
if (total > 0) if (total > 0)
db.attachment().setProgress(attachment.id, (int) (size * 100 / total)); db.attachment().setProgress(id, (int) (size * 100 / total));
} }
} }
// Store attachment data // Store attachment data
db.attachment().setDownloaded(attachment.id, size); db.attachment().setDownloaded(id, size);
Log.i("Downloaded attachment size=" + size); Log.i("Downloaded attachment size=" + size);
} catch (FolderClosedIOException ex) { } catch (FolderClosedIOException ex) {
throw new FolderClosedException(ex.getFolder(), "downloadAttachment", ex); throw new FolderClosedException(ex.getFolder(), "downloadAttachment", ex);
} catch (Throwable ex) { } catch (Throwable ex) {
// Reset progress on failure // Reset progress on failure
db.attachment().setError(attachment.id, Helper.formatThrowable(ex)); db.attachment().setError(id, Helper.formatThrowable(ex));
throw ex; throw ex;
} }
} }

Loading…
Cancel
Save