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

[image_picker_android] Adjust file extension in FileUtils when it does not match its mime type #3409

Conversation

SynSzakala
Copy link
Contributor

Sometimes a file name returned by the ContentResolver can have an extension that doesn't match its real type (e.g. if the file was re-encoded).
Previously file extension was selected only based on the mime type. flutter/plugins#6096 changed it, so that the name returned by ContentResolver was used if available, with mime-based extension used only as fallback.
This PR fixes the regression by changing extension of the name returned from ContentResolver so that it matches its mime type (if known).

Fixes flutter/flutter#121878.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • [] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@SynSzakala SynSzakala requested a review from gmackall as a code owner March 7, 2023 23:53
jakub.walusiak added 2 commits March 8, 2023 00:59
Copy link
Contributor

@stuartmorgan stuartmorgan 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 follow up PR!

These changes aren't in the PR that was uploaded.

@@ -121,4 +128,8 @@ private static void copy(InputStream in, OutputStream out) throws IOException {
}
out.flush();
}

private static String getBaseName(String fileName) {
return fileName.split("\\.")[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

The basename is everything before the last ., not the first, so this will return the wrong name in some cases.

shadowContentResolver.registerInputStream(
uri, new ByteArrayInputStream("imageStream".getBytes(UTF_8)));
String path = fileUtils.getPathFromUri(context, uri);
assertTrue(path.endsWith("png.png"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do not make the filename and the extension the same; this would mask bugs that used the wrong components when constructing the output name.

String path = fileUtils.getPathFromUri(context, uri);
assertTrue(path.endsWith("png.png"));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add another test for the case where a file's basename contains .s.

String path = fileUtils.getPathFromUri(context, uri);
assertTrue(path.endsWith("png.png"));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

You created a new codepath in this PR for the case where the filename is non-null but the extension is null; that case needs a test as well.

jakub.walusiak added 2 commits March 11, 2023 19:08
…d_fix_mismatched_extension

# Conflicts:
#	packages/image_picker/image_picker_android/CHANGELOG.md
#	packages/image_picker/image_picker_android/pubspec.yaml
@SynSzakala SynSzakala requested review from stuartmorgan and removed request for gmackall March 11, 2023 18:12
Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@gmackall gmackall left a comment

Choose a reason for hiding this comment

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

LGTM as well!

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 16, 2023
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 16, 2023
@auto-submit
Copy link
Contributor

auto-submit bot commented Mar 16, 2023

auto label is removed for flutter/packages, pr: 3409, due to - This commit is not mergeable and has conflicts. Please rebase your PR and fix all the conflicts.

@SynSzakala
Copy link
Contributor Author

@stuartmorgan Fixed conflicts, auto-submit can be re-added.

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 17, 2023
@stuartmorgan
Copy link
Contributor

Thanks!

@auto-submit auto-submit bot merged commit 74fd094 into flutter:main Mar 17, 2023
@reimager
Copy link

Thank you all!

tarrinneal pushed a commit to flutter/flutter that referenced this pull request Mar 20, 2023
* 7636eb7 [go_router_builder] Support default value for `Set`, `List` and `Iterable` route parameters (flutter/packages#3391)

* 26c95da [image_picker] Move HashSet construction within if-statement (flutter/packages#3448)

* f5687b2 [image_picker] fix typos in comments (flutter/packages#3413)

* 84afba7 [image_picker] Migrate Android to Pigeon (flutter/packages#3476)

* 42927fc [image_picker]: Bump androidx.exifinterface:exifinterface from 1.3.3 to 1.3.6 in /packages/image_picker/image_picker_android/android (flutter/packages#3238)

* 9a44bdf Require Dart SDK 2.14, because of using APIs. (flutter/packages#3468)

* 12609a2 [ci] Clean up iOS simulators (flutter/packages#3458)

* 9b136a9 [ci/tool] Add external dependency validation (flutter/packages#3466)

* 11aab47 Manual roll Flutter from fb7e828 to 67e5f66 (8 revisions) (flutter/packages#3482)

* 784291b Update goldens (flutter/packages#3442)

* 43a42d1 [webview_flutter_android] Updates Dart and Java InstanceManagers (flutter/packages#3282)

* d0de136 [camera] Reland android flip/change camera while recording (flutter/packages#3460)

* 74fd094 [image_picker_android] Adjust file extension in FileUtils when it does not match its mime type (flutter/packages#3409)

* d2f1d2d [flutter_adaptive_scaffold] : 🐛 [FIX] : Issue: 121135. (flutter/packages#3253)

* 3d078b5 Roll Flutter from 67e5f66 to 53dfd23 (39 revisions) (flutter/packages#3484)

* 3b3a09d [url_launcher] Convert iOS to Pigeon (flutter/packages#3481)

* 80cd50a Roll Flutter from 53dfd23 to 6bd2b3c (17 revisions) (flutter/packages#3486)

* 998bb29 [webview_flutter] Updates the README with the migration of `WebView.initialCookies` and Hybrid Composition on (flutter/packages#3470)

* bbf86a7 Roll Flutter from 6bd2b3c to 3e4e092 (7 revisions) (flutter/packages#3490)
nploi pushed a commit to nploi/packages that referenced this pull request Jul 16, 2023
…s not match its mime type (flutter#3409)

[image_picker_android] Adjust file extension in FileUtils when it does not match its mime type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: image_picker platform-android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[image_picker] Invalid file name for chosen png files
4 participants