From f8219e9900937998b80a82d4f988e5e6d54d03e9 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Mon, 21 Apr 2025 11:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/help/glide/OkHttpStreamFetcher.kt | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt b/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt index 8d49f188e..4499322c7 100644 --- a/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt +++ b/app/src/main/java/io/legado/app/help/glide/OkHttpStreamFetcher.kt @@ -10,6 +10,7 @@ import com.bumptech.glide.util.ContentLengthInputStream import com.script.rhino.runScriptWithContext import io.legado.app.data.entities.BaseSource import io.legado.app.exception.NoStackTraceException +import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.addHeaders import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClientManga @@ -18,7 +19,9 @@ import io.legado.app.model.ReadManga import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.ImageUtils import io.legado.app.utils.isWifiConnect -import kotlinx.coroutines.Job +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.SupervisorJob import okhttp3.Call import okhttp3.Request import okhttp3.Response @@ -39,7 +42,8 @@ class OkHttpStreamFetcher( private var callback: DataFetcher.DataCallback? = null private var source: BaseSource? = null private val manga = options.get(OkHttpModelLoader.mangaOption) == true - private val coroutineContext = Job() + private val coroutineContext = SupervisorJob() + private val coroutineScope = CoroutineScope(coroutineContext) private lateinit var analyzedUrl: GlideUrl @Volatile @@ -110,11 +114,20 @@ class OkHttpStreamFetcher( override fun onResponse(call: Call, response: Response) { responseBody = response.body - if (response.isSuccessful) { + if (!response.isSuccessful) { + if (!manga) { + failUrl.add(url.toStringUrl()) + } + callback?.onLoadFailed(HttpException(response.message, response.code)) + return + } + if (ImageUtils.skipDecode(source, !manga)) { + onStreamReady(responseBody!!.byteStream()) + return + } + Coroutine.async(coroutineScope, executeContext = IO) { val decodeResult = runScriptWithContext(coroutineContext) { - if (ImageUtils.skipDecode(source, !manga)) { - responseBody!!.byteStream() - } else if (manga) { + if (manga) { ImageUtils.decode( url.toString(), responseBody!!.bytes(), @@ -129,20 +142,23 @@ class OkHttpStreamFetcher( ) } } - if (decodeResult == null) { - callback?.onLoadFailed(NoStackTraceException("封面二次解密失败")) - } else { - val contentLength: Long = - if (decodeResult is ByteArrayInputStream) decodeResult.available().toLong() - else responseBody!!.contentLength() - stream = ContentLengthInputStream.obtain(decodeResult, contentLength) - callback?.onDataReady(stream) - } - } else { - if (!manga) { - failUrl.add(analyzedUrl.toStringUrl()) - } - callback?.onLoadFailed(HttpException(response.message, response.code)) + onStreamReady(decodeResult) } } + + private fun onStreamReady(inputStream: InputStream?) { + if (inputStream == null) { + if (!manga) { + failUrl.add(url.toStringUrl()) + } + callback?.onLoadFailed(NoStackTraceException("封面二次解密失败")) + } else { + val contentLength: Long = + if (inputStream is ByteArrayInputStream) inputStream.available().toLong() + else responseBody!!.contentLength() + stream = ContentLengthInputStream.obtain(inputStream, contentLength) + callback?.onDataReady(stream) + } + } + } \ No newline at end of file