fix: Update MIME type handling in CustomWebChromeClient#178
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the getMimeType function in DeviceWebview.kt to improve MIME type detection, specifically by allowing types that already contain a slash and changing the default fallback to /. A review comment suggests utilizing android.webkit.MimeTypeMap to handle a broader range of file extensions dynamically and highlights a potential limitation where only the first valid MIME type is returned, which may restrict file selection when multiple types are requested.
| return when { | ||
| type == ".json" -> "application/json" | ||
| type == ".css" -> "text/css" | ||
| type.contains("/") -> type | ||
| else -> continue | ||
| } |
There was a problem hiding this comment.
Instead of hardcoding specific extensions like .json and .css, you can use android.webkit.MimeTypeMap to support a wider range of file types.
Also, note that this logic returns the first valid MIME type found and ignores any subsequent ones. If a web page specifies multiple types (e.g., accept="image/*, application/pdf"), the file picker will be restricted to only the first type, preventing the user from selecting others. For a more robust implementation, consider handling multiple MIME types or falling back to */* when more than one type is provided.
| return when { | |
| type == ".json" -> "application/json" | |
| type == ".css" -> "text/css" | |
| type.contains("/") -> type | |
| else -> continue | |
| } | |
| return when { | |
| type.startsWith(".") -> android.webkit.MimeTypeMap.getSingleton() | |
| .getMimeTypeFromExtension(type.substring(1)) ?: continue | |
| type.contains("/") -> type | |
| else -> continue | |
| } |
Note
Implemented by Copilot. I tested it on my phone.
Fixes #141
Previously it was loading a generic file picker with all files grayed out.
Now it directly opens image picker in "Image Tool" and file picker in "Software Update" (that accepts any file type).
Summary
getMimeType()now passes through any value already intype/subtypeformat (e.g.image/*,image/jpeg) directly to the file picker intentapplication/octet-streamto*/*so that all files are selectable when no specific type is recognisedMotivation
WLED's web UI uses a standard HTML
<input type="file" accept="image/*">element for image uploads. When this triggers the Android file chooser, the browser passes theacceptattribute value (e.g.image/*) as-is inFileChooserParams.acceptTypes.getMimeType()only handled two specific file extensions (.json→application/json,.css→text/css). Any other value — including valid MIME types likeimage/*— fell through thewhenexpression and returned the hardcoded fallbackapplication/octet-stream.Intent.ACTION_GET_CONTENTuses the intent type to filter which files the user can select. When the type isapplication/octet-stream, the file picker renders images (and most other files) as grayed-out and non-selectable, because their actual MIME types do not match.The fix recognises that any string containing
/is already a valid MIME type and forwards it directly to the intent, soimage/*correctly enables image selection. The fallback is also relaxed to*/*to keep all files accessible when the accept type is unknown.How to Test