Optimized attachment downloading, small fix

pull/156/head
M66B 6 years ago
parent aa806b7ce4
commit a036f0dfb0

@ -67,6 +67,11 @@ public interface DaoAttachment {
" WHERE id = :id") " WHERE id = :id")
void setMessage(long id, long message); void setMessage(long id, long message);
@Query("UPDATE attachment" +
" SET name = :name, type = :type, disposition = :disposition, cid = :cid, encryption = :encryption" +
" WHERE id = :id")
void setInfo(long id, String name, String type, String disposition, String cid, Integer encryption);
@Query("UPDATE attachment" + @Query("UPDATE attachment" +
" SET error = NULL, progress = :progress, available = 0" + " SET error = NULL, progress = :progress, available = 0" +
" WHERE id = :id") " WHERE id = :id")

@ -822,48 +822,11 @@ public class MessageHelper {
return attachments; return attachments;
} }
List<EntityAttachment> getAttachments() throws MessagingException { List<EntityAttachment> getAttachments() {
List<EntityAttachment> result = new ArrayList<>(); List<EntityAttachment> result = new ArrayList<>();
for (AttachmentPart apart : attachments) { for (AttachmentPart apart : attachments)
ContentType ct; result.add(apart.attachment);
try {
ct = new ContentType(apart.part.getContentType());
} catch (ParseException ex) {
Log.w(ex);
ct = new ContentType("application/octet-stream");
}
String[] cid = apart.part.getHeader("Content-ID");
EntityAttachment attachment = new EntityAttachment();
attachment.name = apart.filename;
attachment.type = ct.getBaseType().toLowerCase();
attachment.disposition = apart.disposition;
attachment.size = (long) apart.part.getSize();
attachment.cid = (cid == null || cid.length == 0 ? null : MimeUtility.unfold(cid[0]));
attachment.encryption = (apart.pgp ? EntityAttachment.PGP_MESSAGE : null);
if ("text/calendar".equalsIgnoreCase(attachment.type) && TextUtils.isEmpty(attachment.name))
attachment.name = "invite.ics";
// Try to guess a better content type
// Sometimes PDF files are sent using the wrong type
if ("application/octet-stream".equalsIgnoreCase(attachment.type)) {
String extension = Helper.getExtension(attachment.name);
if (extension != null) {
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
if (type != null) {
Log.w("Guessing file=" + attachment.name + " type=" + type);
attachment.type = type;
}
}
}
if (attachment.size < 0)
attachment.size = null;
result.add(attachment);
}
// Fix duplicate CIDs // Fix duplicate CIDs
for (int i = 0; i < result.size(); i++) { for (int i = 0; i < result.size(); i++) {
@ -881,8 +844,9 @@ public class MessageHelper {
void downloadAttachment(Context context, int index, long id) throws MessagingException, IOException { void downloadAttachment(Context context, int index, long id) throws MessagingException, IOException {
Log.i("downloading attchment id=" + id + " seq=" + index); Log.i("downloading attchment id=" + id + " seq=" + index);
// Attachments of drafts might not have been uploaded yet // Attachments of drafts might not have been uploaded yet
if (index > attachments.size()) { if (index >= attachments.size()) {
Log.w("Attachment unavailable sequence=" + index + " size=" + attachments.size()); Log.w("Attachment unavailable sequence=" + index + " size=" + attachments.size());
return; return;
} }
@ -894,9 +858,17 @@ public class MessageHelper {
EntityAttachment attachment = db.attachment().getAttachment(id); EntityAttachment attachment = db.attachment().getAttachment(id);
if (attachment == null) if (attachment == null)
return; return;
File file = attachment.getFile(context);
// Set info again in case ordering changed
db.attachment().setInfo(id,
apart.attachment.name,
apart.attachment.type,
apart.attachment.disposition,
apart.attachment.cid,
apart.attachment.encryption);
// Download attachment // Download attachment
File file = attachment.getFile(context);
db.attachment().setProgress(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;
@ -942,6 +914,7 @@ public class MessageHelper {
String filename; String filename;
boolean pgp; boolean pgp;
Part part; Part part;
EntityAttachment attachment;
} }
MessageParts getMessageParts() throws IOException, FolderClosedException { MessageParts getMessageParts() throws IOException, FolderClosedException {
@ -1023,6 +996,43 @@ public class MessageHelper {
apart.filename = filename; apart.filename = filename;
apart.pgp = pgp; apart.pgp = pgp;
apart.part = part; apart.part = part;
ContentType ct;
try {
ct = new ContentType(apart.part.getContentType());
} catch (ParseException ex) {
Log.w(ex);
ct = new ContentType("application/octet-stream");
}
String[] cid = apart.part.getHeader("Content-ID");
apart.attachment = new EntityAttachment();
apart.attachment.name = apart.filename;
apart.attachment.type = ct.getBaseType().toLowerCase();
apart.attachment.disposition = apart.disposition;
apart.attachment.size = (long) apart.part.getSize();
apart.attachment.cid = (cid == null || cid.length == 0 ? null : MimeUtility.unfold(cid[0]));
apart.attachment.encryption = (apart.pgp ? EntityAttachment.PGP_MESSAGE : null);
if ("text/calendar".equalsIgnoreCase(apart.attachment.type) && TextUtils.isEmpty(apart.attachment.name))
apart.attachment.name = "invite.ics";
// Try to guess a better content type
// Sometimes PDF files are sent using the wrong type
if ("application/octet-stream".equalsIgnoreCase(apart.attachment.type)) {
String extension = Helper.getExtension(apart.attachment.name);
if (extension != null) {
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
if (type != null) {
Log.w("Guessing file=" + apart.attachment.name + " type=" + type);
apart.attachment.type = type;
}
}
}
if (apart.attachment.size < 0)
apart.attachment.size = null;
parts.attachments.add(apart); parts.attachments.add(apart);
} }
} }

Loading…
Cancel
Save