Skip to content

Commit d18ed61

Browse files
long1eucollinjackson
authored andcommitted
[firebase_storage] Fix Content-Type auto-detection for Android (flutter#1834)
1 parent 4992803 commit d18ed61

5 files changed

Lines changed: 43 additions & 12 deletions

File tree

packages/firebase_storage/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.0.3
2+
3+
* Fix inconsistency of `getPath`, on Android the path returned started with a `/` but on iOS it did not
4+
* Fix content-type auto-detection on Android
5+
16
## 3.0.2
27

38
* Automatically use version from pubspec.yaml when reporting usage to Firebase.

packages/firebase_storage/android/src/main/java/io/flutter/plugins/firebase/storage/FirebaseStoragePlugin.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import android.net.Uri;
88
import android.util.SparseArray;
9+
import android.webkit.MimeTypeMap;
910
import androidx.annotation.NonNull;
1011
import com.google.android.gms.tasks.OnCompleteListener;
1112
import com.google.android.gms.tasks.OnFailureListener;
@@ -49,7 +50,7 @@ private FirebaseStoragePlugin(MethodChannel channel, Registrar registrar) {
4950
}
5051

5152
@Override
52-
public void onMethodCall(MethodCall call, final Result result) {
53+
public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result) {
5354
String app = call.argument("app");
5455
String storageBucket = call.argument("bucket");
5556
if (app == null && storageBucket == null) {
@@ -258,15 +259,13 @@ public void onFailure(@NonNull Exception e) {
258259
private void putFile(MethodCall call, Result result) {
259260
String filename = call.argument("filename");
260261
String path = call.argument("path");
261-
Map<String, Object> metadata = call.argument("metadata");
262262
File file = new File(filename);
263+
final Uri fileUri = Uri.fromFile(file);
264+
Map<String, Object> metadata = call.argument("metadata");
265+
metadata = ensureMimeType(metadata, fileUri);
266+
263267
StorageReference ref = firebaseStorage.getReference().child(path);
264-
UploadTask uploadTask;
265-
if (metadata == null) {
266-
uploadTask = ref.putFile(Uri.fromFile(file));
267-
} else {
268-
uploadTask = ref.putFile(Uri.fromFile(file), buildMetadataFromMap(metadata));
269-
}
268+
final UploadTask uploadTask = ref.putFile(fileUri, buildMetadataFromMap(metadata));
270269
final int handle = addUploadListeners(uploadTask);
271270
result.success(handle);
272271
}
@@ -394,7 +393,7 @@ private void cancelUploadTask(MethodCall call, final Result result) {
394393
}
395394
}
396395

397-
private void resumeUploadTask(MethodCall call, final Result result) {
396+
private void resumeUploadTask(MethodCall call, @NonNull final Result result) {
398397
int handle = call.argument("handle");
399398
UploadTask task = uploadTasks.get(handle);
400399
if (task != null) {
@@ -487,4 +486,25 @@ private Map<String, Object> buildMapFromTaskSnapshot(
487486
}
488487
return map;
489488
}
489+
490+
private Map<String, Object> ensureMimeType(Map<String, Object> metadata, Uri file) {
491+
if (metadata == null) {
492+
metadata = new HashMap<>();
493+
}
494+
495+
if (metadata.get("contentType") == null) {
496+
metadata.put("contentType", getMimeType(file));
497+
}
498+
499+
return metadata;
500+
}
501+
502+
private static String getMimeType(Uri file) {
503+
String type = null;
504+
String extension = MimeTypeMap.getFileExtensionFromUrl(file.toString());
505+
if (extension != null) {
506+
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
507+
}
508+
return type;
509+
}
490510
}

packages/firebase_storage/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies:
88
firebase_storage:
99
path: ../
1010
firebase_core: ^0.4.0
11-
uuid: "^1.0.0"
11+
uuid: ^1.0.0
1212
http: ^0.12.0
1313

1414
dev_dependencies:

packages/firebase_storage/lib/src/storage_reference.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,18 @@ class StorageReference {
8888
/// Returns the full path to this object, not including the Google Cloud
8989
/// Storage bucket.
9090
Future<String> getPath() async {
91-
return await FirebaseStorage.channel
91+
final String path = await FirebaseStorage.channel
9292
.invokeMethod<String>("StorageReference#getPath", <String, String>{
9393
'app': _firebaseStorage.app?.name,
9494
'bucket': _firebaseStorage.storageBucket,
9595
'path': _pathComponents.join("/"),
9696
});
97+
98+
if (path.startsWith('/')) {
99+
return path.substring(1);
100+
} else {
101+
return path;
102+
}
97103
}
98104

99105
/// Returns the short name of this object.

packages/firebase_storage/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Firebase Cloud Storage, a powerful, simple, and
33
cost-effective object storage service for Android and iOS.
44
author: Flutter Team <[email protected]>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_storage
6-
version: 3.0.2
6+
version: 3.0.3
77

88
flutter:
99
plugin:

0 commit comments

Comments
 (0)