From 43a892cc9e1b689c6aa0a80a76f2ddcfeef9f949 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 23 Feb 2020 19:45:05 +0100 Subject: [PATCH] Show signature images in sent messages --- .../main/java/eu/faircode/email/Helper.java | 25 ++++++ .../java/eu/faircode/email/MessageHelper.java | 83 ++++++++++--------- 2 files changed, 70 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 93d9b681dc..2b3161a469 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -834,6 +834,31 @@ public class Helper { out.write(buf, 0, len); } + static long copy(Context context, Uri uri, File file) throws IOException { + long size = 0; + InputStream is = null; + OutputStream os = null; + try { + is = context.getContentResolver().openInputStream(uri); + os = new FileOutputStream(file); + + byte[] buffer = new byte[Helper.BUFFER_SIZE]; + for (int len = is.read(buffer); len != -1; len = is.read(buffer)) { + size += len; + os.write(buffer, 0, len); + } + } finally { + try { + if (is != null) + is.close(); + } finally { + if (os != null) + os.close(); + } + } + return size; + } + static long getAvailableStorageSpace() { StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath()); return stats.getAvailableBlocksLong() * stats.getBlockSizeLong(); diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 1962ae874b..585d66822f 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -63,7 +63,6 @@ import java.util.TimeZone; import java.util.regex.Pattern; import javax.activation.DataHandler; -import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.activation.FileTypeMap; import javax.mail.Address; @@ -499,48 +498,59 @@ public class MessageHelper { Document document = JsoupEx.parse(message.getFile(context)); // When sending message - List contents = new ArrayList<>(); if (identity != null) { document.select("div[fairemail=signature],div[fairemail=reference]").removeAttr("fairemail"); - for (Element img : document.select("img")) { - String source = img.attr("src"); - if (!source.startsWith("content:")) - continue; + DB db = DB.getInstance(context); + try { + db.beginTransaction(); - String cid = BuildConfig.APPLICATION_ID + ".content." + contents.size(); - img.attr("src", "cid:" + cid); + for (Element img : document.select("img")) { + String source = img.attr("src"); + if (!source.startsWith("content:")) + continue; - final Uri uri = Uri.parse(source); - final DocumentFile dfile = DocumentFile.fromSingleUri(context, uri); + Uri uri = Uri.parse(source); + DocumentFile dfile = DocumentFile.fromSingleUri(context, uri); + if (dfile == null) + continue; - BodyPart part = new MimeBodyPart(); - part.setFileName(uri.getLastPathSegment()); - part.setDisposition(Part.INLINE); - part.setHeader("Content-ID", "<" + cid + ">"); - part.setDataHandler(new DataHandler(new DataSource() { - @Override - public InputStream getInputStream() throws IOException { - return context.getContentResolver().openInputStream(uri); - } + String name = dfile.getName(); + String type = dfile.getType(); - @Override - public OutputStream getOutputStream() throws IOException { - return null; - } + if (TextUtils.isEmpty(name)) + name = uri.getLastPathSegment(); + if (TextUtils.isEmpty(type)) + type = "image/*"; - @Override - public String getContentType() { - return dfile.getType(); - } + EntityAttachment attachment = new EntityAttachment(); + attachment.message = message.id; + attachment.sequence = db.attachment().getAttachmentSequence(message.id) + 1; + attachment.name = name; + attachment.type = type; + attachment.disposition = Part.INLINE; + attachment.cid = null; + attachment.size = null; + attachment.progress = 0; - @Override - public String getName() { - return uri.getLastPathSegment(); - } - })); + attachment.id = db.attachment().insertAttachment(attachment); - contents.add(part); + String cid = BuildConfig.APPLICATION_ID + "." + attachment.id; + attachment.cid = "<" + BuildConfig.APPLICATION_ID + "." + attachment.id + ">"; + db.attachment().setCid(attachment.id, attachment.cid); + + attachment.size = Helper.copy(context, uri, attachment.getFile(context)); + attachment.progress = null; + attachment.available = true; + db.attachment().setDownloaded(attachment.id, attachment.size); + + attachments.add(attachment); + img.attr("src", "cid:" + cid); + } + + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); } } @@ -565,8 +575,8 @@ public class MessageHelper { altMultiPart.addBodyPart(plainPart); altMultiPart.addBodyPart(htmlPart); - int availableAttachments = contents.size(); - boolean hasInline = contents.size() > 0; + int availableAttachments = 0; + boolean hasInline = false; for (EntityAttachment attachment : attachments) if (attachment.available) { availableAttachments++; @@ -645,9 +655,6 @@ public class MessageHelper { mixedMultiPart.addBodyPart(attachmentPart); } - for (BodyPart content : contents) - relatedMultiPart.addBodyPart(content); - imessage.setContent(mixedMultiPart); } }