From da61462d792caecaa16f44f298e307f787e031bf Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 25 May 2024 17:17:30 +0300 Subject: [PATCH] Update parsers --- app/build.gradle | 2 +- .../core/network/CommonHeadersInterceptor.kt | 22 +++++++++++++++---- .../core/parser/MangaLoaderContextImpl.kt | 15 ++----------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ee04db19a..968972860 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -82,7 +82,7 @@ afterEvaluate { } dependencies { //noinspection GradleDependency - implementation('com.github.KotatsuApp:kotatsu-parsers:d218ad5a67') { + implementation('com.github.KotatsuApp:kotatsu-parsers:51da0b62c1') { exclude group: 'org.json', module: 'json' } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/network/CommonHeadersInterceptor.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/network/CommonHeadersInterceptor.kt index 0b209ce2f..26e4dc212 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/network/CommonHeadersInterceptor.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/network/CommonHeadersInterceptor.kt @@ -4,8 +4,10 @@ import android.util.Log import dagger.Lazy import okhttp3.Headers import okhttp3.Interceptor +import okhttp3.Interceptor.Chain import okhttp3.Request import okhttp3.Response +import okio.IOException import org.koitharu.kotatsu.BuildConfig import org.koitharu.kotatsu.core.parser.MangaLoaderContextImpl import org.koitharu.kotatsu.core.parser.MangaRepository @@ -13,6 +15,7 @@ import org.koitharu.kotatsu.core.parser.RemoteMangaRepository import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.util.mergeWith +import org.koitharu.kotatsu.parsers.util.runCatchingCancellable import java.net.IDN import javax.inject.Inject import javax.inject.Singleton @@ -23,7 +26,7 @@ class CommonHeadersInterceptor @Inject constructor( private val mangaLoaderContextLazy: Lazy, ) : Interceptor { - override fun intercept(chain: Interceptor.Chain): Response { + override fun intercept(chain: Chain): Response { val request = chain.request() val source = request.tag(MangaSource::class.java) val repository = if (source != null) { @@ -46,7 +49,7 @@ class CommonHeadersInterceptor @Inject constructor( headersBuilder.trySet(CommonHeaders.REFERER, "https://$idn/") } val newRequest = request.newBuilder().headers(headersBuilder.build()).build() - return repository?.intercept(ProxyChain(chain, newRequest)) ?: chain.proceed(newRequest) + return repository?.interceptSafe(ProxyChain(chain, newRequest)) ?: chain.proceed(newRequest) } private fun Headers.Builder.trySet(name: String, value: String) = try { @@ -55,10 +58,21 @@ class CommonHeadersInterceptor @Inject constructor( e.printStackTraceDebug() } + private fun Interceptor.interceptSafe(chain: Chain): Response = runCatchingCancellable { + intercept(chain) + }.getOrElse { e -> + if (e is IOException) { + throw e + } else { + // only IOException can be safely thrown from an Interceptor + throw IOException("Error in interceptor: ${e.message}", e) + } + } + private class ProxyChain( - private val delegate: Interceptor.Chain, + private val delegate: Chain, private val request: Request, - ) : Interceptor.Chain by delegate { + ) : Chain by delegate { override fun request(): Request = request } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaLoaderContextImpl.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaLoaderContextImpl.kt index fbea3a92c..a4fe4fa25 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaLoaderContextImpl.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaLoaderContextImpl.kt @@ -16,7 +16,6 @@ import okhttp3.OkHttpClient import okhttp3.Response import okhttp3.ResponseBody.Companion.asResponseBody import okio.Buffer -import okio.IOException import org.koitharu.kotatsu.core.network.MangaHttpClient import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar import org.koitharu.kotatsu.core.prefs.SourceSettings @@ -30,7 +29,6 @@ import org.koitharu.kotatsu.parsers.bitmap.Bitmap import org.koitharu.kotatsu.parsers.config.MangaSourceConfig import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.network.UserAgents -import org.koitharu.kotatsu.parsers.util.runCatchingCancellable import java.lang.ref.WeakReference import java.util.Locale import javax.inject.Inject @@ -77,10 +75,7 @@ class MangaLoaderContextImpl @Inject constructor( return LocaleListCompat.getAdjustedDefault().toList() } - override fun redrawImageResponse( - response: Response, - redraw: (image: Bitmap) -> Bitmap - ): Response = runCatchingCancellable { + override fun redrawImageResponse(response: Response, redraw: (image: Bitmap) -> Bitmap): Response { val image = response.requireBody().byteStream() val opts = BitmapFactory.Options() @@ -92,15 +87,9 @@ class MangaLoaderContextImpl @Inject constructor( result.compressTo(it.outputStream()) }.asResponseBody("image/jpeg".toMediaType()) - response.newBuilder() + return response.newBuilder() .body(body) .build() - }.getOrElse { error -> - if (error is IOException) { - throw error - } else { - throw IOException(error.message, error) - } } override fun createBitmap(width: Int, height: Int): Bitmap {