"image/jpg"->"image/jpeg" on MIME type checks and saving

Fixes #4602
Closes #4643
This commit is contained in:
Geonu Kang 2015-11-21 17:04:19 +09:00 committed by Moxie Marlinspike
parent 6431773288
commit e83827ab75
3 changed files with 24 additions and 15 deletions

View File

@ -29,7 +29,6 @@ import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.mms.PartAuthority; import org.thoughtcrime.securesms.mms.PartAuthority;
@ -37,6 +36,7 @@ import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
import org.thoughtcrime.securesms.recipients.Recipients; import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicTheme; import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.ViewUtil; import org.thoughtcrime.securesms.util.ViewUtil;
import java.io.IOException; import java.io.IOException;
@ -167,7 +167,8 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity
final Intent intent = new Intent(this, target); final Intent intent = new Intent(this, target);
final String textExtra = getIntent().getStringExtra(Intent.EXTRA_TEXT); final String textExtra = getIntent().getStringExtra(Intent.EXTRA_TEXT);
final Uri streamExtra = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); final Uri streamExtra = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
final String type = streamExtra != null ? getMimeType(streamExtra) : getIntent().getType(); final String type = streamExtra != null ? getMimeType(streamExtra)
: MediaUtil.getCorrectedMimeType(getIntent().getType());
intent.putExtra(ConversationActivity.TEXT_EXTRA, textExtra); intent.putExtra(ConversationActivity.TEXT_EXTRA, textExtra);
if (resolvedExtra != null) intent.setDataAndType(resolvedExtra, type); if (resolvedExtra != null) intent.setDataAndType(resolvedExtra, type);
@ -175,14 +176,9 @@ public class ShareActivity extends PassphraseRequiredActionBarActivity
} }
private String getMimeType(Uri uri) { private String getMimeType(Uri uri) {
String type = getContentResolver().getType(uri); final String type = MediaUtil.getMimeType(getApplicationContext(), uri);
return type == null ? MediaUtil.getCorrectedMimeType(getIntent().getType())
if (type == null) { : type;
String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type == null ? getIntent().getType() : type;
} }
private class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> { private class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {

View File

@ -76,7 +76,20 @@ public class MediaUtil {
final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString()); final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
} }
return type; return getCorrectedMimeType(type);
}
public static @Nullable String getCorrectedMimeType(@Nullable String mimeType) {
if (mimeType == null) return null;
switch(mimeType) {
case "image/jpg":
return MimeTypeMap.getSingleton().hasMimeType(ContentType.IMAGE_JPEG)
? ContentType.IMAGE_JPEG
: mimeType;
default:
return mimeType;
}
} }
public static long getMediaSize(Context context, MasterSecret masterSecret, Uri uri) throws IOException { public static long getMediaSize(Context context, MasterSecret masterSecret, Uri uri) throws IOException {

View File

@ -58,7 +58,8 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
return FAILURE; return FAILURE;
} }
File mediaFile = constructOutputFile(attachment.contentType, attachment.date); String contentType = MediaUtil.getCorrectedMimeType(attachment.contentType);
File mediaFile = constructOutputFile(contentType, attachment.date);
InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, attachment.uri); InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, attachment.uri);
if (inputStream == null) { if (inputStream == null) {
@ -69,7 +70,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
Util.copy(inputStream, outputStream); Util.copy(inputStream, outputStream);
MediaScannerConnection.scanFile(context, new String[]{mediaFile.getAbsolutePath()}, MediaScannerConnection.scanFile(context, new String[]{mediaFile.getAbsolutePath()},
new String[]{attachment.contentType}, null); new String[]{contentType}, null);
return SUCCESS; return SUCCESS;
} catch (IOException ioe) { } catch (IOException ioe) {
@ -121,8 +122,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask<SaveAttachmentTa
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss"); SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
String base = "signal-" + dateFormatter.format(timestamp); String base = "signal-" + dateFormatter.format(timestamp);
if (extension == null) if (extension == null) extension = "attach";
extension = "attach";
int i = 0; int i = 0;
File file = new File(outputDirectory, base + "." + extension); File file = new File(outputDirectory, base + "." + extension);