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")
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" +
" SET error = NULL, progress = :progress, available = 0" +
" WHERE id = :id")

@ -822,48 +822,11 @@ public class MessageHelper {
return attachments;
}
List<EntityAttachment> getAttachments() throws MessagingException {
List<EntityAttachment> getAttachments() {
List<EntityAttachment> result = new ArrayList<>();
for (AttachmentPart apart : attachments) {
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");
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);
}
for (AttachmentPart apart : attachments)
result.add(apart.attachment);
// Fix duplicate CIDs
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 {
Log.i("downloading attchment id=" + id + " seq=" + index);
// 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());
return;
}
@ -894,9 +858,17 @@ public class MessageHelper {
EntityAttachment attachment = db.attachment().getAttachment(id);
if (attachment == null)
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
File file = attachment.getFile(context);
db.attachment().setProgress(id, null);
try (InputStream is = apart.part.getInputStream()) {
long size = 0;
@ -942,6 +914,7 @@ public class MessageHelper {
String filename;
boolean pgp;
Part part;
EntityAttachment attachment;
}
MessageParts getMessageParts() throws IOException, FolderClosedException {
@ -1023,6 +996,43 @@ public class MessageHelper {
apart.filename = filename;
apart.pgp = pgp;
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);
}
}

Loading…
Cancel
Save