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
10 changes: 10 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ package androidx.core.graphics.drawable {

}

package androidx.core.io {

public final class FileKt {
ctor public FileKt();
method public static String? getMimeType(java.io.File);
}

}

package androidx.core.net {

public final class UriKt {
Expand Down Expand Up @@ -412,6 +421,7 @@ package androidx.core.text {
public final class StringKt {
ctor public StringKt();
method public static String htmlEncode(String);
method public static String urlEncode(String);
}

}
Expand Down
55 changes: 55 additions & 0 deletions src/androidTest/java/androidx/core/io/FileTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.core.io

import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Test
import java.io.File

class FileTest {

@Test fun jpg() {
val photo = File("sunset.jpg")
val type = photo.getMimeType()
assertSame("image/jpeg", type)
}

@Test fun png() {
val logo = File("images/logo.png")
val type = logo.getMimeType()
assertSame("image/png", type)
}

@Test fun pdfWithSpaces() {
val thesis = File("my documents/master thesis-v5.1-final-final2.pdf")
val type = thesis.getMimeType()
assertSame("application/pdf", type)
}

@Test fun noExtension() {
val path = File("foo/bar")
val type = path.getMimeType()
assertNull(type)
}

@Test fun unknownExtension() {
val unknownFile = File("foo/bar.baz")
val type = unknownFile.getMimeType()
assertNull(type)
}
}
32 changes: 32 additions & 0 deletions src/main/java/androidx/core/io/File.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.core.io

import android.webkit.MimeTypeMap
import androidx.core.text.urlEncode
import java.io.File

/**
* 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"

*/
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?.

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.

?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) }
}
12 changes: 11 additions & 1 deletion src/main/java/androidx/core/text/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
* limitations under the License.
*/

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


package androidx.core.text

import android.net.Uri
import android.text.TextUtils

/**
Expand All @@ -26,3 +29,10 @@ import android.text.TextUtils
* @see TextUtils.htmlEncode
*/
inline fun String.htmlEncode(): String = TextUtils.htmlEncode(this)

/**
* Url-encode the string.
*
* @see Uri.encode
*/
inline fun String.urlEncode(): String = Uri.encode(this)