Skip to content

Commit

Permalink
Improved article loading.
Browse files Browse the repository at this point in the history
* We are previously checking `hasEntryByPath`, `hasEntryByTitle`, `mainEntry.isRedirect` which are internally calling the same function as we are calling after checking this condition so it would be better to directly use those function to avoid calling same function twice see more details kiwix/java-libkiwix#60.
  • Loading branch information
MohitMaliDeveloper authored and kelson42 committed Aug 29, 2023
1 parent ca05e2a commit e01c88e
Showing 1 changed file with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import org.kiwix.kiwixmobile.core.utils.files.FileUtils
import org.kiwix.libkiwix.JNIKiwixException
import org.kiwix.libzim.Archive
import org.kiwix.libzim.DirectAccessInfo
import org.kiwix.libzim.EntryNotFoundException
import org.kiwix.libzim.Item
import org.kiwix.libzim.Query
import org.kiwix.libzim.Search
Expand Down Expand Up @@ -90,11 +89,8 @@ class ZimFileReader constructor(
val mainPage: String?
get() =
try {
if (jniKiwixReader.mainEntry.isRedirect)
jniKiwixReader.mainEntry.getItem(true).path
else
jniKiwixReader.mainEntry.path
} catch (entryNotFound: EntryNotFoundException) {
jniKiwixReader.mainEntry.getItem(true).path
} catch (ignore: Exception) {
null
}
val id: String get() = jniKiwixReader.uuid
Expand Down Expand Up @@ -143,10 +139,11 @@ class ZimFileReader constructor(
}

fun getPageUrlFrom(title: String): String? =
if (jniKiwixReader.hasEntryByTitle(title))
try {
jniKiwixReader.getEntryByTitle(title).path
else
} catch (ignore: Exception) {
null
}

fun getRandomArticleUrl(): String? = jniKiwixReader.randomEntry.path

Expand Down Expand Up @@ -180,12 +177,12 @@ class ZimFileReader constructor(

private fun getActualUrl(url: String, actualUrl: Boolean = false): String {
val actualPath = url.toUri().filePath.decodeUrl
var redirectPath = if (jniKiwixReader.hasEntryByPath(actualPath)) {
var redirectPath = try {
jniKiwixReader.getEntryByPath(actualPath)
.getItem(true)
.path
.replaceWithEncodedString
} else {
} catch (ignore: Exception) {
actualPath.replaceWithEncodedString
}
if (actualUrl && url.decodeUrl.contains("?")) {
Expand All @@ -208,9 +205,9 @@ class ZimFileReader constructor(
}

private fun loadAsset(uri: String): InputStream? {
val article = if (jniKiwixReader.hasEntryByPath(uri.filePath)) {
val article = try {
jniKiwixReader.getEntryByPath(uri.filePath).getItem(true)
} else {
} catch (ignore: Exception) {
null
}
val infoPair = article?.directAccessInformation
Expand Down Expand Up @@ -260,9 +257,9 @@ class ZimFileReader constructor(
}

private fun getItem(url: String): Item? =
if (jniKiwixReader.hasEntryByPath(getActualUrl(url))) {
try {
jniKiwixReader.getEntryByPath(getActualUrl(url)).getItem(true)
} else {
} catch (ignore: Exception) {
null
}

Expand Down

0 comments on commit e01c88e

Please sign in to comment.