-
Notifications
You must be signed in to change notification settings - Fork 563
Get the MIME type from File and UrlEncode #442
base: master
Are you sure you want to change the base?
Conversation
@tobiasschuerg What if there are spaces in your file path? It will not work in that condition. You need to use encode uri for that purpose. Like,
|
* @return The MIME type for the files' extension or null iff there is none. | ||
*/ | ||
fun File.getMimeType(): String? { | ||
return MimeTypeMap.getFileExtensionFromUrl(toString().urlEncode()) |
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.
IMHO, We can use path instead of toString() as it easy to understand.
@file:Suppress("NOTHING_TO_INLINE") // Aliases to public API. | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
// Aliases to public API. |
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: Remove extra spaces. Keep this consistent as others.
* Returns the MIME type (content type) of this [File]. | ||
* | ||
* @see MimeUtils | ||
* @return The MIME type for the files' extension or null iff there is none. |
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: There should be "if" instead of "iff"
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!
* @see MimeUtils | ||
* @return The MIME type for the files' extension or null if there is none. | ||
*/ | ||
fun File.getMimeType(): String? { |
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.
The syntax seems confusing:
- In Kotlin we have properties. Declaring a method instead of a property may be used as a way of paying reader's attention to a possible heavy computations behind the call. I'm not sure about
getMimeTypeFromExtension
's performance. But if it is not so time consuming, declaring it as a property seems a better choice. - The name. After I've read
file.getMimeType()
the first time, my first impression was "hmm, does it guesses mime type by reading first few bytes of a file?". Then I read implementation and found it actually usesgetMimeTypeFromExtension
and doesn't read file content. Note how SDK's naming pays attention to the way the mime type is guessed. This implementation detail may be important at the call site.
I'd suggest to declare it as val File.mimeTypeFromExtension: String?
or fun File.guessMimeTypeFromExtension(): String?
.
Get the MIME type of a
File
via