perf(AnalyzeUrl):skip init block when mUrl is DataURL

pull/1786/head
Xwite 3 years ago
parent 69dfea0218
commit cfa98112ce
  1. 5
      app/src/main/java/io/legado/app/help/glide/ImageLoader.kt
  2. 6
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  3. 6
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -6,8 +6,8 @@ import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import com.bumptech.glide.RequestBuilder import com.bumptech.glide.RequestBuilder
import io.legado.app.constant.AppPattern.dataUriRegex
import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isDataUrl
import io.legado.app.utils.isContentScheme import io.legado.app.utils.isContentScheme
import java.io.File import java.io.File
@ -20,7 +20,7 @@ object ImageLoader {
fun load(context: Context, path: String?): RequestBuilder<Drawable> { fun load(context: Context, path: String?): RequestBuilder<Drawable> {
return when { return when {
path.isNullOrEmpty() -> GlideApp.with(context).load(path) 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.isAbsUrl() -> GlideApp.with(context).load(path)
path.isContentScheme() -> GlideApp.with(context).load(Uri.parse(path)) path.isContentScheme() -> GlideApp.with(context).load(Uri.parse(path))
else -> kotlin.runCatching { else -> kotlin.runCatching {
@ -34,6 +34,7 @@ object ImageLoader {
fun loadBitmap(context: Context, path: String?): RequestBuilder<Bitmap> { fun loadBitmap(context: Context, path: String?): RequestBuilder<Bitmap> {
return when { return when {
path.isNullOrEmpty() -> GlideApp.with(context).asBitmap().load(path) 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.isAbsUrl() -> GlideApp.with(context).asBitmap().load(path)
path.isContentScheme() -> GlideApp.with(context).asBitmap().load(Uri.parse(path)) path.isContentScheme() -> GlideApp.with(context).asBitmap().load(Uri.parse(path))
else -> kotlin.runCatching { else -> kotlin.runCatching {

@ -71,6 +71,7 @@ class AnalyzeUrl(
private var webJs: String? = null private var webJs: String? = null
init { init {
if (!mUrl.isDataUrl()) {
val urlMatcher = paramPattern.matcher(baseUrl) val urlMatcher = paramPattern.matcher(baseUrl)
if (urlMatcher.find()) baseUrl = baseUrl.substring(0, urlMatcher.start()) if (urlMatcher.find()) baseUrl = baseUrl.substring(0, urlMatcher.start())
(headerMapF ?: source?.getHeaderMap(true))?.let { (headerMapF ?: source?.getHeaderMap(true))?.let {
@ -82,6 +83,7 @@ class AnalyzeUrl(
} }
initUrl() initUrl()
} }
}
/** /**
* 处理url * 处理url
@ -457,10 +459,9 @@ class AnalyzeUrl(
* 访问网站,返回ByteArray * 访问网站,返回ByteArray
*/ */
suspend fun getByteArrayAwait(): ByteArray { suspend fun getByteArrayAwait(): ByteArray {
val concurrentRecord = fetchStart()
@Suppress("RegExpRedundantEscape") @Suppress("RegExpRedundantEscape")
val dataUriFindResult = dataUriRegex.find(urlNoQuery) val dataUriFindResult = dataUriRegex.find(urlNoQuery)
val concurrentRecord = fetchStart()
setCookie(source?.getKey())
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
if (dataUriFindResult != null) { if (dataUriFindResult != null) {
val dataUriBase64 = dataUriFindResult.groupValues[1] val dataUriBase64 = dataUriFindResult.groupValues[1]
@ -468,6 +469,7 @@ class AnalyzeUrl(
fetchEnd(concurrentRecord) fetchEnd(concurrentRecord)
return byteArray return byteArray
} else { } else {
setCookie(source?.getKey())
val byteArray = getProxyClient(proxy).newCallResponseBody(retry) { val byteArray = getProxyClient(proxy).newCallResponseBody(retry) {
addHeaders(headerMap) addHeaders(headerMap)
when (method) { when (method) {

@ -2,6 +2,7 @@
package io.legado.app.utils package io.legado.app.utils
import io.legado.app.constant.AppPattern.dataUriRegex
import android.icu.text.Collator import android.icu.text.Collator
import android.icu.util.ULocale import android.icu.util.ULocale
import android.net.Uri import android.net.Uri
@ -25,6 +26,11 @@ fun String?.isAbsUrl() =
it.startsWith("http://", true) || it.startsWith("https://", true) it.startsWith("http://", true) || it.startsWith("https://", true)
} ?: false } ?: false
fun String?.isDataUrl() =
this?.let {
dataUriRegex.matches(it)
} ?: false
fun String?.isJson(): Boolean = fun String?.isJson(): Boolean =
this?.run { this?.run {
val str = this.trim() val str = this.trim()

Loading…
Cancel
Save