|
6 | 6 |
|
7 | 7 | import android.net.Uri; |
8 | 8 | import android.util.SparseArray; |
| 9 | +import android.webkit.MimeTypeMap; |
9 | 10 | import androidx.annotation.NonNull; |
10 | 11 | import com.google.android.gms.tasks.OnCompleteListener; |
11 | 12 | import com.google.android.gms.tasks.OnFailureListener; |
@@ -49,7 +50,7 @@ private FirebaseStoragePlugin(MethodChannel channel, Registrar registrar) { |
49 | 50 | } |
50 | 51 |
|
51 | 52 | @Override |
52 | | - public void onMethodCall(MethodCall call, final Result result) { |
| 53 | + public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result) { |
53 | 54 | String app = call.argument("app"); |
54 | 55 | String storageBucket = call.argument("bucket"); |
55 | 56 | if (app == null && storageBucket == null) { |
@@ -258,15 +259,13 @@ public void onFailure(@NonNull Exception e) { |
258 | 259 | private void putFile(MethodCall call, Result result) { |
259 | 260 | String filename = call.argument("filename"); |
260 | 261 | String path = call.argument("path"); |
261 | | - Map<String, Object> metadata = call.argument("metadata"); |
262 | 262 | 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 | + |
263 | 267 | 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)); |
270 | 269 | final int handle = addUploadListeners(uploadTask); |
271 | 270 | result.success(handle); |
272 | 271 | } |
@@ -394,7 +393,7 @@ private void cancelUploadTask(MethodCall call, final Result result) { |
394 | 393 | } |
395 | 394 | } |
396 | 395 |
|
397 | | - private void resumeUploadTask(MethodCall call, final Result result) { |
| 396 | + private void resumeUploadTask(MethodCall call, @NonNull final Result result) { |
398 | 397 | int handle = call.argument("handle"); |
399 | 398 | UploadTask task = uploadTasks.get(handle); |
400 | 399 | if (task != null) { |
@@ -487,4 +486,25 @@ private Map<String, Object> buildMapFromTaskSnapshot( |
487 | 486 | } |
488 | 487 | return map; |
489 | 488 | } |
| 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 | + } |
490 | 510 | } |
0 commit comments