Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

Get the MIME type from File and UrlEncode #442

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Get the MIME type from File and UrlEncode #442

wants to merge 12 commits into from

Conversation

tobiasschuerg
Copy link

@tobiasschuerg tobiasschuerg commented Mar 19, 2018

Get the MIME type of a File via

val type: String? = file.getMimeType()

@Sathawale27
Copy link
Contributor

Sathawale27 commented Mar 20, 2018

@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,

val file = File("mnt/sdcard/abc test file.png")

* @return The MIME type for the files' extension or null iff there is none.
*/
fun File.getMimeType(): String? {
return MimeTypeMap.getFileExtensionFromUrl(toString().urlEncode())
Copy link
Contributor

@Sathawale27 Sathawale27 Mar 22, 2018

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.
Copy link
Contributor

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.
Copy link
Contributor

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"

Copy link
Contributor

@Sathawale27 Sathawale27 left a 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? {

Choose a reason for hiding this comment

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

The syntax seems confusing:

  1. 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.
  2. 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 uses getMimeTypeFromExtension 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?.

@tobiasschuerg tobiasschuerg changed the title Get the MIME type from File Get the MIME type from File and UrlEncode Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants