mirror of
https://github.com/oxen-io/session-android.git
synced 2025-01-11 20:53:39 +00: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
@ -134,7 +134,9 @@ public class PersistentBlobProvider {
|
|||||||
|
|
||||||
public static @Nullable String getMimeType(@NonNull Context context, @NonNull Uri persistentBlobUri) {
|
public static @Nullable String getMimeType(@NonNull Context context, @NonNull Uri persistentBlobUri) {
|
||||||
if (!isAuthority(context, persistentBlobUri)) return null;
|
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) {
|
private static @NonNull String getExtensionFromMimeType(String mimeType) {
|
||||||
@ -142,6 +144,12 @@ public class PersistentBlobProvider {
|
|||||||
return extension != null ? extension : BLOB_EXTENSION;
|
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 {
|
private static @NonNull File getExternalDir(Context context) throws IOException {
|
||||||
final File externalDir = context.getExternalFilesDir(null);
|
final File externalDir = context.getExternalFilesDir(null);
|
||||||
if (externalDir == null) throw new IOException("no external files directory");
|
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) {
|
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 {
|
try {
|
||||||
return MATCHER.match(uri) == MATCH || uri.getPath().startsWith(getExternalDir(context).getAbsolutePath());
|
return uri.getPath().startsWith(getExternalDir(context).getAbsolutePath());
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user