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

[image_picker] set real extension instead always jpg #1269

Merged
merged 8 commits into from
Apr 30, 2019

Conversation

ivk1800
Copy link
Contributor

@ivk1800 ivk1800 commented Feb 23, 2019

No description provided.

@bparrishMines bparrishMines changed the title set real extension instead always jpg [image_picker] set real extension instead always jpg Feb 27, 2019
Copy link
Contributor

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

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

@mklim Could you take a look at the getImageExtension part just to make sure the code to get the extension makes sense?

try {
inputStream = context.getContentResolver().openInputStream(uri);
file = File.createTempFile("image_picker", "jpg", context.getCacheDir());
file = File.createTempFile("image_picker", extensionWithDot, context.getCacheDir());
Copy link
Contributor

Choose a reason for hiding this comment

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

what happens to the file path if extensionWithDot is ""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nothing, because suffix simply concatenating with prefix. suffix may be null, in which case the suffix .tmp will be used

Copy link
Contributor

Choose a reason for hiding this comment

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

Does File#createTempFile distinguish between null and ""? Technically we are sending it "" here in undefined cases, not null, and its documentation talks about defaulting to ".tmp" when this param is null specifically.

extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
}
} catch (Exception ignored) {

Copy link
Contributor

Choose a reason for hiding this comment

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

ignoring exception seems odd. Do we not have to resolve this if there is an exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if exception was thrown, final file name will be without extension.

Copy link
Contributor

Choose a reason for hiding this comment

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

Two things here:

  1. I think catching the top level Exception itself is too broad of a net to throw, it could mask other underlying bugs. Could we instead try to catch the specific types of Exceptions that we're expecting to be thrown from querying the ContentResolver and getting data out of the Cursor here?

  2. I think it would be safer to default to "jpg" in cases where we can't really find the file than defaulting to File#createTempFile's default ".tmp". "jpg" isn't necessarily correct in this case, but it matches the previous behavior of the plugin so it's unlikely to cause new regressions. But using ".tmp" could cause breakages in rare cases, by converting a ".jpg" instead a ".tmp" because of an I/O error with querying the mime type here. What do you think?

Copy link
Contributor

@mklim mklim left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! If you don't have time to keep working on this PR, please let us know and we'll be happy to shepherd it forward from here.

pubspec.yaml and CHANGELOG.md also need to be updated with a new patch version and description of this change.

extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
}
} catch (Exception ignored) {

Copy link
Contributor

Choose a reason for hiding this comment

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

Two things here:

  1. I think catching the top level Exception itself is too broad of a net to throw, it could mask other underlying bugs. Could we instead try to catch the specific types of Exceptions that we're expecting to be thrown from querying the ContentResolver and getting data out of the Cursor here?

  2. I think it would be safer to default to "jpg" in cases where we can't really find the file than defaulting to File#createTempFile's default ".tmp". "jpg" isn't necessarily correct in this case, but it matches the previous behavior of the plugin so it's unlikely to cause new regressions. But using ".tmp" could cause breakages in rare cases, by converting a ".jpg" instead a ".tmp" because of an I/O error with querying the mime type here. What do you think?

@@ -133,9 +134,11 @@ private static String getPathFromRemoteUri(final Context context, final Uri uri)
InputStream inputStream = null;
OutputStream outputStream = null;
boolean success = false;
String extension = getImageExtension(context, uri);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think it would be cleaner here if you handled all the logic about prepending the dot if the string was empty in getImageExtension itself, and then treated the result here as the final extension. That way we could keep all the image extension logic contained to the helper function for it.

try {
inputStream = context.getContentResolver().openInputStream(uri);
file = File.createTempFile("image_picker", "jpg", context.getCacheDir());
file = File.createTempFile("image_picker", extensionWithDot, context.getCacheDir());
Copy link
Contributor

Choose a reason for hiding this comment

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

Does File#createTempFile distinguish between null and ""? Technically we are sending it "" here in undefined cases, not null, and its documentation talks about defaulting to ".tmp" when this param is null specifically.

@cyanglaz
Copy link
Contributor

cyanglaz commented Apr 25, 2019

@pepegich any updates after the review from @mklim?

@ivk1800
Copy link
Contributor Author

ivk1800 commented Apr 25, 2019

@mklim please review new updates

Copy link
Contributor

@mklim mklim left a comment

Choose a reason for hiding this comment

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

Looking good, thanks for working on this! Just one thing left before this is ready to merge.

pubspec.yaml and CHANGELOG.md also need to be updated with a new patch version and description of this change.

Copy link
Contributor

@mklim mklim left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks again!

Co-Authored-By: pepegich <boytheprodigy@gmail.com>
@mklim mklim added the submit queue The Flutter team is in the process of landing this PR. label Apr 30, 2019
@mklim mklim merged commit 3830b02 into flutter:master Apr 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes submit queue The Flutter team is in the process of landing this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants