From c059a7395e71b51281a7e75be450b3bab1a578f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Mon, 19 Mar 2018 22:53:45 +0100 Subject: [PATCH 01/10] Added File.getMimeType extension. --- src/main/java/androidx/core/io/File.kt | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/androidx/core/io/File.kt 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..117a9fa5 --- /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 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. + */ +fun File.getMimeType(): String? { + return MimeTypeMap.getFileExtensionFromUrl(toString()) + ?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) } +} From fab716007f5afd01d89f502d14029620a2b39e8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Mon, 19 Mar 2018 23:03:12 +0100 Subject: [PATCH 02/10] Added test for jepd and png. --- .../java/androidx/core/io/FileTest.kt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/androidTest/java/androidx/core/io/FileTest.kt 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..882d4d8a --- /dev/null +++ b/src/androidTest/java/androidx/core/io/FileTest.kt @@ -0,0 +1,37 @@ +/* + * 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.assertSame +import org.junit.Test +import java.io.File + +class FileTest { + + @Test fun jpeg() { + 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) + } + +} \ No newline at end of file From 708e3279336f8d956d37f69900d76d25f1426dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Mon, 19 Mar 2018 23:05:17 +0100 Subject: [PATCH 03/10] Updated api. --- api/current.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/api/current.txt b/api/current.txt index e805fde9..560622f5 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? getMimeType(java.io.File); + } + +} + package androidx.core.net { public final class UriKt { From 6faf0b6a38089019829fd7fd1e89db405d1b2766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Mon, 19 Mar 2018 23:21:09 +0100 Subject: [PATCH 04/10] Added two more tests for missing and unknown extension. --- src/androidTest/java/androidx/core/io/FileTest.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/androidTest/java/androidx/core/io/FileTest.kt b/src/androidTest/java/androidx/core/io/FileTest.kt index 882d4d8a..6e41fe07 100644 --- a/src/androidTest/java/androidx/core/io/FileTest.kt +++ b/src/androidTest/java/androidx/core/io/FileTest.kt @@ -16,13 +16,14 @@ 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 jpeg() { + @Test fun jpg() { val photo = File("sunset.jpg") val type = photo.getMimeType() assertSame("image/jpeg", type) @@ -34,4 +35,15 @@ class FileTest { assertSame("image/png", 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) + } } \ No newline at end of file From 4601222529581be3cc3cf9b42db76cd7a9a95d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Tue, 20 Mar 2018 12:38:06 +0100 Subject: [PATCH 05/10] Url encodes the path plus test for a path with spaces. --- src/androidTest/java/androidx/core/io/FileTest.kt | 6 ++++++ src/main/java/androidx/core/io/File.kt | 3 ++- src/main/java/androidx/core/text/String.kt | 12 +++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/androidTest/java/androidx/core/io/FileTest.kt b/src/androidTest/java/androidx/core/io/FileTest.kt index 6e41fe07..460af33f 100644 --- a/src/androidTest/java/androidx/core/io/FileTest.kt +++ b/src/androidTest/java/androidx/core/io/FileTest.kt @@ -35,6 +35,12 @@ class FileTest { 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() diff --git a/src/main/java/androidx/core/io/File.kt b/src/main/java/androidx/core/io/File.kt index 117a9fa5..59231cb3 100644 --- a/src/main/java/androidx/core/io/File.kt +++ b/src/main/java/androidx/core/io/File.kt @@ -17,6 +17,7 @@ package androidx.core.io import android.webkit.MimeTypeMap +import androidx.core.text.urlEncode import java.io.File /** @@ -26,6 +27,6 @@ import java.io.File * @return The MIME type for the files' extension or null iff there is none. */ fun File.getMimeType(): String? { - return MimeTypeMap.getFileExtensionFromUrl(toString()) + return MimeTypeMap.getFileExtensionFromUrl(toString().urlEncode()) ?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) } } diff --git a/src/main/java/androidx/core/text/String.kt b/src/main/java/androidx/core/text/String.kt index 8bffdd0e..da3e086c 100644 --- a/src/main/java/androidx/core/text/String.kt +++ b/src/main/java/androidx/core/text/String.kt @@ -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. package androidx.core.text +import android.net.Uri import android.text.TextUtils /** @@ -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) From d446f318382d9f22420cb213ad63d48c8de5590e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Tue, 20 Mar 2018 12:40:56 +0100 Subject: [PATCH 06/10] Updated api. --- api/current.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/api/current.txt b/api/current.txt index 560622f5..930f86d9 100644 --- a/api/current.txt +++ b/api/current.txt @@ -421,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); } } From 3460aac2cd944eb2dc5f05e5995cb49c05cb0db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Fri, 23 Mar 2018 17:44:49 +0100 Subject: [PATCH 07/10] Renaming and formatting. --- src/main/java/androidx/core/io/File.kt | 4 ++-- src/main/java/androidx/core/text/String.kt | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/androidx/core/io/File.kt b/src/main/java/androidx/core/io/File.kt index 59231cb3..8dafff59 100644 --- a/src/main/java/androidx/core/io/File.kt +++ b/src/main/java/androidx/core/io/File.kt @@ -24,9 +24,9 @@ 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. + * @return The MIME type for the files' extension or null if there is none. */ fun File.getMimeType(): String? { - return MimeTypeMap.getFileExtensionFromUrl(toString().urlEncode()) + return MimeTypeMap.getFileExtensionFromUrl(path.urlEncode()) ?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) } } diff --git a/src/main/java/androidx/core/text/String.kt b/src/main/java/androidx/core/text/String.kt index da3e086c..f2274509 100644 --- a/src/main/java/androidx/core/text/String.kt +++ b/src/main/java/androidx/core/text/String.kt @@ -14,9 +14,7 @@ * limitations under the License. */ -@file:Suppress("NOTHING_TO_INLINE") - -// Aliases to public API. +@file:Suppress("NOTHING_TO_INLINE") // Aliases to public API. package androidx.core.text From 408b1e65af7e43f5c8c090924c2c9fab96374bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Mon, 26 Mar 2018 12:27:13 +0200 Subject: [PATCH 08/10] Renamed method getMimeType() a property and renamed it to mimeTypeFromExtension. --- src/androidTest/java/androidx/core/io/FileTest.kt | 10 +++++----- src/main/java/androidx/core/io/File.kt | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/androidTest/java/androidx/core/io/FileTest.kt b/src/androidTest/java/androidx/core/io/FileTest.kt index 460af33f..10c00db4 100644 --- a/src/androidTest/java/androidx/core/io/FileTest.kt +++ b/src/androidTest/java/androidx/core/io/FileTest.kt @@ -25,31 +25,31 @@ class FileTest { @Test fun jpg() { val photo = File("sunset.jpg") - val type = photo.getMimeType() + val type = photo.mimeTypeFromExtension assertSame("image/jpeg", type) } @Test fun png() { val logo = File("images/logo.png") - val type = logo.getMimeType() + 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.getMimeType() + val type = thesis.mimeTypeFromExtension assertSame("application/pdf", type) } @Test fun noExtension() { val path = File("foo/bar") - val type = path.getMimeType() + val type = path.mimeTypeFromExtension assertNull(type) } @Test fun unknownExtension() { val unknownFile = File("foo/bar.baz") - val type = unknownFile.getMimeType() + 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 index 8dafff59..c10ce77f 100644 --- a/src/main/java/androidx/core/io/File.kt +++ b/src/main/java/androidx/core/io/File.kt @@ -21,12 +21,12 @@ import androidx.core.text.urlEncode import java.io.File /** - * Returns the MIME type (content type) of this [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. */ -fun File.getMimeType(): String? { - return MimeTypeMap.getFileExtensionFromUrl(path.urlEncode()) +val File.mimeTypeFromExtension: String? + get() = MimeTypeMap.getFileExtensionFromUrl(path.urlEncode()) ?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) } -} + From 6a1f9b97caf674d5db2d54ce9621ed11b542610e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Mon, 26 Mar 2018 12:28:24 +0200 Subject: [PATCH 09/10] Updated api. --- api/current.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/current.txt b/api/current.txt index 930f86d9..e3324204 100644 --- a/api/current.txt +++ b/api/current.txt @@ -312,7 +312,7 @@ package androidx.core.io { public final class FileKt { ctor public FileKt(); - method public static String? getMimeType(java.io.File); + method public static String? getMimeTypeFromExtension(java.io.File); } } From ce47ff518e1a44cfa5094df3e128f2e483dad53a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=BCrg?= Date: Mon, 26 Mar 2018 13:45:48 +0200 Subject: [PATCH 10/10] Removed empty line. --- src/main/java/androidx/core/io/File.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/androidx/core/io/File.kt b/src/main/java/androidx/core/io/File.kt index c10ce77f..2e7c5a62 100644 --- a/src/main/java/androidx/core/io/File.kt +++ b/src/main/java/androidx/core/io/File.kt @@ -28,5 +28,4 @@ import java.io.File */ val File.mimeTypeFromExtension: String? get() = MimeTypeMap.getFileExtensionFromUrl(path.urlEncode()) - ?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) } - + ?.apply { MimeTypeMap.getSingleton().getMimeTypeFromExtension(toLowerCase()) } \ No newline at end of file