Avoid hard crash if cached image file was already deleted

Closes #9720
This commit is contained in:
arkon 2024-01-06 18:15:17 -05:00
parent 36f307e3bb
commit 3ea026e311
2 changed files with 68 additions and 50 deletions

View File

@ -17,10 +17,12 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.supervisorScope
import logcat.LogPriority
import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchIO
import tachiyomi.core.util.lang.withIOContext import tachiyomi.core.util.lang.withIOContext
import tachiyomi.core.util.lang.withUIContext import tachiyomi.core.util.lang.withUIContext
import tachiyomi.core.util.system.ImageUtil import tachiyomi.core.util.system.ImageUtil
import tachiyomi.core.util.system.logcat
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.InputStream import java.io.InputStream
@ -136,6 +138,7 @@ class PagerPageHolder(
val streamFn = page.stream ?: return val streamFn = page.stream ?: return
try {
val (bais, isAnimated, background) = withIOContext { val (bais, isAnimated, background) = withIOContext {
streamFn().buffered(16).use { stream -> streamFn().buffered(16).use { stream ->
process(item, stream).use { itemStream -> process(item, stream).use { itemStream ->
@ -171,6 +174,12 @@ class PagerPageHolder(
} }
removeErrorLayout() removeErrorLayout()
} }
} catch (e: Throwable) {
logcat(LogPriority.ERROR, e)
withUIContext {
setError()
}
}
} }
private fun process(page: ReaderPage, imageStream: BufferedInputStream): InputStream { private fun process(page: ReaderPage, imageStream: BufferedInputStream): InputStream {

View File

@ -23,10 +23,12 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import logcat.LogPriority
import tachiyomi.core.util.lang.launchIO import tachiyomi.core.util.lang.launchIO
import tachiyomi.core.util.lang.withIOContext import tachiyomi.core.util.lang.withIOContext
import tachiyomi.core.util.lang.withUIContext import tachiyomi.core.util.lang.withUIContext
import tachiyomi.core.util.system.ImageUtil import tachiyomi.core.util.system.ImageUtil
import tachiyomi.core.util.system.logcat
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.InputStream import java.io.InputStream
@ -184,6 +186,7 @@ class WebtoonPageHolder(
val streamFn = page?.stream ?: return val streamFn = page?.stream ?: return
try {
val (openStream, isAnimated) = withIOContext { val (openStream, isAnimated) = withIOContext {
val stream = streamFn().buffered(16) val stream = streamFn().buffered(16)
val openStream = process(stream) val openStream = process(stream)
@ -207,6 +210,12 @@ class WebtoonPageHolder(
suspendCancellableCoroutine<Nothing> { continuation -> suspendCancellableCoroutine<Nothing> { continuation ->
continuation.invokeOnCancellation { openStream.close() } continuation.invokeOnCancellation { openStream.close() }
} }
} catch (e: Throwable) {
logcat(LogPriority.ERROR, e)
withUIContext {
setError()
}
}
} }
private fun process(imageStream: BufferedInputStream): InputStream { private fun process(imageStream: BufferedInputStream): InputStream {