Skip sending unused inline images

This commit is contained in:
M66B 2019-01-06 13:51:38 +00:00
parent b9d590362e
commit 43a1af4879
1 changed files with 46 additions and 28 deletions

View File

@ -23,6 +23,9 @@ import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -279,8 +282,10 @@ public class MessageHelper {
static void build(Context context, EntityMessage message, MimeMessage imessage) throws IOException, MessagingException { static void build(Context context, EntityMessage message, MimeMessage imessage) throws IOException, MessagingException {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
String html = message.read(context);
StringBuilder body = new StringBuilder(); StringBuilder body = new StringBuilder();
body.append(message.read(context)); body.append(html);
if (Helper.isPro(context) && message.identity != null) { if (Helper.isPro(context) && message.identity != null) {
EntityIdentity identity = db.identity().getIdentity(message.identity); EntityIdentity identity = db.identity().getIdentity(message.identity);
@ -288,38 +293,51 @@ public class MessageHelper {
body.append(identity.signature); body.append(identity.signature);
} }
String plain = HtmlHelper.getText(body.toString()); String plainContent = HtmlHelper.getText(body.toString());
StringBuilder html = new StringBuilder(); StringBuilder htmlContent = new StringBuilder();
html.append("<!DOCTYPE html>").append("\n"); htmlContent.append("<!DOCTYPE html>").append("\n");
html.append("<html>").append("\n"); htmlContent.append("<html>").append("\n");
html.append("<head>").append("\n"); htmlContent.append("<head>").append("\n");
html.append("<meta charset=\"utf-8\" /> ").append("\n"); htmlContent.append("<meta charset=\"utf-8\" /> ").append("\n");
html.append("</head>").append("\n"); htmlContent.append("</head>").append("\n");
html.append("<body>").append("\n"); htmlContent.append("<body>").append("\n");
html.append(body.toString()).append("\n"); htmlContent.append(body.toString()).append("\n");
html.append("</body>").append("\n"); htmlContent.append("</body>").append("\n");
html.append("</html>").append("\n"); htmlContent.append("</html>").append("\n");
BodyPart plainBody = new MimeBodyPart(); BodyPart plainPart = new MimeBodyPart();
plainBody.setContent(plain, "text/plain; charset=" + Charset.defaultCharset().name()); plainPart.setContent(plainContent, "text/plain; charset=" + Charset.defaultCharset().name());
BodyPart htmlBody = new MimeBodyPart(); BodyPart htmlPart = new MimeBodyPart();
htmlBody.setContent(html.toString(), "text/html; charset=" + Charset.defaultCharset().name()); htmlPart.setContent(htmlContent.toString(), "text/html; charset=" + Charset.defaultCharset().name());
Multipart alternative = new MimeMultipart("alternative"); Multipart alternativePart = new MimeMultipart("alternative");
alternative.addBodyPart(plainBody); alternativePart.addBodyPart(plainPart);
alternative.addBodyPart(htmlBody); alternativePart.addBodyPart(htmlPart);
List<String> 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<EntityAttachment> attachments = db.attachment().getAttachments(message.id); List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
if (attachments.size() == 0) { for (EntityAttachment attachment : new ArrayList<>(attachments))
imessage.setContent(alternative); if (attachment.isInline() && attachment.cid != null && !cids.contains(attachment.cid)) {
} else { Log.i("Removing unused inline attachment cid=" + attachment.cid);
Multipart multipart = new MimeMultipart("mixed"); attachments.remove(attachment);
}
BodyPart bp = new MimeBodyPart(); if (attachments.size() == 0)
bp.setContent(alternative); imessage.setContent(alternativePart);
multipart.addBodyPart(bp); else {
Multipart mixedPart = new MimeMultipart("mixed");
BodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setContent(alternativePart);
mixedPart.addBodyPart(attachmentPart);
for (final EntityAttachment attachment : attachments) for (final EntityAttachment attachment : attachments)
if (attachment.available) { if (attachment.available) {
@ -345,10 +363,10 @@ public class MessageHelper {
if (attachment.cid != null) if (attachment.cid != null)
bpAttachment.setHeader("Content-ID", attachment.cid); bpAttachment.setHeader("Content-ID", attachment.cid);
multipart.addBodyPart(bpAttachment); mixedPart.addBodyPart(bpAttachment);
} }
imessage.setContent(multipart); imessage.setContent(mixedPart);
} }
} }