mirror of
https://github.com/oxen-io/session-android.git
synced 2023-12-14 02:53:01 +01:00
Fix bug on getting MIME type for external Persistent Blob URI's
Fixes #5295 Closes #5300 // FREEBIE
This commit is contained in:
parent
ba960a82e2
commit
a7cbe5e2a2
1 changed files with 14 additions and 3 deletions
|
@ -134,7 +134,9 @@ public class PersistentBlobProvider {
|
|||
|
||||
public static @Nullable String getMimeType(@NonNull Context context, @NonNull Uri persistentBlobUri) {
|
||||
if (!isAuthority(context, persistentBlobUri)) return null;
|
||||
return persistentBlobUri.getPathSegments().get(MIMETYPE_PATH_SEGMENT);
|
||||
return isExternalBlobUri(context, persistentBlobUri)
|
||||
? getMimeTypeFromExtension(persistentBlobUri)
|
||||
: persistentBlobUri.getPathSegments().get(MIMETYPE_PATH_SEGMENT);
|
||||
}
|
||||
|
||||
private static @NonNull String getExtensionFromMimeType(String mimeType) {
|
||||
|
@ -142,6 +144,12 @@ public class PersistentBlobProvider {
|
|||
return extension != null ? extension : BLOB_EXTENSION;
|
||||
}
|
||||
|
||||
private static @NonNull String getMimeTypeFromExtension(@NonNull Uri uri) {
|
||||
final String mimeType = MimeTypeMap.getSingleton()
|
||||
.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
|
||||
return mimeType != null ? mimeType : "application/octet-stream";
|
||||
}
|
||||
|
||||
private static @NonNull File getExternalDir(Context context) throws IOException {
|
||||
final File externalDir = context.getExternalFilesDir(null);
|
||||
if (externalDir == null) throw new IOException("no external files directory");
|
||||
|
@ -149,11 +157,14 @@ public class PersistentBlobProvider {
|
|||
}
|
||||
|
||||
public static boolean isAuthority(@NonNull Context context, @NonNull Uri uri) {
|
||||
return MATCHER.match(uri) == MATCH || isExternalBlobUri(context, uri);
|
||||
}
|
||||
|
||||
private static boolean isExternalBlobUri(@NonNull Context context, @NonNull Uri uri) {
|
||||
try {
|
||||
return MATCHER.match(uri) == MATCH || uri.getPath().startsWith(getExternalDir(context).getAbsolutePath());
|
||||
return uri.getPath().startsWith(getExternalDir(context).getAbsolutePath());
|
||||
} catch (IOException ioe) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue