Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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? getMimeTypeFromExtension(java.io.File);
}

}

package androidx.core.net {

public final class UriKt {
Expand Down Expand Up @@ -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);
}

}
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.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)
}
}
31 changes: 31 additions & 0 deletions src/main/java/androidx/core/io/File.kt
Original file line number Diff line number Diff line change
@@ -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()) }
8 changes: 8 additions & 0 deletions src/main/java/androidx/core/text/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package androidx.core.text

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

/**
Expand All @@ -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)