Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4e5d1167 authored by cketti's avatar cketti
Browse files

Delete attachment metadata and thumbnails when deleting attachments

parent c6696f63
Loading
Loading
Loading
Loading
+20 −8
Original line number Diff line number Diff line
@@ -2692,22 +2692,34 @@ public class LocalStore extends Store implements Serializable {
                public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                    Cursor attachmentsCursor = null;
                    try {
                        attachmentsCursor = db.query("attachments", new String[]
                                                     { "id" }, "message_id = ?", new String[]
                                                     { Long.toString(messageId) }, null, null, null);
                        String accountUuid = mAccount.getUuid();
                        Context context = mApplication;

                        // Get attachment IDs
                        String[] whereArgs = new String[] { Long.toString(messageId) };
                        attachmentsCursor = db.query("attachments", new String[] { "id" },
                                "message_id = ?", whereArgs, null, null, null);

                        final File attachmentDirectory = StorageManager.getInstance(mApplication)
                                .getAttachmentDirectory(uUid, database.getStorageProviderId());

                        while (attachmentsCursor.moveToNext()) {
                            long attachmentId = attachmentsCursor.getLong(0);
                            String attachmentId = Long.toString(attachmentsCursor.getLong(0));
                            try {
                                File file = new File(attachmentDirectory, Long.toString(attachmentId));
                                // Delete stored attachment
                                File file = new File(attachmentDirectory, attachmentId);
                                if (file.exists()) {
                                    file.delete();
                                }
                            } catch (Exception e) {

                                // Delete thumbnail file
                                AttachmentProvider.deleteThumbnail(context, accountUuid,
                                        attachmentId);
                            } catch (Exception e) { /* ignore */ }
                        }
                        }

                        // Delete attachment metadata from the database
                        db.delete("attachments", "message_id = ?", whereArgs);
                    } finally {
                        Utility.closeQuietly(attachmentsCursor);
                    }
+25 −3
Original line number Diff line number Diff line
@@ -92,6 +92,30 @@ public class AttachmentProvider extends ContentProvider {
        }
    }

    /**
     * Delete the thumbnail of an attachment.
     *
     * @param context
     *         The application context.
     * @param accountUuid
     *         The UUID of the account the attachment belongs to.
     * @param attachmentId
     *         The ID of the attachment the thumbnail was created for.
     */
    public static void deleteThumbnail(Context context, String accountUuid, String attachmentId) {
        File file = getThumbnailFile(context, accountUuid, attachmentId);
        if (file.exists()) {
            file.delete();
        }
    }

    private static File getThumbnailFile(Context context, String accountUuid,
            String attachmentId) {
        String filename = "thmb_" + accountUuid + "_" + attachmentId + ".tmp";
        File dir = context.getCacheDir();
        return new File(dir, filename);
    }


    @Override
    public boolean onCreate() {
@@ -139,9 +163,7 @@ public class AttachmentProvider extends ContentProvider {
            int width = Integer.parseInt(segments.get(3));
            int height = Integer.parseInt(segments.get(4));

            String filename = "thmb_" + accountUuid + "_" + attachmentId + ".tmp";
            File dir = getContext().getCacheDir();
            file = new File(dir, filename);
            file = getThumbnailFile(getContext(), accountUuid, attachmentId);
            if (!file.exists()) {
                String type = getType(accountUuid, attachmentId, FORMAT_VIEW);
                try {