mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 10:46:53 -04:00
Merge pull request #4158 from kiwix/Fixes#4157
Fixed: `Item.getSize` sometimes throwing exception and application crashses.
This commit is contained in:
commit
061249ad5f
@ -199,7 +199,7 @@ class ZimFileReader constructor(
|
|||||||
private fun loadContent(uri: String, extension: String): InputStream? {
|
private fun loadContent(uri: String, extension: String): InputStream? {
|
||||||
val item = getItem(uri)
|
val item = getItem(uri)
|
||||||
if (compressedExtensions.any { it != extension }) {
|
if (compressedExtensions.any { it != extension }) {
|
||||||
item?.size?.let {
|
item?.itemSize()?.let {
|
||||||
// Check if the item size exceeds 1 MB
|
// Check if the item size exceeds 1 MB
|
||||||
if (it / Kb > 1024) {
|
if (it / Kb > 1024) {
|
||||||
// Retrieve direct access information for the item
|
// Retrieve direct access information for the item
|
||||||
@ -293,7 +293,7 @@ class ZimFileReader constructor(
|
|||||||
file: File,
|
file: File,
|
||||||
infoPair: DirectAccessInfo
|
infoPair: DirectAccessInfo
|
||||||
): InputStream? =
|
): InputStream? =
|
||||||
item?.size?.let {
|
item?.itemSize()?.let {
|
||||||
AssetFileDescriptor(
|
AssetFileDescriptor(
|
||||||
infoPair.parcelFileDescriptor(file),
|
infoPair.parcelFileDescriptor(file),
|
||||||
infoPair.offset,
|
infoPair.offset,
|
||||||
@ -455,3 +455,14 @@ const val ILLUSTRATION_SIZE = 48
|
|||||||
// add content prefix to url since searched items return the url inside of zim without content prefix.
|
// add content prefix to url since searched items return the url inside of zim without content prefix.
|
||||||
val String.addContentPrefix: String
|
val String.addContentPrefix: String
|
||||||
get() = if (startsWith(CONTENT_PREFIX)) this else CONTENT_PREFIX + this
|
get() = if (startsWith(CONTENT_PREFIX)) this else CONTENT_PREFIX + this
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles any error thrown by this method. Developers should handle the flow if this method
|
||||||
|
* returns null. For more details, see: https://github.com/kiwix/kiwix-android/issues/4157
|
||||||
|
*/
|
||||||
|
fun Item.itemSize(): Long? = try {
|
||||||
|
size
|
||||||
|
} catch (ignore: Exception) {
|
||||||
|
Log.e(TAG, "Could not retrieve the item size.\n Original exception: $ignore")
|
||||||
|
null
|
||||||
|
}
|
||||||
|
@ -58,7 +58,7 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F
|
|||||||
val headers = mutableMapOf("Accept-Ranges" to "bytes")
|
val headers = mutableMapOf("Accept-Ranges" to "bytes")
|
||||||
if ("Range" in requestHeaders.keys) {
|
if ("Range" in requestHeaders.keys) {
|
||||||
setStatusCodeAndReasonPhrase(HttpURLConnection.HTTP_PARTIAL, "Partial Content")
|
setStatusCodeAndReasonPhrase(HttpURLConnection.HTTP_PARTIAL, "Partial Content")
|
||||||
val fullSize = zimFileReader?.getItem(url)?.size ?: 0L
|
val fullSize = zimFileReader?.getItem(url)?.itemSize() ?: 0L
|
||||||
val lastByte = fullSize - 1
|
val lastByte = fullSize - 1
|
||||||
val byteRanges = requestHeaders.getValue("Range").substringAfter("=").split("-")
|
val byteRanges = requestHeaders.getValue("Range").substringAfter("=").split("-")
|
||||||
headers["Content-Range"] = "bytes ${byteRanges[0]}-$lastByte/$fullSize"
|
headers["Content-Range"] = "bytes ${byteRanges[0]}-$lastByte/$fullSize"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user