Skip sending unused inline images

pull/147/head
M66B 6 years ago
parent b9d590362e
commit 43a1af4879

@ -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);
}
if (attachments.size() == 0)
imessage.setContent(alternativePart);
else {
Multipart mixedPart = new MimeMultipart("mixed");
BodyPart bp = new MimeBodyPart(); BodyPart attachmentPart = new MimeBodyPart();
bp.setContent(alternative); attachmentPart.setContent(alternativePart);
multipart.addBodyPart(bp); 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);
} }
} }

Loading…
Cancel
Save