From ae1a36b482c370ccf4af70c298e2749aa316f251 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 6 Jan 2019 10:15:06 +0000 Subject: [PATCH] Reset unused cids, refactoring --- .../java/eu/faircode/email/DaoAttachment.java | 12 ++- .../eu/faircode/email/EntityAttachment.java | 6 +- .../eu/faircode/email/EntityOperation.java | 3 +- .../eu/faircode/email/FragmentCompose.java | 18 +--- .../main/java/eu/faircode/email/Helper.java | 97 ++++++++----------- .../java/eu/faircode/email/HtmlHelper.java | 13 +++ .../eu/faircode/email/ServiceSynchronize.java | 20 +++- 7 files changed, 88 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoAttachment.java b/app/src/main/java/eu/faircode/email/DaoAttachment.java index 19b9124ebd..345bc1abed 100644 --- a/app/src/main/java/eu/faircode/email/DaoAttachment.java +++ b/app/src/main/java/eu/faircode/email/DaoAttachment.java @@ -60,10 +60,20 @@ public interface DaoAttachment { EntityAttachment getAttachment(long message, String cid); @Query("UPDATE attachment" + - " SET progress = :progress" + + " SET progress = :progress, available = 0" + " WHERE id = :id") void setProgress(long id, Integer progress); + @Query("UPDATE attachment" + + " SET size = :size, progress = NULL, available = 1" + + " WHERE id = :id") + void setDownloaded(long id, Integer size); + + @Query("UPDATE attachment" + + " SET cid = NULL" + + " WHERE id = :id") + void clearCid(long id); + @Insert long insertAttachment(EntityAttachment attachment); diff --git a/app/src/main/java/eu/faircode/email/EntityAttachment.java b/app/src/main/java/eu/faircode/email/EntityAttachment.java index d44d8d9d24..17e8c24c13 100644 --- a/app/src/main/java/eu/faircode/email/EntityAttachment.java +++ b/app/src/main/java/eu/faircode/email/EntityAttachment.java @@ -107,7 +107,7 @@ public class EntityAttachment { OutputStream os = null; try { this.progress = null; - db.attachment().updateAttachment(this); + db.attachment().setProgress(this.id, null); is = this.part.getInputStream(); os = new BufferedOutputStream(new FileOutputStream(file)); @@ -127,13 +127,13 @@ public class EntityAttachment { this.size = size; this.progress = null; this.available = true; - db.attachment().updateAttachment(this); + db.attachment().setDownloaded(this.id, size); Log.i("Downloaded attachment size=" + this.size); } catch (IOException ex) { // Reset progress on failure this.progress = null; - db.attachment().updateAttachment(this); + db.attachment().setProgress(this.id, null); throw ex; } finally { try { diff --git a/app/src/main/java/eu/faircode/email/EntityOperation.java b/app/src/main/java/eu/faircode/email/EntityOperation.java index 1b8327389e..5c2ebe1212 100644 --- a/app/src/main/java/eu/faircode/email/EntityOperation.java +++ b/app/src/main/java/eu/faircode/email/EntityOperation.java @@ -184,8 +184,7 @@ public class EntityOperation { EntityAttachment.getFile(context, attachment.id)); } catch (IOException ex) { Log.e(ex); - attachment.available = false; - db.attachment().updateAttachment(attachment); + db.attachment().setProgress(attachment.id, null); } } } diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 8e8d311b30..61190fdac7 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -843,10 +843,7 @@ public class FragmentCompose extends FragmentEx { os1 = new BufferedOutputStream(new FileOutputStream(file1)); os1.write(bytes1); - attachment1.size = bytes1.length; - attachment1.progress = null; - attachment1.available = true; - db.attachment().updateAttachment(attachment1); + db.attachment().setDownloaded(attachment1.id, bytes1.length); } finally { if (os1 != null) os1.close(); @@ -868,10 +865,7 @@ public class FragmentCompose extends FragmentEx { os2 = new BufferedOutputStream(new FileOutputStream(file2)); os2.write(bytes2); - attachment2.size = bytes2.length; - attachment2.progress = null; - attachment2.available = true; - db.attachment().updateAttachment(attachment2); + db.attachment().setDownloaded(attachment2.id, bytes2.length); } finally { if (os2 != null) os2.close(); @@ -1186,10 +1180,7 @@ public class FragmentCompose extends FragmentEx { if (image) attachment.cid = "<" + BuildConfig.APPLICATION_ID + "." + attachment.id + ">"; - attachment.size = size; - attachment.progress = null; - attachment.available = true; - db.attachment().updateAttachment(attachment); + db.attachment().setDownloaded(attachment.id, size); } finally { try { if (is != null) @@ -1201,8 +1192,7 @@ public class FragmentCompose extends FragmentEx { } } catch (IOException ex) { // Reset progress on failure - attachment.progress = null; - db.attachment().updateAttachment(attachment); + db.attachment().setProgress(attachment.id, null); throw ex; } diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 3325611763..616206c1d3 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -379,17 +379,17 @@ public class Helper { private static void attachSettings(Context context, long id, int sequence) throws IOException { DB db = DB.getInstance(context); - EntityAttachment ops = new EntityAttachment(); - ops.message = id; - ops.sequence = sequence; - ops.name = "settings.txt"; - ops.type = "text/plain"; - ops.size = null; - ops.progress = 0; - ops.id = db.attachment().insertAttachment(ops); + EntityAttachment attachment = new EntityAttachment(); + attachment.message = id; + attachment.sequence = sequence; + attachment.name = "settings.txt"; + attachment.type = "text/plain"; + attachment.size = null; + attachment.progress = 0; + attachment.id = db.attachment().insertAttachment(attachment); OutputStream os = null; - File file = EntityAttachment.getFile(context, ops.id); + File file = EntityAttachment.getFile(context, attachment.id); try { os = new BufferedOutputStream(new FileOutputStream(file)); @@ -400,10 +400,7 @@ public class Helper { for (String key : settings.keySet()) size += write(os, key + "=" + settings.get(key) + "\r\n"); - ops.size = size; - ops.progress = null; - ops.available = true; - db.attachment().updateAttachment(ops); + db.attachment().setDownloaded(attachment.id, size); } finally { if (os != null) os.close(); @@ -413,17 +410,17 @@ public class Helper { private static void attachNetworkInfo(Context context, long id, int sequence) throws IOException { DB db = DB.getInstance(context); - EntityAttachment ops = new EntityAttachment(); - ops.message = id; - ops.sequence = sequence; - ops.name = "network.txt"; - ops.type = "text/plain"; - ops.size = null; - ops.progress = 0; - ops.id = db.attachment().insertAttachment(ops); + EntityAttachment attachment = new EntityAttachment(); + attachment.message = id; + attachment.sequence = sequence; + attachment.name = "network.txt"; + attachment.type = "text/plain"; + attachment.size = null; + attachment.progress = 0; + attachment.id = db.attachment().insertAttachment(attachment); OutputStream os = null; - File file = EntityAttachment.getFile(context, ops.id); + File file = EntityAttachment.getFile(context, attachment.id); try { os = new BufferedOutputStream(new FileOutputStream(file)); @@ -439,10 +436,7 @@ public class Helper { size += write(os, "network=" + ni + " capabilities=" + caps + "\r\n\r\n"); } - ops.size = size; - ops.progress = null; - ops.available = true; - db.attachment().updateAttachment(ops); + db.attachment().setDownloaded(attachment.id, size); } finally { if (os != null) os.close(); @@ -473,10 +467,7 @@ public class Helper { for (EntityLog entry : db.log().getLogs(from)) size += write(os, String.format("%s %s\r\n", DF.format(entry.time), entry.data)); - log.size = size; - log.progress = null; - log.available = true; - db.attachment().updateAttachment(log); + db.attachment().setDownloaded(log.id, size); } finally { if (os != null) os.close(); @@ -486,17 +477,17 @@ public class Helper { private static void attachOperations(Context context, long id, int sequence) throws IOException { DB db = DB.getInstance(context); - EntityAttachment ops = new EntityAttachment(); - ops.message = id; - ops.sequence = sequence; - ops.name = "operations.txt"; - ops.type = "text/plain"; - ops.size = null; - ops.progress = 0; - ops.id = db.attachment().insertAttachment(ops); + EntityAttachment attachment = new EntityAttachment(); + attachment.message = id; + attachment.sequence = sequence; + attachment.name = "operations.txt"; + attachment.type = "text/plain"; + attachment.size = null; + attachment.progress = 0; + attachment.id = db.attachment().insertAttachment(attachment); OutputStream os = null; - File file = EntityAttachment.getFile(context, ops.id); + File file = EntityAttachment.getFile(context, attachment.id); try { os = new BufferedOutputStream(new FileOutputStream(file)); @@ -511,10 +502,7 @@ public class Helper { op.args, op.error)); - ops.size = size; - ops.progress = null; - ops.available = true; - db.attachment().updateAttachment(ops); + db.attachment().setDownloaded(attachment.id, size); } finally { if (os != null) os.close(); @@ -524,19 +512,19 @@ public class Helper { private static void attachLogcat(Context context, long id, int sequence) throws IOException { DB db = DB.getInstance(context); - EntityAttachment logcat = new EntityAttachment(); - logcat.message = id; - logcat.sequence = sequence; - logcat.name = "logcat.txt"; - logcat.type = "text/plain"; - logcat.size = null; - logcat.progress = 0; - logcat.id = db.attachment().insertAttachment(logcat); + EntityAttachment attachment = new EntityAttachment(); + attachment.message = id; + attachment.sequence = sequence; + attachment.name = "logcat.txt"; + attachment.type = "text/plain"; + attachment.size = null; + attachment.progress = 0; + attachment.id = db.attachment().insertAttachment(attachment); Process proc = null; BufferedReader br = null; OutputStream os = null; - File file = EntityAttachment.getFile(context, logcat.id); + File file = EntityAttachment.getFile(context, attachment.id); try { os = new BufferedOutputStream(new FileOutputStream(file)); @@ -554,10 +542,7 @@ public class Helper { while ((line = br.readLine()) != null) size += write(os, line + "\r\n"); - logcat.size = size; - logcat.progress = null; - logcat.available = true; - db.attachment().updateAttachment(logcat); + db.attachment().setDownloaded(attachment.id, size); } finally { if (os != null) os.close(); diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 70618b1f0a..0a48607fef 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -43,6 +43,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -288,4 +289,16 @@ public class HtmlHelper { return sb.toString(); } + + static List getCids(String html) { + List result = new ArrayList<>(); + + for (Element element : Jsoup.parse(html).select("img")) { + String src = element.attr("src"); + if (src.startsWith("cid:")) + result.add("<" + src.substring(4) + ">"); + } + + return result; + } } diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index f3b82ad8d7..3579247a78 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -643,11 +643,11 @@ public class ServiceSynchronize extends LifecycleService { if (message.content) try { - String html = message.read(this); + String body = message.read(this); StringBuilder sb = new StringBuilder(); if (!TextUtils.isEmpty(message.subject)) sb.append(message.subject).append("
"); - sb.append(HtmlHelper.getPreview(html)); + sb.append(HtmlHelper.getPreview(body)); mbuilder.setStyle(new Notification.BigTextStyle().bigText(Html.fromHtml(sb.toString()))); } catch (IOException ex) { Log.e(ex); @@ -1897,10 +1897,15 @@ public class ServiceSynchronize extends LifecycleService { throw new MessageRemovedException(); MessageHelper helper = new MessageHelper((MimeMessage) imessage); - String html = helper.getHtml(); - String preview = HtmlHelper.getPreview(html); - message.write(this, html); + String body = helper.getHtml(); + String preview = HtmlHelper.getPreview(body); + message.write(this, body); db.message().setMessageContent(message.id, true, preview); + + List cids = HtmlHelper.getCids(body); + for (EntityAttachment attachment : db.attachment().getAttachments(message.id)) + if (attachment.cid != null && !cids.contains(attachment.cid)) + db.attachment().clearCid(attachment.id); } private void doAttachment(EntityFolder folder, EntityOperation op, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws JSONException, MessagingException, IOException { @@ -2561,6 +2566,11 @@ public class ServiceSynchronize extends LifecycleService { db.message().setMessageContent( message.id, true, HtmlHelper.getPreview(body)); Log.i(folder.name + " downloaded message id=" + message.id + " size=" + message.size); + + List cids = HtmlHelper.getCids(body); + for (EntityAttachment attachment : attachments) + if (attachment.cid != null && !cids.contains(attachment.cid)) + db.attachment().clearCid(attachment.id); } List iattachments = null;