Skip to content

Commit 529e037

Browse files
rajveermalviyagnprice
authored andcommitted
share: Use filename to guess mimetype, in addition to the header bytes
Use the received file's name when trying to guess the mimetype in addition to checking for magic header bytes, using `lookupMimeType`.
1 parent 963f170 commit 529e037

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

android/app/src/main/kotlin/com/zulip/flutter/AndroidIntentEventListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fun getIntentSharedFile(context: Context, url: Uri): IntentSharedFile {
9494
cursor.moveToFirst()
9595
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
9696
cursor.getString(nameIndex)
97-
} ?: ("unknown." + (mimeType?.split('/')?.last() ?: "bin"))
97+
}
9898

9999
class ResolverFailedException(msg: String) : RuntimeException(msg)
100100

android/app/src/main/kotlin/com/zulip/flutter/AndroidIntents.g.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ private object AndroidIntentsPigeonUtils {
4848

4949
/** Generated class from Pigeon that represents data sent in messages. */
5050
data class IntentSharedFile (
51-
val name: String,
51+
val name: String? = null,
5252
val mimeType: String? = null,
5353
val bytes: ByteArray
5454
)
5555
{
5656
companion object {
5757
fun fromList(pigeonVar_list: List<Any?>): IntentSharedFile {
58-
val name = pigeonVar_list[0] as String
58+
val name = pigeonVar_list[0] as String?
5959
val mimeType = pigeonVar_list[1] as String?
6060
val bytes = pigeonVar_list[2] as ByteArray
6161
return IntentSharedFile(name, mimeType, bytes)

lib/host/android_intents.g.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ bool _deepEquals(Object? a, Object? b) {
2424

2525
class IntentSharedFile {
2626
IntentSharedFile({
27-
required this.name,
27+
this.name,
2828
this.mimeType,
2929
required this.bytes,
3030
});
3131

32-
String name;
32+
String? name;
3333

3434
String? mimeType;
3535

@@ -49,7 +49,7 @@ class IntentSharedFile {
4949
static IntentSharedFile decode(Object result) {
5050
result as List<Object?>;
5151
return IntentSharedFile(
52-
name: result[0]! as String,
52+
name: result[0] as String?,
5353
mimeType: result[1] as String?,
5454
bytes: result[2]! as Uint8List,
5555
);

lib/widgets/share.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,23 @@ class ShareService {
7979
// Try to guess the mimeType from file header magic-number.
8080
mimeType ??= lookupMimeType(
8181
// Seems like the path shouldn't be required; we still want to look for
82-
// matches on `headerBytes`. Thankfully we can still do that, by calling
83-
// lookupMimeType with the empty string as the path. That's a value that
84-
// doesn't map to any particular type, so the path will be effectively
85-
// ignored, as desired. Upstream comment:
82+
// matches on `headerBytes` when we don't have a path/filename.
83+
// Thankfully we can still do that, by calling lookupMimeType with the
84+
// empty string as the path. That's a value that doesn't map to any
85+
// particular type, so the path will be effectively ignored, as desired.
86+
// Upstream comment:
8687
// https://github.com/dart-lang/mime/issues/11#issuecomment-2246824452
87-
'',
88+
sharedFile.name ?? '',
8889
headerBytes: List.unmodifiable(
8990
sharedFile.bytes.take(defaultMagicNumbersMaxLength)));
9091

92+
final filename =
93+
sharedFile.name ?? 'unknown.${mimeType?.split('/').last ?? 'bin'}';
94+
9195
return FileToUpload(
9296
content: Stream.value(sharedFile.bytes),
9397
length: sharedFile.bytes.length,
94-
filename: sharedFile.name,
98+
filename: filename,
9599
mimeType: mimeType);
96100
});
97101

pigeon/android_intents.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class IntentSharedFile {
2020
required this.bytes,
2121
});
2222

23-
final String name;
23+
final String? name;
2424
final String? mimeType;
2525
final Uint8List bytes;
2626
}

0 commit comments

Comments
 (0)