mirror of
https://github.com/oxen-io/session-android.git
synced 2025-06-10 06:28:34 +00:00
Fix issue with mimeType resolution in share flow.
This commit is contained in:
parent
3824e90997
commit
0a883dc234
@ -322,7 +322,7 @@ public class BlobProvider {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BlobBuilder withFileName(@NonNull String fileName) {
|
public BlobBuilder withFileName(@Nullable String fileName) {
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.thoughtcrime.securesms.sharing;
|
package org.thoughtcrime.securesms.sharing;
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -37,7 +36,7 @@ class ShareRepository {
|
|||||||
/**
|
/**
|
||||||
* Handles a single URI that may be local or external.
|
* Handles a single URI that may be local or external.
|
||||||
*/
|
*/
|
||||||
void getResolved(@NonNull Uri uri, @Nullable String mimeType, @NonNull Callback<Optional<ShareData>> callback) {
|
void getResolved(@Nullable Uri uri, @Nullable String mimeType, @NonNull Callback<Optional<ShareData>> callback) {
|
||||||
SignalExecutors.BOUNDED.execute(() -> {
|
SignalExecutors.BOUNDED.execute(() -> {
|
||||||
try {
|
try {
|
||||||
callback.onResult(Optional.of(getResolvedInternal(uri, mimeType)));
|
callback.onResult(Optional.of(getResolvedInternal(uri, mimeType)));
|
||||||
@ -62,8 +61,6 @@ class ShareRepository {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
private @NonNull ShareData getResolvedInternal(@Nullable Uri uri, @Nullable String mimeType) throws IOException {
|
private @NonNull ShareData getResolvedInternal(@Nullable Uri uri, @Nullable String mimeType) throws IOException {
|
||||||
Context context = ApplicationDependencies.getApplication();
|
Context context = ApplicationDependencies.getApplication();
|
||||||
@ -72,11 +69,9 @@ class ShareRepository {
|
|||||||
return ShareData.forPrimitiveTypes();
|
return ShareData.forPrimitiveTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mimeType == null) {
|
mimeType = getMimeType(context, uri, mimeType);
|
||||||
mimeType = context.getContentResolver().getType(uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (PartAuthority.isLocalUri(uri) && mimeType != null) {
|
if (PartAuthority.isLocalUri(uri)) {
|
||||||
return ShareData.forIntentData(uri, mimeType, false);
|
return ShareData.forIntentData(uri, mimeType, false);
|
||||||
} else {
|
} else {
|
||||||
InputStream stream = context.getContentResolver().openInputStream(uri);
|
InputStream stream = context.getContentResolver().openInputStream(uri);
|
||||||
@ -85,38 +80,35 @@ class ShareRepository {
|
|||||||
throw new IOException("Failed to open stream!");
|
throw new IOException("Failed to open stream!");
|
||||||
}
|
}
|
||||||
|
|
||||||
long size = getSize(context, uri);
|
long size = getSize(context, uri);
|
||||||
String fileName = getFileName(context, uri);
|
String fileName = getFileName(context, uri);
|
||||||
String fillMimeType = Optional.fromNullable(mimeType).or(MediaUtil.UNKNOWN);
|
|
||||||
|
|
||||||
Uri blobUri;
|
Uri blobUri;
|
||||||
|
|
||||||
if (MediaUtil.isImageType(fillMimeType) || MediaUtil.isVideoType(fillMimeType)) {
|
if (MediaUtil.isImageType(mimeType) || MediaUtil.isVideoType(mimeType)) {
|
||||||
blobUri = BlobProvider.getInstance()
|
blobUri = BlobProvider.getInstance()
|
||||||
.forData(stream, size)
|
.forData(stream, size)
|
||||||
.withMimeType(fillMimeType)
|
.withMimeType(mimeType)
|
||||||
.withFileName(fileName)
|
.withFileName(fileName)
|
||||||
.createForSingleSessionOnDisk(context);
|
.createForSingleSessionOnDisk(context);
|
||||||
} else {
|
} else {
|
||||||
blobUri = BlobProvider.getInstance()
|
blobUri = BlobProvider.getInstance()
|
||||||
.forData(stream, size)
|
.forData(stream, size)
|
||||||
.withMimeType(fillMimeType)
|
.withMimeType(mimeType)
|
||||||
.withFileName(fileName)
|
.withFileName(fileName)
|
||||||
.createForMultipleSessionsOnDisk(context);
|
.createForMultipleSessionsOnDisk(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ShareData.forIntentData(blobUri, fillMimeType, true);
|
return ShareData.forIntentData(blobUri, mimeType, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
private @Nullable
|
private @Nullable ShareData getResolvedInternal(@NonNull List<Uri> uris) throws IOException {
|
||||||
ShareData getResolvedInternal(@NonNull List<Uri> uris) throws IOException {
|
Context context = ApplicationDependencies.getApplication();
|
||||||
Context context = ApplicationDependencies.getApplication();
|
|
||||||
ContentResolver resolver = context.getContentResolver();
|
|
||||||
|
|
||||||
Map<Uri, String> mimeTypes = Stream.of(uris)
|
Map<Uri, String> mimeTypes = Stream.of(uris)
|
||||||
.map(uri -> new Pair<>(uri, Optional.fromNullable(resolver.getType(uri)).or(MediaUtil.UNKNOWN)))
|
.map(uri -> new Pair<>(uri, getMimeType(context, uri, null)))
|
||||||
.filter(p -> MediaUtil.isImageType(p.second) || MediaUtil.isVideoType(p.second))
|
.filter(p -> MediaUtil.isImageType(p.second) || MediaUtil.isVideoType(p.second))
|
||||||
.collect(Collectors.toMap(p -> p.first, p -> p.second));
|
.collect(Collectors.toMap(p -> p.first, p -> p.second));
|
||||||
|
|
||||||
@ -173,6 +165,16 @@ class ShareRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static @NonNull String getMimeType(@NonNull Context context, @NonNull Uri uri, @Nullable String mimeType) {
|
||||||
|
String updatedMimeType = MediaUtil.getMimeType(context, uri);
|
||||||
|
|
||||||
|
if (updatedMimeType == null) {
|
||||||
|
updatedMimeType = MediaUtil.getCorrectedMimeType(mimeType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedMimeType != null ? updatedMimeType : MediaUtil.UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
private static long getSize(@NonNull Context context, @NonNull Uri uri) throws IOException {
|
private static long getSize(@NonNull Context context, @NonNull Uri uri) throws IOException {
|
||||||
long size = 0;
|
long size = 0;
|
||||||
|
|
||||||
@ -189,14 +191,14 @@ class ShareRepository {
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static @NonNull String getFileName(@NonNull Context context, @NonNull Uri uri) {
|
private static @Nullable String getFileName(@NonNull Context context, @NonNull Uri uri) {
|
||||||
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
|
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
|
||||||
if (cursor != null && cursor.moveToFirst() && cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) >= 0) {
|
if (cursor != null && cursor.moveToFirst() && cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) >= 0) {
|
||||||
return cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
|
return cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long getDuration(@NonNull Context context, @NonNull Uri uri) {
|
private static long getDuration(@NonNull Context context, @NonNull Uri uri) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user