diff --git a/AnkiDroid/src/test/java/com/ichi2/utils/AssetHelperTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/utils/AssetHelperTest.kt similarity index 68% rename from AnkiDroid/src/test/java/com/ichi2/utils/AssetHelperTest.kt rename to AnkiDroid/src/androidTest/java/com/ichi2/anki/utils/AssetHelperTest.kt index 82901eef918b..af9dc7d5a5f1 100644 --- a/AnkiDroid/src/test/java/com/ichi2/utils/AssetHelperTest.kt +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/utils/AssetHelperTest.kt @@ -13,15 +13,23 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -package com.ichi2.utils +package com.ichi2.anki.utils +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.ichi2.utils.AssetHelper import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.MatcherAssert.assertThat import org.junit.Test +import org.junit.runner.RunWith +@RunWith(AndroidJUnit4::class) class AssetHelperTest { @Test fun guessMimeTypeTest() { + assertThat(AssetHelper.guessMimeType("test.js"), equalTo("text/javascript")) + assertThat(AssetHelper.guessMimeType("test.mjs"), equalTo("text/javascript")) + assertThat(AssetHelper.guessMimeType("test.json"), equalTo("application/json")) + assertThat(AssetHelper.guessMimeType("test.css"), equalTo("text/css")) assertThat(AssetHelper.guessMimeType("test.txt"), equalTo("text/plain")) assertThat(AssetHelper.guessMimeType("test.png"), equalTo("image/png")) assertThat(AssetHelper.guessMimeType("test.zip"), equalTo("application/zip")) diff --git a/AnkiDroid/src/main/java/com/ichi2/utils/AssetHelper.kt b/AnkiDroid/src/main/java/com/ichi2/utils/AssetHelper.kt index 6d2c249e8a4a..ef959ad3ec9c 100644 --- a/AnkiDroid/src/main/java/com/ichi2/utils/AssetHelper.kt +++ b/AnkiDroid/src/main/java/com/ichi2/utils/AssetHelper.kt @@ -35,12 +35,12 @@ */ package com.ichi2.utils -import java.net.URLConnection +import android.webkit.MimeTypeMap /** Clone of RestrictedApi functionality */ object AssetHelper { /** - * Use [URLConnection.guessContentTypeFromName] to guess MIME type or return the + * Use [MimeTypeMap.getMimeTypeFromExtension] to guess MIME type or return the * "text/plain" if it can't guess. * * Copy of [androidx.webkit.internal.AssetHelper.guessMimeType] @@ -49,7 +49,12 @@ object AssetHelper { * @return MIME type guessed from file extension or "text/plain". */ fun guessMimeType(path: String?): String { - val mimeType = URLConnection.guessContentTypeFromName(path) - return mimeType ?: "text/plain" + val extension = MimeTypeMap.getFileExtensionFromUrl(path) + return when (extension) { + "js" -> "text/javascript" + "mjs" -> "text/javascript" + "json" -> "application/json" + else -> MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: "text/plain" + } } }