-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Windows] Fix FilePicker Returns wrong ContentType for *.webp files #31913
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
base: main
Are you sure you want to change the base?
[Windows] Fix FilePicker Returns wrong ContentType for *.webp files #31913
Conversation
Hey there @@devanathan-vaithiyanathan! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
{ ".htm", "text/html" }, | ||
{ ".css", "text/css" }, | ||
{ ".js", "application/javascript" }, | ||
{ ".json", "application/json" }, |
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.
Should constants from https://github.com/dotnet/runtime/blob/d4269d0e47ef402b0646ab997a8ed5cbf95b87d2/src/libraries/System.Net.Mail/src/System/Net/Mime/MediaTypeNames.cs#L18 be used if there are available?
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.
@MartyIX , Thanks for suggestion, I've modified the changes
{ | ||
// Static mapping for file extensions to MIME types | ||
// Used as fallback when Windows StorageFile.ContentType doesn't provide correct MIME type | ||
static readonly Dictionary<string, string> ExtensionToMimeTypeMap = new(StringComparer.OrdinalIgnoreCase) |
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.
In this case, use as fallback and only for reading, for faster lookups can use a FrozenDictionary
.
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.
@jsuarezruiz , Thanks for the suggestion. I have updated the implementation to use FrozenDictionary for faster read-only lookups instead of a regular Dictionary.
|
||
#if WINDOWS | ||
[Fact] | ||
public async Task EnsureFileResultContentType() |
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.
To test correctly PlatformGetContentType
, could include more tests?
- Trailing Spaces: ".jpg "
- Case Variations: ".JPG", ".Jpg", ".jPg"
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.
@jsuarezruiz , Good point! I have added additional test cases to cover trailing spaces and case-insensitive extensions to ensure PlatformGetContentType handles these scenarios correctly.
if (string.IsNullOrWhiteSpace(extension)) | ||
return null; | ||
|
||
extension = extension.ToLowerInvariant(); |
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.
Not common, but would require changes to correctly manage multiple dots. Example: .tar.gz
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.
I've included a test case for multi-dot extensions like .tar.gz and adjust the logic if necessary to ensure proper content type resolution.
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Issue Details
On Windows, the StorageFile.ContentType API often returns a generic or incorrect MIME type (e.g., .webp → application/octet-stream), causing FilePicker to report wrong or empty ContentType values.
Description of Change
Fixed by adding an extension-to-MIME mapping dictionary in FileSystem.uwp.cs that includes .webp and other formats. The solution uses fallback logic - it preserves Windows' native ContentType when correct, but automatically uses our mapping when Windows returns the generic "application/octet-stream" or empty values.
Issues Fixed
Fixes #31808
Tested the behavior in the following platforms.
Output Screenshot
Before-fix.mp4
After-fix.mp4