From 45c01d39a449c714d8e0420b3f2ba0d69e940ad7 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 25 Jun 2019 19:53:04 +0200 Subject: [PATCH] Check message and attachment files on cleanup --- .../java/eu/faircode/email/DaoAttachment.java | 9 +++++++ .../java/eu/faircode/email/DaoMessage.java | 9 +++++++ .../java/eu/faircode/email/WorkerCleanup.java | 24 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/DaoAttachment.java b/app/src/main/java/eu/faircode/email/DaoAttachment.java index b945c0123a..fef0da59e2 100644 --- a/app/src/main/java/eu/faircode/email/DaoAttachment.java +++ b/app/src/main/java/eu/faircode/email/DaoAttachment.java @@ -63,6 +63,10 @@ public interface DaoAttachment { " LIMIT 1") EntityAttachment getAttachment(long message, String cid); + @Query("SELECT id FROM attachment" + + " WHERE available") + List getAttachmentAvailable(); + @Query("UPDATE attachment" + " SET message = :message" + " WHERE id = :id") @@ -78,6 +82,11 @@ public interface DaoAttachment { " WHERE id = :id") void setDownloaded(long id, Long size); + @Query("UPDATE attachment" + + " SET size = NULL, progress = NULL, available = 0" + + " WHERE id = :id") + void setUnavailable(long id); + @Query("UPDATE attachment" + " SET error = :error, progress = NULL, available = 0" + " WHERE id = :id") diff --git a/app/src/main/java/eu/faircode/email/DaoMessage.java b/app/src/main/java/eu/faircode/email/DaoMessage.java index 88bd9695bb..f77a188052 100644 --- a/app/src/main/java/eu/faircode/email/DaoMessage.java +++ b/app/src/main/java/eu/faircode/email/DaoMessage.java @@ -216,6 +216,12 @@ public interface DaoMessage { " ORDER BY message.received DESC") List getMessageAll(Long folder, boolean snoozed); + @Query("SELECT id" + + " FROM message" + + " WHERE content" + + " ORDER BY message.received DESC") + List getMessageWithContent(); + @Query("SELECT *" + " FROM message" + " WHERE account = :account" + @@ -380,6 +386,9 @@ public interface DaoMessage { @Query("UPDATE message SET revisions = :revisions WHERE id = :id") int setMessageRevisions(long id, Integer revisions); + @Query("UPDATE message SET content = :content WHERE id = :id") + int setMessageContent(long id, boolean content); + @Query("UPDATE message SET content = :content, plain_only = :plain_only, preview = :preview, warning = :warning WHERE id = :id") int setMessageContent(long id, boolean content, Boolean plain_only, String preview, String warning); diff --git a/app/src/main/java/eu/faircode/email/WorkerCleanup.java b/app/src/main/java/eu/faircode/email/WorkerCleanup.java index b4186b0aff..2465d8d4e1 100644 --- a/app/src/main/java/eu/faircode/email/WorkerCleanup.java +++ b/app/src/main/java/eu/faircode/email/WorkerCleanup.java @@ -135,6 +135,30 @@ public class WorkerCleanup extends Worker { Log.w("Error deleting " + file); } + // Check message files + Log.i("Checking message files"); + List mids = db.message().getMessageWithContent(); + for (Long mid : mids) { + EntityMessage message = db.message().getMessage(mid); + File file = message.getFile(context); + if (!file.exists()) { + Log.w("Message file missing id=" + mid); + db.message().setMessageContent(mid, false); + } + } + + // Check attachments files + Log.i("Checking attachments files"); + List aids = db.attachment().getAttachmentAvailable(); + for (Long aid : aids) { + EntityAttachment attachment = db.attachment().getAttachment(aid); + File file = attachment.getFile(context); + if (!file.exists()) { + Log.w("Attachment file missing id=" + aid); + db.attachment().setUnavailable(aid); + } + } + Log.i("Cleanup contacts"); int contacts = db.contact().deleteContacts(now - KEEP_CONTACTS_DURATION); Log.i("Deleted contacts=" + contacts);