-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[image_picker] set real extension instead always jpg #1269
[image_picker] set real extension instead always jpg #1269
Conversation
There was a problem hiding this 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()); |
There was a problem hiding this comment.
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 ""
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) { | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things here:
-
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 theCursor
here? -
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?
There was a problem hiding this 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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things here:
-
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 theCursor
here? -
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); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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.
@mklim please review new updates |
There was a problem hiding this 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.
There was a problem hiding this 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>
No description provided.