diff --git a/api/current.txt b/api/current.txt index 4c56ac98..09400d7e 100644 --- a/api/current.txt +++ b/api/current.txt @@ -308,6 +308,15 @@ package androidx.core.graphics.drawable { } +package androidx.core.io { + + public final class FileKt { + ctor public FileKt(); + method public static String? getMimeTypeFromExtension(java.io.File); + } + +} + package androidx.core.net { public final class UriKt { @@ -409,6 +418,7 @@ package androidx.core.text { public final class StringKt { ctor public StringKt(); method public static String htmlEncode(String); + method public static String urlEncode(String); } } diff --git a/src/androidTest/java/androidx/core/io/FileTest.kt b/src/androidTest/java/androidx/core/io/FileTest.kt new file mode 100644 index 00000000..10c00db4 --- /dev/null +++ b/src/androidTest/java/androidx/core/io/FileTest.kt @@ -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.mimeTypeFromExtension + assertSame("image/jpeg", type) + } + + @Test fun png() { + val logo = File("images/logo.png") + val type = logo.mimeTypeFromExtension + assertSame("image/png", type) + } + + @Test fun pdfWithSpaces() { + val thesis = File("my documents/master thesis-v5.1-final-final2.pdf") + val type = thesis.mimeTypeFromExtension + assertSame("application/pdf", type) + } + + @Test fun noExtension() { + val path = File("foo/bar") + val type = path.mimeTypeFromExtension + assertNull(type) + } + + @Test fun unknownExtension() { + val unknownFile = File("foo/bar.baz") + val type = unknownFile.mimeTypeFromExtension + assertNull(type) + } +} \ No newline at end of file diff --git a/src/main/java/androidx/core/io/File.kt b/src/main/java/androidx/core/io/File.kt new file mode 100644 index 00000000..2e7c5a62 --- /dev/null +++ b/src/main/java/androidx/core/io/File.kt @@ -0,0 +1,31 @@ +/* + * 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] based on its extension. + * + * @see MimeUtils + * @return The MIME type for the files' extension or null if there is none. + */ +val File.mimeTypeFromExtension: String? + get() = MimeTypeMap.getFileExtensionFromUrl(path.urlEncode()) + ?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) } \ No newline at end of file diff --git a/src/main/java/androidx/core/text/String.kt b/src/main/java/androidx/core/text/String.kt index 8bffdd0e..f2274509 100644 --- a/src/main/java/androidx/core/text/String.kt +++ b/src/main/java/androidx/core/text/String.kt @@ -18,6 +18,7 @@ package androidx.core.text +import android.net.Uri import android.text.TextUtils /** @@ -26,3 +27,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)