From 80d99baf29be8966a008e1ea863d46ab722ac441 Mon Sep 17 00:00:00 2001 From: Apoorv Khatreja Date: Fri, 25 Mar 2011 04:52:25 +0800 Subject: [PATCH] Implemented table lookup for mime type replacement (issue 873 and similar), added javadocs, moved to MimeUtility.java. --- .../fsck/k9/mail/internet/MimeUtility.java | 28 +++++++++++++++---- src/com/fsck/k9/mail/store/LocalStore.java | 2 +- .../fsck/k9/provider/AttachmentProvider.java | 7 ++--- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/com/fsck/k9/mail/internet/MimeUtility.java b/src/com/fsck/k9/mail/internet/MimeUtility.java index be318d998..42033eda1 100644 --- a/src/com/fsck/k9/mail/internet/MimeUtility.java +++ b/src/com/fsck/k9/mail/internet/MimeUtility.java @@ -875,8 +875,10 @@ public class MimeUtility { { "zirz", "application/vnd.zul"}, { "zmm", "application/vnd.handheld-entertainment+xml"} }; - - + + private static final String[][] MIME_TYPE_REPLACEMENT_MAP = new String[][] { + {"image/jpg","image/jpeg"} + }; public static String unfold(String s) { if (s == null) { @@ -1181,17 +1183,33 @@ public class MimeUtility { // If the MIME type set by the user's mailer is application/octet-stream, try to figure // out whether there's a sane file type extension. if (returnedType != null && !DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(returnedType)) { - return returnedType; + return getCorrectedMimeType(returnedType); } else if (extension != null) { for (String[] contentTypeMapEntry : MIME_TYPE_BY_EXTENSION_MAP) { if (contentTypeMapEntry[0].equals(extension)) { - return contentTypeMapEntry[1]; + return getCorrectedMimeType(contentTypeMapEntry[1]); } } } - return DEFAULT_ATTACHMENT_MIME_TYPE; + return getCorrectedMimeType(DEFAULT_ATTACHMENT_MIME_TYPE); } + + /** + * Table lookup for MIME type replacement, enabling easy fixes for issues similar to issue 873. + * To map any MIME type to a different one, add a {originalMimeType, replacementMimeType} string pair to MIME_TYPE_REPLACEMENT_MAP. + * + * @param mimeType the MIME type + * @return the corrected MIME type + */ + public static String getCorrectedMimeType(String mimeType) { + for (String[] mimeTypeMapEntry : MIME_TYPE_REPLACEMENT_MAP) { + if (mimeTypeMapEntry[0].equals(mimeType)) { + return mimeTypeMapEntry[1]; + } + } + return mimeType; + } private static Message getMessageFromPart(Part part) { while (part != null) { diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index e90e5a64e..7141d18c4 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -1010,7 +1010,7 @@ public class LocalStore extends Store implements Serializable { if (MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(type)) { type = MimeUtility.getMimeTypeByExtension(name); } - return type; + return MimeUtility.getCorrectedMimeType(type); } finally { if (cursor != null) { cursor.close(); diff --git a/src/com/fsck/k9/provider/AttachmentProvider.java b/src/com/fsck/k9/provider/AttachmentProvider.java index 9cb492f9b..90ac52c89 100644 --- a/src/com/fsck/k9/provider/AttachmentProvider.java +++ b/src/com/fsck/k9/provider/AttachmentProvider.java @@ -105,11 +105,8 @@ public class AttachmentProvider extends ContentProvider { try { final LocalStore localStore = LocalStore.getLocalInstance(account, K9.app); - if (localStore.getAttachmentType(id).equals("image/jpg")) { - return "image/jpeg"; - } else { - return localStore.getAttachmentType(id); - } + return localStore.getAttachmentType(id); + } catch (MessagingException e) { Log.e(K9.LOG_TAG, "Unable to retrieve LocalStore for " + account, e); return null;