Skip to content

Commit

Permalink
"image/jpg"->"image/jpeg" on MIME type checks and saving
Browse files Browse the repository at this point in the history
Fixes #4602
Closes #4643
  • Loading branch information
unrulygnu authored and moxie0 committed Nov 27, 2015
1 parent 6431773 commit e83827a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
16 changes: 6 additions & 10 deletions src/org/thoughtcrime/securesms/ShareActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;

import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.mms.PartAuthority;
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.ViewUtil;

import java.io.IOException;
Expand Down Expand Up @@ -167,22 +167,18 @@ private Intent getBaseShareIntent(final @NonNull Class<?> target) {
final Intent intent = new Intent(this, target);
final String textExtra = getIntent().getStringExtra(Intent.EXTRA_TEXT);
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);
if (resolvedExtra != null) intent.setDataAndType(resolvedExtra, type);

return intent;
}

private String getMimeType(Uri uri) {
String type = getContentResolver().getType(uri);

if (type == null) {
String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}

return type == null ? getIntent().getType() : type;
final String type = MediaUtil.getMimeType(getApplicationContext(), uri);
return type == null ? MediaUtil.getCorrectedMimeType(getIntent().getType())
: type;
}

private class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {
Expand Down
15 changes: 14 additions & 1 deletion src/org/thoughtcrime/securesms/util/MediaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,20 @@ public static Slide getSlideForAttachment(Context context, Attachment attachment
final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
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 {
Expand Down
8 changes: 4 additions & 4 deletions src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ protected Integer doInBackground(SaveAttachmentTask.Attachment... attachments) {
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);

if (inputStream == null) {
Expand All @@ -69,7 +70,7 @@ protected Integer doInBackground(SaveAttachmentTask.Attachment... attachments) {
Util.copy(inputStream, outputStream);

MediaScannerConnection.scanFile(context, new String[]{mediaFile.getAbsolutePath()},
new String[]{attachment.contentType}, null);
new String[]{contentType}, null);

return SUCCESS;
} catch (IOException ioe) {
Expand Down Expand Up @@ -121,8 +122,7 @@ private File constructOutputFile(String contentType, long timestamp) throws IOEx
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
String base = "signal-" + dateFormatter.format(timestamp);

if (extension == null)
extension = "attach";
if (extension == null) extension = "attach";

int i = 0;
File file = new File(outputDirectory, base + "." + extension);
Expand Down

1 comment on commit e83827a

@WhisperBTC
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! BitHub has sent payment of $34.02USD for this commit.

Please sign in to comment.