Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[firebase_storage] Fix putFile method's Content-Type auto-detection for iOS#1355

Merged
collinjackson merged 5 commits intoflutter:masterfrom
mono0926:cloud-storage-put-file
May 2, 2019
Merged

[firebase_storage] Fix putFile method's Content-Type auto-detection for iOS#1355
collinjackson merged 5 commits intoflutter:masterfrom
mono0926:cloud-storage-put-file

Conversation

@mono0926
Copy link
Contributor

@mono0926 mono0926 commented Mar 18, 2019

  • putFile should use putFile iOS SDK method correctly
  • putFile should auto-detect Content-Type automatically by file extension if not specified

I tested at this sample on iOS:
https://github.com/flutter/plugins/blob/master/packages/firebase_storage/example/lib/main.dart.

This uses putFile here:

final StorageUploadTask uploadTask = ref.putFile(

Before this pull request

Uploaded file:
https://firebasestorage.googleapis.com/v0/b/flutter-firebase-plugins.appspot.com/o/text%2Ffoob3d87700-3260-11e9-d838-d7a1f7b2a3d4.txt

contentType is application/octet-stream, but this is unintended value because putFile should auto-detect contentType as written this document.

The putFile: method automatically infers the content type from the NSURL filename extension, but you can override the auto-detected type by specifying contentType in the metadata. If you do not provide a contentType and Cloud Storage cannot infer a default from the file extension, Cloud Storage uses application/octet-stream.
https://firebase.google.com/docs/storage/ios/upload-files#add_file_metadata

In this case, the file's extension is txt, so contentType should be plain/text.

After this pull request

Uploaded file:
https://firebasestorage.googleapis.com/v0/b/flutter-firebase-plugins.appspot.com/o/text%2Ffoo09aa2e30-3260-11e9-efe1-e3ed438ea03f.txt

contentType is plain/text, which is intended value.

@mono0926
Copy link
Contributor Author

mono0926 commented Mar 18, 2019

Android's uploaded file URL:
https://firebasestorage.googleapis.com/v0/b/flutter-firebase-plugins.appspot.com/o/text%2Ffoo47306190-3260-11e9-a8ff-931a12d4e7ce.txt

contentType is blank(neither application/octet-stream nor plain/text), but I do not know why this happens.

putFile seems to use putFile correctly 🤔

private void putFile(MethodCall call, Result result) {
String filename = call.argument("filename");
String path = call.argument("path");
Map<String, Object> metadata = call.argument("metadata");
File file = new File(filename);
StorageReference ref = firebaseStorage.getReference().child(path);
UploadTask uploadTask;
if (metadata == null) {
uploadTask = ref.putFile(Uri.fromFile(file));
} else {
uploadTask = ref.putFile(Uri.fromFile(file), buildMetadataFromMap(metadata));
}
final int handle = addUploadListeners(uploadTask);
result.success(handle);
}

For your information, if these lines commented out, contentType changed to application/octet-stream 🤔

StorageMetadata(
contentLanguage: 'en',
customMetadata: <String, String>{'activity': 'test'},
),

https://firebasestorage.googleapis.com/v0/b/flutter-firebase-plugins.appspot.com/o/text%2Ffooe5417810-3260-11e9-c95c-995c37e340ac.txt

@mono0926 mono0926 force-pushed the cloud-storage-put-file branch 5 times, most recently from c4b48d7 to ca24c82 Compare March 18, 2019 11:01
@mono0926 mono0926 changed the title Fix firebase_storage putFile method's Content-Type auto-detection for iOS [firebase_storage] Fix putFile method's Content-Type auto-detection for iOS Mar 19, 2019
@mono0926 mono0926 changed the title [firebase_storage] Fix putFile method's Content-Type auto-detection for iOS [firebase_storage] Fix putFile method's Content-Type auto-detection for iOS Mar 19, 2019
@mono0926 mono0926 force-pushed the cloud-storage-put-file branch from ca24c82 to 5024d12 Compare March 31, 2019 07:35
@collinjackson collinjackson self-requested a review May 1, 2019 14:54
Copy link
Contributor

@collinjackson collinjackson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this PR.

Could you please write a CHANGELOG and update pubspec.yaml for release?

It looks like it might be possible to update the integration test to include the test for the correct behavior here. Would you be willing to add that?

https://github.com/flutter/plugins/blob/master/packages/firebase_storage/example/test_driver/firebase_storage.dart

- `putFile` should use `putFile` iOS SDK method correctly
- `putFile` uses file extension as Content-Type automatically if not specified
@mono0926 mono0926 force-pushed the cloud-storage-put-file branch from 5024d12 to 4ba8dc1 Compare May 2, 2019 01:48
mono0926 added 2 commits May 2, 2019 11:09
- flutter#1355 (review)
-  ‘content-type’ was ‘application/octet-stream’ before this pull request
@mono0926 mono0926 requested a review from kroikie as a code owner May 2, 2019 02:18
@mono0926
Copy link
Contributor Author

mono0926 commented May 2, 2019

@collinjackson

I've added commits:

Could you please write a CHANGELOG and update pubspec.yaml for release?

1a1563b

It looks like it might be possible to update the integration test to include the test for the correct behavior here. Would you be willing to add that?

778d6a6

Could you check that please?

@collinjackson
Copy link
Contributor

collinjackson commented May 2, 2019

Thanks for the contribution! Since this is a pre-1.0 plugin and this bug fix doesn't affect the public API of the plugin, I'm updating the version to make it a patch change.

@collinjackson collinjackson merged commit 616688a into flutter:master May 2, 2019
@mono0926 mono0926 deleted the cloud-storage-put-file branch May 2, 2019 23:16
collinjackson pushed a commit to collinjackson/flutterfire-old2 that referenced this pull request Jun 24, 2019
…or iOS (#1355)

* Use `putFile` iOS SDK method instead of `putData` for `putFile`

- `putFile` should use `putFile` iOS SDK method correctly
- `putFile` uses file extension as Content-Type automatically if not specified

* Verify ‘content-type’ is ‘text/plain’

- flutter/plugins#1355 (review)
-  ‘content-type’ was ‘application/octet-stream’ before this pull request
collinjackson pushed a commit to firebase/flutterfire that referenced this pull request Aug 14, 2019
…or iOS (#1355)

* Use `putFile` iOS SDK method instead of `putData` for `putFile`

- `putFile` should use `putFile` iOS SDK method correctly
- `putFile` uses file extension as Content-Type automatically if not specified

* Verify ‘content-type’ is ‘text/plain’

- flutter/plugins#1355 (review)
-  ‘content-type’ was ‘application/octet-stream’ before this pull request
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants