From 43a1af48798b4307f45f761741a9f7619a00bed6 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 6 Jan 2019 13:51:38 +0000 Subject: [PATCH] Skip sending unused inline images --- .../java/eu/faircode/email/MessageHelper.java | 74 ++++++++++++------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index f77b92cd5d..52f2440730 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -23,6 +23,9 @@ import android.content.Context; import android.text.TextUtils; import android.webkit.MimeTypeMap; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Element; + import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; @@ -279,8 +282,10 @@ public class MessageHelper { static void build(Context context, EntityMessage message, MimeMessage imessage) throws IOException, MessagingException { DB db = DB.getInstance(context); + String html = message.read(context); + StringBuilder body = new StringBuilder(); - body.append(message.read(context)); + body.append(html); if (Helper.isPro(context) && message.identity != null) { EntityIdentity identity = db.identity().getIdentity(message.identity); @@ -288,38 +293,51 @@ public class MessageHelper { body.append(identity.signature); } - String plain = HtmlHelper.getText(body.toString()); + String plainContent = HtmlHelper.getText(body.toString()); - StringBuilder html = new StringBuilder(); - html.append("").append("\n"); - html.append("").append("\n"); - html.append("").append("\n"); - html.append(" ").append("\n"); - html.append("").append("\n"); - html.append("").append("\n"); - html.append(body.toString()).append("\n"); - html.append("").append("\n"); - html.append("").append("\n"); + StringBuilder htmlContent = new StringBuilder(); + htmlContent.append("").append("\n"); + htmlContent.append("").append("\n"); + htmlContent.append("").append("\n"); + htmlContent.append(" ").append("\n"); + htmlContent.append("").append("\n"); + htmlContent.append("").append("\n"); + htmlContent.append(body.toString()).append("\n"); + htmlContent.append("").append("\n"); + htmlContent.append("").append("\n"); - BodyPart plainBody = new MimeBodyPart(); - plainBody.setContent(plain, "text/plain; charset=" + Charset.defaultCharset().name()); + BodyPart plainPart = new MimeBodyPart(); + plainPart.setContent(plainContent, "text/plain; charset=" + Charset.defaultCharset().name()); - BodyPart htmlBody = new MimeBodyPart(); - htmlBody.setContent(html.toString(), "text/html; charset=" + Charset.defaultCharset().name()); + BodyPart htmlPart = new MimeBodyPart(); + htmlPart.setContent(htmlContent.toString(), "text/html; charset=" + Charset.defaultCharset().name()); - Multipart alternative = new MimeMultipart("alternative"); - alternative.addBodyPart(plainBody); - alternative.addBodyPart(htmlBody); + Multipart alternativePart = new MimeMultipart("alternative"); + alternativePart.addBodyPart(plainPart); + alternativePart.addBodyPart(htmlPart); + + List cids = new ArrayList<>(); + for (Element element : Jsoup.parse(html).select("img")) { + String src = element.attr("src"); + if (src.startsWith("cid:")) + cids.add("<" + src.substring(4) + ">"); + } List attachments = db.attachment().getAttachments(message.id); - if (attachments.size() == 0) { - imessage.setContent(alternative); - } else { - Multipart multipart = new MimeMultipart("mixed"); + for (EntityAttachment attachment : new ArrayList<>(attachments)) + if (attachment.isInline() && attachment.cid != null && !cids.contains(attachment.cid)) { + Log.i("Removing unused inline attachment cid=" + attachment.cid); + attachments.remove(attachment); + } - BodyPart bp = new MimeBodyPart(); - bp.setContent(alternative); - multipart.addBodyPart(bp); + if (attachments.size() == 0) + imessage.setContent(alternativePart); + else { + Multipart mixedPart = new MimeMultipart("mixed"); + + BodyPart attachmentPart = new MimeBodyPart(); + attachmentPart.setContent(alternativePart); + mixedPart.addBodyPart(attachmentPart); for (final EntityAttachment attachment : attachments) if (attachment.available) { @@ -345,10 +363,10 @@ public class MessageHelper { if (attachment.cid != null) bpAttachment.setHeader("Content-ID", attachment.cid); - multipart.addBodyPart(bpAttachment); + mixedPart.addBodyPart(bpAttachment); } - imessage.setContent(multipart); + imessage.setContent(mixedPart); } }