Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed and added support for dataUri in form data #33675

Closed
wants to merge 9 commits into from

Conversation

hetanthakkar
Copy link
Contributor

@hetanthakkar hetanthakkar commented Apr 20, 2022

Summary

Continuation of #33548
Fixes #25790. The issue resulted because the getFileInputStream() method of RequestBodyUtil failed to return an inputStream of the given dataUri This happened because the openInputStream() method of context.getContentResolver() expects a content or a file in its parameter but instead received dataUri which resulted in FileNotFoundException.

issue

Solution:

I've now converted the dataUri to bitmap and then attached an inputStream to the compressed bitmap. This way we won't have to store the image temporarily. I think converting the dataUri to Bitmap is necessary as there is no method that lets us convert the dataUri to inputStream directly. And regarding large size images, converting them to bitmap is the only(efficient) step I can think of, as the conversion is handled by in-built java function.

This issue has been unresolved for quite some time now, so resolving this PR would be greatly appreciated.

Changelog

[Android] [Added] - Support for dataUri in form data

Test Plan

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Apr 20, 2022
@react-native-bot react-native-bot added Platform: Android Android applications. Type: Enhancement A new feature or enhancement of an existing feature. labels Apr 20, 2022
@analysis-bot
Copy link

analysis-bot commented Apr 20, 2022

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 018d5cf
Branch: main

@analysis-bot
Copy link

analysis-bot commented Apr 20, 2022

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,779,163 +108
android hermes armeabi-v7a 7,184,232 +111
android hermes x86 8,088,060 +98
android hermes x86_64 8,068,775 +106
android jsc arm64-v8a 9,653,446 +50
android jsc armeabi-v7a 8,427,233 +60
android jsc x86 9,603,086 +59
android jsc x86_64 10,200,734 +57

Base commit: 018d5cf
Branch: main

@hetanthakkar
Copy link
Contributor Author

@cortinico Can you please review this PR

byte[] decodedDataUrString = Base64.decode(fileContentUriStr.split(",")[1], Base64.DEFAULT);
Bitmap bitMap = BitmapFactory.decodeByteArray(decodedDataUrString, 0, decodedDataUrString.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitMap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't work as it as you're compressing with JPG an image that could be potentially a PNG/GIF or other formats.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cortinico compressFormat also supports webp and PNG! We can add them too!?

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like the correct approach would be:

bitMap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bytes);

Copy link
Contributor Author

@hetanthakkar hetanthakkar May 4, 2022

Choose a reason for hiding this comment

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

@cortinico Thank you for the review!, Do you mean something like this?

if(format is PNG)
bitMap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bytes);
if(format is JPG)
bitMap.compress(CompressFormat.JPG, 0 /* ignored for JPG */, bytes);
if(format is WEBP)
bitMap.compress(CompressFormat.WEBP, 0 /* ignored for WEBP */, bytes);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like the correct approach would be:

bitMap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bytes);

Or should we do this just for PNG format, Sorry I didn’t completely get your approach

Copy link
Contributor

Choose a reason for hiding this comment

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

The snippet I suggested essentially ignores compression, so you don't need to check the format at all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cortinico I've made the changes. does the changes looks good?

@hetanthakkar
Copy link
Contributor Author

hetanthakkar commented May 5, 2022

A test is failing, I don’t think the failure is related to my changes! Is there anything else I need to do?

@facebook-github-bot
Copy link
Contributor

@cortinico has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@hetanthakkar
Copy link
Contributor Author

@cortinico Internal tests are failing. Do i need to do make any corrections!? Or it's nothing to worry about

@cortinico
Copy link
Contributor

Nope I'll take care of it next week 👍

@hetanthakkar
Copy link
Contributor Author

@cortinico Okay Thank you

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @hetanthakkar1 in c663c0e.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Platform: Android Android applications. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. Type: Enhancement A new feature or enhancement of an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android] FormData does not support data URIs
5 participants