diff --git a/src/com/fsck/k9/cache/TemporaryAttachmentStore.java b/src/com/fsck/k9/cache/TemporaryAttachmentStore.java new file mode 100644 index 000000000..c2a4a264a --- /dev/null +++ b/src/com/fsck/k9/cache/TemporaryAttachmentStore.java @@ -0,0 +1,59 @@ +package com.fsck.k9.cache; + + +import java.io.File; +import java.io.IOException; + +import android.content.Context; +import android.util.Log; + +import com.fsck.k9.K9; +import com.fsck.k9.helper.FileHelper; + + +public class TemporaryAttachmentStore { + private static String TEMPORARY_ATTACHMENT_DIRECTORY = "attachments"; + private static long MAX_FILE_AGE = 12 * 60 * 60 * 1000; // 12h + + public static File getFile(Context context, String attachmentName) throws IOException { + File directory = createOrCleanAttachmentDirectory(context); + String filename = FileHelper.sanitizeFilename(attachmentName); + return new File(directory, filename); + } + + private static File createOrCleanAttachmentDirectory(Context context) throws IOException { + File directory = getTemporaryAttachmentDirectory(context); + if (directory.exists()) { + cleanOldFiles(directory); + } else { + if (!directory.mkdir()) { + throw new IOException("Couldn't create temporary attachment store: " + directory.getAbsolutePath()); + } + } + return directory; + } + + private static File getTemporaryAttachmentDirectory(Context context) { + return new File(context.getExternalCacheDir(), TEMPORARY_ATTACHMENT_DIRECTORY); + } + + private static void cleanOldFiles(File directory) { + File[] files = directory.listFiles(); + if (files == null) { + return; + } + + long cutOffTime = System.currentTimeMillis() - MAX_FILE_AGE; + for (File file : files) { + if (file.lastModified() < cutOffTime) { + if (file.delete()) { + if (K9.DEBUG) { + Log.d(K9.LOG_TAG, "Deleted from temporary attachment store: " + file.getName()); + } + } else { + Log.w(K9.LOG_TAG, "Couldn't delete from temporary attachment store: " + file.getName()); + } + } + } + } +} diff --git a/src/com/fsck/k9/view/AttachmentView.java b/src/com/fsck/k9/view/AttachmentView.java index 03a199b20..5367e66cd 100644 --- a/src/com/fsck/k9/view/AttachmentView.java +++ b/src/com/fsck/k9/view/AttachmentView.java @@ -32,6 +32,7 @@ import android.widget.Toast; import com.fsck.k9.Account; import com.fsck.k9.K9; import com.fsck.k9.R; +import com.fsck.k9.cache.TemporaryAttachmentStore; import com.fsck.k9.controller.MessagingController; import com.fsck.k9.controller.MessagingListener; import com.fsck.k9.helper.FileHelper; @@ -223,7 +224,7 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo */ public void writeFile(File directory) { try { - File file = writeAttachmentToStorage(directory); + File file = saveAttachmentWithUniqueFileName(directory); displayAttachmentSavedMessage(file.toString()); @@ -236,10 +237,16 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo } } - private File writeAttachmentToStorage(File directory) throws IOException { + private File saveAttachmentWithUniqueFileName(File directory) throws IOException { String filename = FileHelper.sanitizeFilename(name); File file = FileHelper.createUniqueFile(directory, filename); + writeAttachmentToStorage(file); + + return file; + } + + private void writeAttachmentToStorage(File file) throws IOException { Uri uri = AttachmentProvider.getAttachmentUri(account, part.getAttachmentId()); InputStream in = context.getContentResolver().openInputStream(uri); try { @@ -253,8 +260,6 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo } finally { in.close(); } - - return file; } public void showFile() { @@ -270,8 +275,9 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo IntentAndResolvedActivitiesCount resultForFileUri = getBestViewIntentForFileUri(); if (resultForFileUri.getActivitiesCount() > 0) { try { - File file = writeAttachmentToStorage(new File(K9.getAttachmentDefaultPath())); - return createViewIntentForFileUri(resultForFileUri.getMimeType(), Uri.fromFile(file)); + File tempFile = TemporaryAttachmentStore.getFile(context, name); + writeAttachmentToStorage(tempFile); + return createViewIntentForFileUri(resultForFileUri.getMimeType(), Uri.fromFile(tempFile)); } catch (IOException e) { if (K9.DEBUG) { Log.e(K9.LOG_TAG, "Error while saving attachment to use file:// URI with ACTION_VIEW Intent", e);