Rewritten body part decoding

pull/147/head
M66B 6 years ago
parent f61a76af80
commit b8f6501088

@ -136,7 +136,7 @@ FairEmail uses:
* [JavaMail](https://projects.eclipse.org/projects/ee4j.javamail). Copyright (c) 1997-2018 Oracle® and/or its affiliates. All rights reserved. [GPLv2+CE license](https://javaee.github.io/javamail/JavaMail-License). * [JavaMail](https://projects.eclipse.org/projects/ee4j.javamail). Copyright (c) 1997-2018 Oracle® and/or its affiliates. All rights reserved. [GPLv2+CE license](https://javaee.github.io/javamail/JavaMail-License).
* [jsoup](https://jsoup.org/). Copyright © 2009 - 2017 Jonathan Hedley. [MIT license](https://jsoup.org/license). * [jsoup](https://jsoup.org/). Copyright © 2009 - 2017 Jonathan Hedley. [MIT license](https://jsoup.org/license).
* [JCharset](http://www.freeutils.net/source/jcharset/). Copyright © 2005-2015 Amichai Rothman. [GNU General Public License](http://www.freeutils.net/source/jcharset/#license) * ~~[JCharset](http://www.freeutils.net/source/jcharset/). Copyright © 2005-2015 Amichai Rothman. [GNU General Public License](http://www.freeutils.net/source/jcharset/#license)~~
* [Android Support Library](https://developer.android.com/tools/support-library/). Copyright (C) 2011 The Android Open Source Project. [Apache license](https://android.googlesource.com/platform/frameworks/support/+/master/LICENSE.txt). * [Android Support Library](https://developer.android.com/tools/support-library/). Copyright (C) 2011 The Android Open Source Project. [Apache license](https://android.googlesource.com/platform/frameworks/support/+/master/LICENSE.txt).
* [Android Architecture Components](https://developer.android.com/topic/libraries/architecture/). Copyright 2018 The Android Open Source Project, Inc. [Apache license](https://github.com/googlesamples/android-architecture-components/blob/master/LICENSE). * [Android Architecture Components](https://developer.android.com/topic/libraries/architecture/). Copyright 2018 The Android Open Source Project, Inc. [Apache license](https://github.com/googlesamples/android-architecture-components/blob/master/LICENSE).
* [colorpicker](https://android.googlesource.com/platform/frameworks/opt/colorpicker). Copyright (C) 2013 The Android Open Source Project. [Apache license](https://android.googlesource.com/platform/frameworks/opt/colorpicker/+/master/src/com/android/colorpicker/ColorPickerDialog.java). * [colorpicker](https://android.googlesource.com/platform/frameworks/opt/colorpicker). Copyright (C) 2013 The Android Open Source Project. [Apache license](https://android.googlesource.com/platform/frameworks/opt/colorpicker/+/master/src/com/android/colorpicker/ColorPickerDialog.java).

@ -158,7 +158,7 @@ dependencies {
implementation "org.jsoup:jsoup:$jsoup_version" implementation "org.jsoup:jsoup:$jsoup_version"
// http://www.freeutils.net/source/jcharset/ // http://www.freeutils.net/source/jcharset/
implementation "net.freeutils:jcharset:$jcharset_version" //implementation "net.freeutils:jcharset:$jcharset_version"
// http://www.xbill.org/dnsjava/ // http://www.xbill.org/dnsjava/
implementation "dnsjava:dnsjava:$dnsjava_version" implementation "dnsjava:dnsjava:$dnsjava_version"

@ -1215,13 +1215,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
ByteArrayInputStream is = new ByteArrayInputStream(decrypted.toByteArray()); ByteArrayInputStream is = new ByteArrayInputStream(decrypted.toByteArray());
MimeMessage imessage = new MimeMessage(isession, is); MimeMessage imessage = new MimeMessage(isession, is);
MessageHelper helper = new MessageHelper(imessage); MessageHelper helper = new MessageHelper(imessage);
MessageHelper.MessageParts parts = helper.getMessageParts();
try { try {
db.beginTransaction(); db.beginTransaction();
// Write decrypted body // Write decrypted body
EntityMessage m = db.message().getMessage(id); EntityMessage m = db.message().getMessage(id);
m.write(context, helper.getHtml()); m.write(context, parts.getHtml());
// Remove previously decrypted attachments // Remove previously decrypted attachments
for (EntityAttachment a : attachments) for (EntityAttachment a : attachments)
@ -1230,7 +1231,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
// Add decrypted attachments // Add decrypted attachments
int sequence = db.attachment().getAttachmentSequence(id); int sequence = db.attachment().getAttachmentSequence(id);
for (EntityAttachment a : helper.getAttachments()) { for (EntityAttachment a : parts.getAttachments()) {
a.message = id; a.message = id;
a.sequence = ++sequence; a.sequence = ++sequence;
a.id = db.attachment().insertAttachment(a); a.id = db.attachment().insertAttachment(a);

@ -21,23 +21,16 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Part; import javax.mail.Part;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.ForeignKey; import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index; import androidx.room.Index;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
@ -56,7 +49,6 @@ import static androidx.room.ForeignKey.CASCADE;
) )
public class EntityAttachment { public class EntityAttachment {
static final String TABLE_NAME = "attachment"; static final String TABLE_NAME = "attachment";
static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes
static final Integer PGP_MESSAGE = 1; static final Integer PGP_MESSAGE = 1;
static final Integer PGP_SIGNATURE = 2; static final Integer PGP_SIGNATURE = 2;
@ -78,9 +70,6 @@ public class EntityAttachment {
@NonNull @NonNull
public Boolean available = false; public Boolean available = false;
@Ignore
BodyPart part;
boolean isInline() { boolean isInline() {
return (disposition != null && disposition.equalsIgnoreCase(Part.INLINE)); return (disposition != null && disposition.equalsIgnoreCase(Part.INLINE));
} }
@ -104,49 +93,6 @@ public class EntityAttachment {
} }
} }
void download(Context context, DB db) throws MessagingException, IOException {
// Build filename
File file = EntityAttachment.getFile(context, this.id);
// Download attachment
InputStream is = null;
OutputStream os = null;
try {
db.attachment().setProgress(this.id, null);
is = this.part.getInputStream();
os = new BufferedOutputStream(new FileOutputStream(file));
long size = 0;
byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE];
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
size += len;
os.write(buffer, 0, len);
// Update progress
if (this.size != null)
db.attachment().setProgress(this.id, (int)(size * 100 / this.size));
}
// Store attachment data
db.attachment().setDownloaded(this.id, size);
Log.i("Downloaded attachment size=" + this.size);
} catch (IOException ex) {
// Reset progress on failure
db.attachment().setProgress(this.id, null);
throw ex;
} finally {
try {
if (is != null)
is.close();
} finally {
if (os != null)
os.close();
}
}
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof EntityAttachment) { if (obj instanceof EntityAttachment) {
@ -164,5 +110,4 @@ public class EntityAttachment {
} else } else
return false; return false;
} }
} }

@ -1284,7 +1284,7 @@ public class FragmentCompose extends FragmentBase {
is = context.getContentResolver().openInputStream(uri); is = context.getContentResolver().openInputStream(uri);
os = new BufferedOutputStream(new FileOutputStream(file)); os = new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer = new byte[EntityAttachment.ATTACHMENT_BUFFER_SIZE]; byte[] buffer = new byte[MessageHelper.ATTACHMENT_BUFFER_SIZE];
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
size += len; size += len;
os.write(buffer, 0, len); os.write(buffer, 0, len);
@ -1575,8 +1575,7 @@ public class FragmentCompose extends FragmentBase {
result.draft.id = db.message().insertMessage(result.draft); result.draft.id = db.message().insertMessage(result.draft);
result.draft.write(context, body == null ? "" : body); result.draft.write(context, body == null ? "" : body);
db.message().setMessageContent( db.message().setMessageContent(result.draft.id, true, HtmlHelper.getPreview(body));
result.draft.id, true, HtmlHelper.getPreview(body));
if ("new".equals(action)) { if ("new".equals(action)) {
ArrayList<Uri> uris = args.getParcelableArrayList("attachments"); ArrayList<Uri> uris = args.getParcelableArrayList("attachments");

@ -26,12 +26,15 @@ import android.webkit.MimeTypeMap;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import java.io.BufferedOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
@ -65,6 +68,8 @@ public class MessageHelper {
private final static int FETCH_SIZE = 1024 * 1024; // bytes, default 16K private final static int FETCH_SIZE = 1024 * 1024; // bytes, default 16K
private final static int POOL_TIMEOUT = 3 * 60 * 1000; // milliseconds, default 45 sec private final static int POOL_TIMEOUT = 3 * 60 * 1000; // milliseconds, default 45 sec
static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes
static Properties getSessionProperties(int auth_type, String realm, boolean insecure) { static Properties getSessionProperties(int auth_type, String realm, boolean insecure) {
Properties props = new Properties(); Properties props = new Properties();
@ -526,145 +531,172 @@ public class MessageHelper {
return address.getAddress(); return address.getAddress();
} }
String getHtml() throws MessagingException, IOException { class MessageParts {
return getHtml(imessage); private Part plain = null;
} private Part html = null;
private List<AttachmentPart> attachments = new ArrayList<>();
private static String readStream(InputStream is, String charset) throws IOException { String getHtml() throws MessagingException {
ByteArrayOutputStream os = new ByteArrayOutputStream(); if (plain == null && html == null)
byte[] buffer = new byte[4096]; return null;
for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len);
return new String(os.toByteArray(), charset);
}
private static String getHtml(Part part) throws MessagingException, IOException { String result;
String disposition; boolean text = false;
try { Part part = (html == null ? plain : html);
disposition = part.getDisposition();
} catch (MessagingException ex) {
Log.w(ex);
disposition = null;
}
if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) &&
(part.isMimeType("text/plain") || part.isMimeType("text/html"))) {
String s;
try { try {
Object content = part.getContent(); Object content = part.getContent();
try {
if (content instanceof String) if (content instanceof String)
s = (String) content; result = (String) content;
else if (content instanceof InputStream) else if (content instanceof InputStream)
// Typically com.sun.mail.util.QPDecoderStream // Typically com.sun.mail.util.QPDecoderStream
s = readStream((InputStream) content, "UTF-8"); result = readStream((InputStream) content, "UTF-8");
else else
s = content.toString(); result = content.toString();
} catch (UnsupportedEncodingException ex) { } catch (Throwable ex) {
// x-binaryenc
// https://javaee.github.io/javamail/FAQ#unsupen
Log.w("Unsupported encoding: " + part.getContentType());
return readStream(part.getInputStream(), "US-ASCII");
}
} catch (IOException ex) {
// IOException; Unknown encoding: none
Log.w(ex); Log.w(ex);
return "<pre>" + ex + "<br />" + android.util.Log.getStackTraceString(ex) + "</pre>"; text = true;
result = ex + "\n" + android.util.Log.getStackTraceString(ex);
} }
if (part.isMimeType("text/plain")) if (part.isMimeType("text/plain") || text)
s = "<pre>" + s.replaceAll("\\r?\\n", "<br />") + "</pre>"; result = "<pre>" + result.replaceAll("\\r?\\n", "<br />") + "</pre>";
return s; if (part.isMimeType("text/plain")) {
Log.i("Plain text");
return result;
} else {
Log.i("HTML text");
return result;
}
} }
if (part.isMimeType("multipart/alternative")) { List<EntityAttachment> getAttachments() throws MessagingException {
String text = null; List<EntityAttachment> result = new ArrayList<>();
try {
Multipart mp = (Multipart) part.getContent(); for (AttachmentPart apart : attachments) {
for (int i = 0; i < mp.getCount(); i++) { ContentType ct = new ContentType(apart.part.getContentType());
Part bp = mp.getBodyPart(i); String[] cid = apart.part.getHeader("Content-ID");
if (bp.isMimeType("text/plain")) {
if (text == null) EntityAttachment attachment = new EntityAttachment();
text = getHtml(bp); attachment.name = apart.filename;
} else if (bp.isMimeType("text/html")) { attachment.type = ct.getBaseType().toLowerCase();
String s = getHtml(bp); attachment.disposition = apart.disposition;
if (s != null) attachment.size = (long) apart.part.getSize();
return s; attachment.cid = (cid == null || cid.length == 0 ? null : cid[0]);
} else attachment.encryption = (apart.pgp ? EntityAttachment.PGP_MESSAGE : null);
return getHtml(bp);
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;
} }
} catch (ParseException ex) {
// ParseException: In parameter list boundary="...">, expected parameter name, got ";"
Log.w(ex);
text = "<pre>" + ex + "<br />" + android.util.Log.getStackTraceString(ex) + "</pre>";
} }
return text;
} }
if (part.isMimeType("multipart/*")) if (attachment.size < 0)
try { attachment.size = null;
Multipart mp = (Multipart) part.getContent();
for (int i = 0; i < mp.getCount(); i++) { result.add(attachment);
String s = getHtml(mp.getBodyPart(i)); }
if (s != null)
return s; // Fix duplicate CIDs
for (int i = 0; i < result.size(); i++) {
String cid = result.get(i).cid;
if (cid != null)
for (int j = i + 1; j < result.size(); j++) {
EntityAttachment a = result.get(j);
if (cid.equals(a.cid))
a.cid = null;
} }
} catch (ParseException ex) {
Log.w(ex);
return "<pre>" + ex + "<br />" + android.util.Log.getStackTraceString(ex) + "</pre>";
} }
return null; return result;
} }
public List<EntityAttachment> getAttachments() throws IOException, MessagingException { void downloadAttachment(Context context, DB db, long id, int sequence) throws MessagingException, IOException {
List<EntityAttachment> result = new ArrayList<>(); // Attachments of drafts might not have been uploaded yet
if (sequence > attachments.size()) {
Log.w("Attachment unavailable sequence=" + sequence + " size=" + attachments.size());
return;
}
// Get data
AttachmentPart apart = attachments.get(sequence - 1);
long total = apart.part.getSize();
File file = EntityAttachment.getFile(context, id);
// Download attachment
OutputStream os = null;
try { try {
Object content = imessage.getContent(); db.attachment().setProgress(id, null);
if (content instanceof String)
return result;
if (content instanceof Multipart) { InputStream is = apart.part.getInputStream();
boolean pgp = false; os = new BufferedOutputStream(new FileOutputStream(file));
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++) { long size = 0;
BodyPart part = multipart.getBodyPart(i); byte[] buffer = new byte[ATTACHMENT_BUFFER_SIZE];
result.addAll(getAttachments(part, pgp)); for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
ContentType ct = new ContentType(part.getContentType()); size += len;
if ("application/pgp-encrypted".equals(ct.getBaseType().toLowerCase())) os.write(buffer, 0, len);
pgp = true;
} // Update progress
if (total > 0)
db.attachment().setProgress(id, (int) (size * 100 / total));
} }
// Store attachment data
db.attachment().setDownloaded(id, size);
Log.i("Downloaded attachment size=" + size);
} catch (IOException ex) { } catch (IOException ex) {
if (ex.getCause() instanceof MessagingException) // Reset progress on failure
Log.w(ex); db.attachment().setProgress(id, null);
else
throw ex; throw ex;
} catch (ParseException ex) { } finally {
Log.w(ex); if (os != null)
os.close();
}
}
} }
return result; private class AttachmentPart {
String disposition;
String filename;
boolean pgp;
Part part;
} }
private static List<EntityAttachment> getAttachments(BodyPart part, boolean pgp) throws MessageParts getMessageParts() throws IOException, MessagingException {
IOException, MessagingException { MessageParts parts = new MessageParts();
List<EntityAttachment> result = new ArrayList<>(); getMessageParts(imessage, parts, false); // Can throw ParseException
return parts;
}
Object content; private void getMessageParts(Part part, MessageParts parts, boolean pgp) throws MessagingException, IOException {
if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
for (int i = 0; i < multipart.getCount(); i++)
try { try {
content = part.getContent(); Part cpart = multipart.getBodyPart(i);
} catch (UnsupportedEncodingException ex) { getMessageParts(cpart, parts, pgp);
Log.w("attachment content type=" + part.getContentType()); ContentType ct = new ContentType(cpart.getContentType());
content = part.getInputStream(); if ("application/pgp-encrypted".equals(ct.getBaseType().toLowerCase()))
pgp = true;
} catch (ParseException ex) { } catch (ParseException ex) {
// Nested body: try to continue
// ParseException: In parameter list boundary="...">, expected parameter name, got ";"
Log.w(ex); Log.w(ex);
content = null;
} }
} else {
if (content instanceof InputStream || content instanceof String) {
// https://www.iana.org/assignments/cont-disp/cont-disp.xhtml // https://www.iana.org/assignments/cont-disp/cont-disp.xhtml
String disposition; String disposition;
try { try {
@ -682,54 +714,35 @@ public class MessageHelper {
filename = null; filename = null;
} }
if (Part.ATTACHMENT.equalsIgnoreCase(disposition) || Log.i("Part" +
!(part.isMimeType("text/plain") || part.isMimeType("text/html")) || " disposition=" + disposition +
!TextUtils.isEmpty(filename)) { " filename=" + filename +
ContentType ct = new ContentType(part.getContentType()); " content type=" + part.getContentType());
String[] cid = part.getHeader("Content-ID");
EntityAttachment attachment = new EntityAttachment(); if (!Part.ATTACHMENT.equalsIgnoreCase(disposition) &&
attachment.name = filename; ((parts.plain == null && part.isMimeType("text/plain")) ||
attachment.type = ct.getBaseType().toLowerCase(); (parts.html == null && part.isMimeType("text/html")))) {
attachment.disposition = disposition; if (part.isMimeType("text/plain"))
attachment.size = (long) part.getSize(); parts.plain = part;
attachment.cid = (cid == null || cid.length == 0 ? null : cid[0]); else
attachment.encryption = (pgp ? EntityAttachment.PGP_MESSAGE : null); parts.html = part;
attachment.part = part; } else {
AttachmentPart apart = new AttachmentPart();
if (TextUtils.isEmpty(attachment.name) && "text/calendar".equals(attachment.type)) apart.disposition = disposition;
attachment.name = "invite.ics"; apart.filename = filename;
apart.pgp = pgp;
// Try to guess a better content type apart.part = part;
// Sometimes PDF files are sent using the wrong type parts.attachments.add(apart);
if ("application/octet-stream".equals(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);
} }
} else if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart cpart = multipart.getBodyPart(i);
result.addAll(getAttachments(cpart, pgp));
ContentType ct = new ContentType(cpart.getContentType());
if ("application/pgp-encrypted".equals(ct.getBaseType().toLowerCase()))
pgp = true;
} }
} }
return result; private static String readStream(InputStream is, String charset) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len);
return new String(os.toByteArray(), charset);
} }
static boolean equal(Address[] a1, Address[] a2) { static boolean equal(Address[] a1, Address[] a2) {

@ -1932,7 +1932,8 @@ public class ServiceSynchronize extends LifecycleService {
throw new MessageRemovedException(); throw new MessageRemovedException();
MessageHelper helper = new MessageHelper((MimeMessage) imessage); MessageHelper helper = new MessageHelper((MimeMessage) imessage);
String body = helper.getHtml(); MessageHelper.MessageParts parts = helper.getMessageParts();
String body = parts.getHtml();
String preview = HtmlHelper.getPreview(body); String preview = HtmlHelper.getPreview(body);
message.write(this, body); message.write(this, body);
db.message().setMessageContent(message.id, true, preview); db.message().setMessageContent(message.id, true, preview);
@ -1954,14 +1955,8 @@ public class ServiceSynchronize extends LifecycleService {
// Download attachment // Download attachment
MessageHelper helper = new MessageHelper((MimeMessage) imessage); MessageHelper helper = new MessageHelper((MimeMessage) imessage);
List<EntityAttachment> attachments = helper.getAttachments(); MessageHelper.MessageParts parts = helper.getMessageParts();
if (sequence - 1 < attachments.size()) { parts.downloadAttachment(this, db, attachment.id, sequence);
attachment.part = attachments.get(sequence - 1).part;
attachment.download(this, db);
} else {
db.attachment().setProgress(attachment.id, null);
throw new IllegalArgumentException("Attachment not found seq=" + sequence + " size=" + attachments.size());
}
} }
private void synchronizeFolders(EntityAccount account, IMAPStore istore, ServiceState state) throws MessagingException { private void synchronizeFolders(EntityAccount account, IMAPStore istore, ServiceState state) throws MessagingException {
@ -2480,15 +2475,11 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(folder.name + " added id=" + message.id + " uid=" + message.uid); Log.i(folder.name + " added id=" + message.id + " uid=" + message.uid);
int sequence = 1; int sequence = 1;
for (EntityAttachment attachment : helper.getAttachments()) { MessageHelper.MessageParts parts = helper.getMessageParts();
for (EntityAttachment attachment : parts.getAttachments()) {
Log.i(folder.name + " attachment seq=" + sequence + Log.i(folder.name + " attachment seq=" + sequence +
" name=" + attachment.name + " type=" + attachment.type + " name=" + attachment.name + " type=" + attachment.type +
" cid=" + attachment.cid + " pgp=" + attachment.encryption); " cid=" + attachment.cid + " pgp=" + attachment.encryption);
if (!TextUtils.isEmpty(attachment.cid) &&
db.attachment().getAttachment(message.id, attachment.cid) != null) {
Log.i("Skipping duplicated CID");
continue;
}
attachment.message = message.id; attachment.message = message.id;
attachment.sequence = sequence++; attachment.sequence = sequence++;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
@ -2600,29 +2591,21 @@ public class ServiceSynchronize extends LifecycleService {
ifolder.fetch(new Message[]{imessage}, fp); ifolder.fetch(new Message[]{imessage}, fp);
} }
MessageHelper.MessageParts parts = helper.getMessageParts();
if (!message.content) if (!message.content)
if (!metered || (message.size != null && message.size < maxSize)) { if (!metered || (message.size != null && message.size < maxSize)) {
String body = helper.getHtml(); String body = parts.getHtml();
message.write(context, body); message.write(context, body);
db.message().setMessageContent( db.message().setMessageContent(message.id, true, HtmlHelper.getPreview(body));
message.id, true, HtmlHelper.getPreview(body));
Log.i(folder.name + " downloaded message id=" + message.id + " size=" + message.size); Log.i(folder.name + " downloaded message id=" + message.id + " size=" + message.size);
} }
List<EntityAttachment> iattachments = null;
for (int i = 0; i < attachments.size(); i++) { for (int i = 0; i < attachments.size(); i++) {
EntityAttachment attachment = attachments.get(i); EntityAttachment attachment = attachments.get(i);
if (!attachment.available) if (!attachment.available)
if (!metered || (attachment.size != null && attachment.size < maxSize)) { if (!metered || (attachment.size != null && attachment.size < maxSize))
if (iattachments == null) parts.downloadAttachment(context, db, attachment.id, attachment.sequence);
iattachments = helper.getAttachments();
// Attachments of drafts might not have been uploaded yet
if (i < iattachments.size()) {
attachment.part = iattachments.get(i).part;
attachment.download(context, db);
Log.i(folder.name + " downloaded message id=" + message.id + " attachment=" + attachment.name + " size=" + message.size);
}
}
} }
} }
} }

@ -0,0 +1,24 @@
package eu.faircode.email;
import java.nio.charset.Charset;
import java.nio.charset.spi.CharsetProvider;
import java.util.Collections;
import java.util.Iterator;
public class UnknownCharsetProvider extends CharsetProvider {
@Override
public Iterator<Charset> charsets() {
return Collections.emptyIterator();
}
@Override
public Charset charsetForName(String name) {
// x-binaryenc
// UseInqueCodePage
// none
// unknown-8bit
// https://javaee.github.io/javamail/FAQ#unsupen
Log.w("Unknown charset=" + name);
return Charset.forName("US-ASCII");
}
}
Loading…
Cancel
Save