mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Also identify media type if there are unknown characters in the filename (#2329)
fix #2306
This commit is contained in:
@@ -27,6 +27,7 @@ import org.json.JSONObject;
|
||||
import org.thoughtcrime.securesms.connect.DcEventCenter;
|
||||
import org.thoughtcrime.securesms.connect.DcHelper;
|
||||
import org.thoughtcrime.securesms.util.JsonUtils;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
import org.thoughtcrime.securesms.util.Prefs;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
@@ -139,7 +140,7 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
|
||||
if (blob == null) {
|
||||
throw new Exception("\"" + path + "\" not found");
|
||||
}
|
||||
String ext = MimeTypeMap.getFileExtensionFromUrl(path);
|
||||
String ext = MediaUtil.getFileExtensionFromUrl(path);
|
||||
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
|
||||
if (mimeType == null) {
|
||||
switch (ext) {
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
||||
import org.thoughtcrime.securesms.notifications.NotificationCenter;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
@@ -286,7 +287,7 @@ public class DcHelper {
|
||||
private static String checkMime(String path, String mimeType) {
|
||||
if(mimeType == null || mimeType.equals("application/octet-stream")) {
|
||||
path = path.replaceAll(" ", "");
|
||||
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
|
||||
String extension = MediaUtil.getFileExtensionFromUrl(path);
|
||||
String newType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
||||
if(newType != null) return newType;
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ public class PersistentBlobProvider {
|
||||
|
||||
private static @NonNull String getMimeTypeFromExtension(@NonNull Uri uri) {
|
||||
final String mimeType = MimeTypeMap.getSingleton()
|
||||
.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
|
||||
.getMimeTypeFromExtension(MediaUtil.getFileExtensionFromUrl(uri.toString()));
|
||||
return mimeType != null ? mimeType : "application/octet-stream";
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ public class MediaUtil {
|
||||
|
||||
String type = context.getContentResolver().getType(uri);
|
||||
if (type == null) {
|
||||
final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
|
||||
final String extension = getFileExtensionFromUrl(uri.toString());
|
||||
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
|
||||
if (type == null) {
|
||||
type = "application/octet-stream";
|
||||
@@ -102,6 +102,41 @@ public class MediaUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a version of android.webkit.MimeTypeMap.getFileExtensionFromUrl() that
|
||||
* doesn't refuse to do its job when there are characters in the URL it doesn't know.
|
||||
* Using MimeTypeMap.getFileExtensionFromUrl() led to bugs like this one:
|
||||
* https://github.com/deltachat/deltachat-android/issues/2306
|
||||
*
|
||||
* @return The url's file extension, or "" if there is none.
|
||||
*/
|
||||
public static String getFileExtensionFromUrl(String url) {
|
||||
if (TextUtils.isEmpty(url)) return "";
|
||||
|
||||
int fragment = url.lastIndexOf('#');
|
||||
if (fragment > 0) {
|
||||
url = url.substring(0, fragment);
|
||||
}
|
||||
|
||||
int query = url.lastIndexOf('?');
|
||||
if (query > 0) {
|
||||
url = url.substring(0, query);
|
||||
}
|
||||
|
||||
int filenamePos = url.lastIndexOf('/');
|
||||
String filename =
|
||||
0 <= filenamePos ? url.substring(filenamePos + 1) : url;
|
||||
|
||||
if (!filename.isEmpty()) {
|
||||
int dotPos = filename.lastIndexOf('.');
|
||||
if (0 <= dotPos) {
|
||||
return filename.substring(dotPos + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static long getMediaSize(Context context, Uri uri) throws IOException {
|
||||
InputStream in = PartAuthority.getAttachmentStream(context, uri);
|
||||
if (in == null) throw new IOException("Couldn't obtain input stream.");
|
||||
|
||||
Reference in New Issue
Block a user