Delete attachment metadata and thumbnails when deleting attachments

This commit is contained in:
cketti 2012-01-24 15:41:03 +01:00
parent c6696f632a
commit 4e5d116713
2 changed files with 45 additions and 11 deletions

View file

@ -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());
.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);
}

View file

@ -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 {