mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-09 15:27:55 -04:00
Merge pull request #2809 from kiwix/Issue#2793
Fixing .js ending articles broken rendering
This commit is contained in:
commit
24435389aa
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2022 Kiwix <android.kiwix.org>
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.kiwixmobile.mimetype
|
||||||
|
|
||||||
|
import androidx.core.content.edit
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import androidx.test.rule.ActivityTestRule
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
import org.kiwix.kiwixlib.JNIKiwixReader
|
||||||
|
import org.kiwix.kiwixmobile.BaseActivityTest
|
||||||
|
import org.kiwix.kiwixmobile.core.NightModeConfig
|
||||||
|
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
|
||||||
|
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||||
|
import org.kiwix.kiwixmobile.main.KiwixMainActivity
|
||||||
|
import java.io.File
|
||||||
|
import java.io.FileOutputStream
|
||||||
|
import java.io.OutputStream
|
||||||
|
|
||||||
|
class MimeTypeTest : BaseActivityTest() {
|
||||||
|
|
||||||
|
override var activityRule: ActivityTestRule<KiwixMainActivity> = activityTestRule {
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||||
|
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
|
||||||
|
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMimeType() {
|
||||||
|
val loadFileStream = MimeTypeTest::class.java.classLoader.getResourceAsStream("testzim.zim")
|
||||||
|
val zimFile = File(context.cacheDir, "testzim.zim")
|
||||||
|
if (zimFile.exists()) zimFile.delete()
|
||||||
|
zimFile.createNewFile()
|
||||||
|
loadFileStream.use { inputStream ->
|
||||||
|
val outputStream: OutputStream = FileOutputStream(zimFile)
|
||||||
|
outputStream.use { it ->
|
||||||
|
val buffer = ByteArray(1024)
|
||||||
|
var length: Int
|
||||||
|
while (inputStream.read(buffer).also { length = it } > 0) {
|
||||||
|
it.write(buffer, 0, length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val zimFileReader = ZimFileReader(
|
||||||
|
zimFile,
|
||||||
|
JNIKiwixReader(zimFile.canonicalPath),
|
||||||
|
NightModeConfig(SharedPreferenceUtil(context), context)
|
||||||
|
)
|
||||||
|
zimFileReader.getRandomArticleUrl()?.let {
|
||||||
|
val mimeType = zimFileReader.readContentAndMimeType(it)
|
||||||
|
if (mimeType.contains("^([^ ]+).*$") || mimeType.contains(";")) {
|
||||||
|
Assert.fail(
|
||||||
|
"Unable to get mime type from zim file. File = " +
|
||||||
|
" $zimFile and url of article = $it"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}.also {
|
||||||
|
zimFileReader.dispose()
|
||||||
|
} ?: kotlin.run {
|
||||||
|
Assert.fail("Unable to get article from zim file $zimFile")
|
||||||
|
}.also {
|
||||||
|
zimFileReader.dispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -21,7 +21,6 @@ import android.content.res.AssetFileDescriptor
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.ParcelFileDescriptor
|
import android.os.ParcelFileDescriptor
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.webkit.MimeTypeMap
|
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import io.reactivex.Completable
|
import io.reactivex.Completable
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
@ -127,13 +126,10 @@ class ZimFileReader constructor(
|
|||||||
return loadContent(uri)
|
return loadContent(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readMimeType(uri: String) = uri.filePath.let {
|
fun readContentAndMimeType(uri: String): String = getContentAndMimeType(uri)
|
||||||
it.mimeType?.takeIf(String::isNotEmpty) ?: mimeTypeFromReader(it) ?: DEFAULT_MIME_TYPE
|
.second.truncateMimeType.also {
|
||||||
}.also { Log.d(TAG, "getting mimetype for $uri = $it") }
|
Log.d(TAG, "getting mimetype for $uri = $it")
|
||||||
|
}
|
||||||
private fun mimeTypeFromReader(it: String) =
|
|
||||||
// Truncate mime-type (everything after the first space and semi-colon(if exists)
|
|
||||||
jniKiwixReader.getMimeType(it.filePath)?.replace("^([^ ]+).*$", "$1")?.substringBefore(";")
|
|
||||||
|
|
||||||
fun getRedirect(url: String) = "${toRedirect(url)}"
|
fun getRedirect(url: String) = "${toRedirect(url)}"
|
||||||
|
|
||||||
@ -264,7 +260,6 @@ class ZimFileReader constructor(
|
|||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
private val assetExtensions =
|
private val assetExtensions =
|
||||||
listOf("3gp", "mp4", "m4a", "webm", "mkv", "ogg", "ogv", "svg", "warc")
|
listOf("3gp", "mp4", "m4a", "webm", "mkv", "ogg", "ogv", "svg", "warc")
|
||||||
private const val DEFAULT_MIME_TYPE = "application/octet-stream"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,9 +267,10 @@ private val Uri.filePath: String
|
|||||||
get() = toString().filePath
|
get() = toString().filePath
|
||||||
private val String.filePath: String
|
private val String.filePath: String
|
||||||
get() = substringAfter(CONTENT_PREFIX).substringBefore("#").substringBefore("?")
|
get() = substringAfter(CONTENT_PREFIX).substringBefore("#").substringBefore("?")
|
||||||
private val String.mimeType: String?
|
|
||||||
get() = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
|
// Truncate mime-type (everything after the first space and semi-colon(if exists)
|
||||||
MimeTypeMap.getFileExtensionFromUrl(this)
|
val String.truncateMimeType: String
|
||||||
)
|
get() = replace("^([^ ]+).*$", "$1").substringBefore(";")
|
||||||
|
|
||||||
private val Pair.parcelFileDescriptor: ParcelFileDescriptor?
|
private val Pair.parcelFileDescriptor: ParcelFileDescriptor?
|
||||||
get() = ParcelFileDescriptor.open(File(filename), ParcelFileDescriptor.MODE_READ_ONLY)
|
get() = ParcelFileDescriptor.open(File(filename), ParcelFileDescriptor.MODE_READ_ONLY)
|
||||||
|
@ -48,7 +48,11 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F
|
|||||||
fun getRedirect(url: String): String = zimFileReader?.getRedirect(url) ?: ""
|
fun getRedirect(url: String): String = zimFileReader?.getRedirect(url) ?: ""
|
||||||
fun load(url: String, requestHeaders: Map<String, String>): WebResourceResponse {
|
fun load(url: String, requestHeaders: Map<String, String>): WebResourceResponse {
|
||||||
val data = zimFileReader?.load(url)
|
val data = zimFileReader?.load(url)
|
||||||
return WebResourceResponse(zimFileReader?.readMimeType(url), Charsets.UTF_8.name(), data)
|
return WebResourceResponse(
|
||||||
|
zimFileReader?.readContentAndMimeType(url),
|
||||||
|
Charsets.UTF_8.name(),
|
||||||
|
data
|
||||||
|
)
|
||||||
.apply {
|
.apply {
|
||||||
val headers = mutableMapOf("Accept-Ranges" to "bytes")
|
val headers = mutableMapOf("Accept-Ranges" to "bytes")
|
||||||
if ("Range" in requestHeaders.keys) {
|
if ("Range" in requestHeaders.keys) {
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Kiwix Android
|
||||||
|
* Copyright (c) 2022 Kiwix <android.kiwix.org>
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.kiwix.kiwixmobile.core.reader
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
|
||||||
|
class ZimFileReaderTest {
|
||||||
|
@Test
|
||||||
|
fun checkMimeTypeWithSemicolon() {
|
||||||
|
val mimeTypeHtml = "text/html;"
|
||||||
|
assertEquals("text/html", mimeTypeHtml.truncateMimeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun checkMimeTypeWithSpecialCharacters() {
|
||||||
|
val mimeTypeCss = "text/css^([^ ]+).*\$"
|
||||||
|
assertEquals("text/css$1", mimeTypeCss.truncateMimeType)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun checkMimeTypeWithSemicolonBeforeSpecialCharacters() {
|
||||||
|
val mimeTypeCss = "text/application;^([^ ]+).*\$;"
|
||||||
|
assertEquals("text/application", mimeTypeCss.truncateMimeType)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user