From cfa98112cecc5cd513bc292f9093ab6fa9a71cfa Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Thu, 14 Apr 2022 13:02:08 +0800 Subject: [PATCH] perf(AnalyzeUrl):skip init block when mUrl is DataURL --- .../io/legado/app/help/glide/ImageLoader.kt | 5 +++-- .../app/model/analyzeRule/AnalyzeUrl.kt | 22 ++++++++++--------- .../io/legado/app/utils/StringExtensions.kt | 6 +++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt b/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt index 4b0616d95..4bfc46d30 100644 --- a/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt +++ b/app/src/main/java/io/legado/app/help/glide/ImageLoader.kt @@ -6,8 +6,8 @@ import android.graphics.drawable.Drawable import android.net.Uri import androidx.annotation.DrawableRes import com.bumptech.glide.RequestBuilder -import io.legado.app.constant.AppPattern.dataUriRegex import io.legado.app.utils.isAbsUrl +import io.legado.app.utils.isDataUrl import io.legado.app.utils.isContentScheme import java.io.File @@ -20,7 +20,7 @@ object ImageLoader { fun load(context: Context, path: String?): RequestBuilder { return when { path.isNullOrEmpty() -> GlideApp.with(context).load(path) - dataUriRegex.find(path) != null -> GlideApp.with(context).load(path) + path.isDataUrl() -> GlideApp.with(context).load(path) path.isAbsUrl() -> GlideApp.with(context).load(path) path.isContentScheme() -> GlideApp.with(context).load(Uri.parse(path)) else -> kotlin.runCatching { @@ -34,6 +34,7 @@ object ImageLoader { fun loadBitmap(context: Context, path: String?): RequestBuilder { return when { path.isNullOrEmpty() -> GlideApp.with(context).asBitmap().load(path) + path.isDataUrl() -> GlideApp.with(context).asBitmap().load(path) path.isAbsUrl() -> GlideApp.with(context).asBitmap().load(path) path.isContentScheme() -> GlideApp.with(context).asBitmap().load(Uri.parse(path)) else -> kotlin.runCatching { diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index dbf8f9b98..20aeae31a 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -71,16 +71,18 @@ class AnalyzeUrl( private var webJs: String? = null init { - val urlMatcher = paramPattern.matcher(baseUrl) - if (urlMatcher.find()) baseUrl = baseUrl.substring(0, urlMatcher.start()) - (headerMapF ?: source?.getHeaderMap(true))?.let { - headerMap.putAll(it) - if (it.containsKey("proxy")) { - proxy = it["proxy"] - headerMap.remove("proxy") + if (!mUrl.isDataUrl()) { + val urlMatcher = paramPattern.matcher(baseUrl) + if (urlMatcher.find()) baseUrl = baseUrl.substring(0, urlMatcher.start()) + (headerMapF ?: source?.getHeaderMap(true))?.let { + headerMap.putAll(it) + if (it.containsKey("proxy")) { + proxy = it["proxy"] + headerMap.remove("proxy") + } } + initUrl() } - initUrl() } /** @@ -457,10 +459,9 @@ class AnalyzeUrl( * 访问网站,返回ByteArray */ suspend fun getByteArrayAwait(): ByteArray { + val concurrentRecord = fetchStart() @Suppress("RegExpRedundantEscape") val dataUriFindResult = dataUriRegex.find(urlNoQuery) - val concurrentRecord = fetchStart() - setCookie(source?.getKey()) @Suppress("BlockingMethodInNonBlockingContext") if (dataUriFindResult != null) { val dataUriBase64 = dataUriFindResult.groupValues[1] @@ -468,6 +469,7 @@ class AnalyzeUrl( fetchEnd(concurrentRecord) return byteArray } else { + setCookie(source?.getKey()) val byteArray = getProxyClient(proxy).newCallResponseBody(retry) { addHeaders(headerMap) when (method) { diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index f8af7ee9a..a5a6c3f39 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -2,6 +2,7 @@ package io.legado.app.utils +import io.legado.app.constant.AppPattern.dataUriRegex import android.icu.text.Collator import android.icu.util.ULocale import android.net.Uri @@ -25,6 +26,11 @@ fun String?.isAbsUrl() = it.startsWith("http://", true) || it.startsWith("https://", true) } ?: false +fun String?.isDataUrl() = + this?.let { + dataUriRegex.matches(it) + } ?: false + fun String?.isJson(): Boolean = this?.run { val str = this.trim()