feat:glideUrl支持添加options

pull/1782/head
Xwite 3 years ago
parent 12a69e1e52
commit 589cf05a2d
  1. 18
      app/src/main/java/io/legado/app/help/glide/ImageLoader.kt
  2. 5
      app/src/main/java/io/legado/app/help/glide/OkHttpModelLoader.kt
  3. 26
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -7,7 +7,6 @@ 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.constant.AppPattern.dataUriRegex
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isContentScheme import io.legado.app.utils.isContentScheme
import java.io.File import java.io.File
@ -21,19 +20,11 @@ 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 -> { dataUriRegex.find(path) != null -> GlideApp.with(context).load(path)
//glide内部已经实现dataUri解析
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 {
val file = File(path) GlideApp.with(context).load(File(path))
if (file.exists()) {
GlideApp.with(context).load(file)
} else {
GlideApp.with(context).load(path)
}
}.getOrElse { }.getOrElse {
GlideApp.with(context).load(path) GlideApp.with(context).load(path)
} }
@ -43,8 +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.isAbsUrl() -> GlideApp.with(context).asBitmap() path.isAbsUrl() -> GlideApp.with(context).asBitmap().load(path)
.load(AnalyzeUrl(path).getGlideUrl())
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 {
GlideApp.with(context).asBitmap().load(File(path)) GlideApp.with(context).asBitmap().load(File(path))
@ -57,7 +47,7 @@ object ImageLoader {
fun loadFile(context: Context, path: String?): RequestBuilder<File> { fun loadFile(context: Context, path: String?): RequestBuilder<File> {
return when { return when {
path.isNullOrEmpty() -> GlideApp.with(context).asFile().load(path) path.isNullOrEmpty() -> GlideApp.with(context).asFile().load(path)
path.isAbsUrl() -> GlideApp.with(context).asFile().load(AnalyzeUrl(path).getGlideUrl()) path.isAbsUrl() -> GlideApp.with(context).asFile().load(path)
path.isContentScheme() -> GlideApp.with(context).asFile().load(Uri.parse(path)) path.isContentScheme() -> GlideApp.with(context).asFile().load(Uri.parse(path))
else -> kotlin.runCatching { else -> kotlin.runCatching {
GlideApp.with(context).asFile().load(File(path)) GlideApp.with(context).asFile().load(File(path))

@ -4,6 +4,8 @@ import com.bumptech.glide.load.Option
import com.bumptech.glide.load.Options import com.bumptech.glide.load.Options
import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.ModelLoader import com.bumptech.glide.load.model.ModelLoader
import io.legado.app.model.analyzeRule.AnalyzeUrl
import java.io.InputStream import java.io.InputStream
object OkHttpModelLoader : ModelLoader<GlideUrl?, InputStream?> { object OkHttpModelLoader : ModelLoader<GlideUrl?, InputStream?> {
@ -17,7 +19,8 @@ object OkHttpModelLoader : ModelLoader<GlideUrl?, InputStream?> {
height: Int, height: Int,
options: Options options: Options
): ModelLoader.LoadData<InputStream?> { ): ModelLoader.LoadData<InputStream?> {
return ModelLoader.LoadData(model, OkHttpStreamFetcher(model, options)) val modelWithHeader = AnalyzeUrl(model.toString()).getGlideUrl()
return ModelLoader.LoadData(modelWithHeader, OkHttpStreamFetcher(modelWithHeader, options))
} }
override fun handles(model: GlideUrl): Boolean { override fun handles(model: GlideUrl): Boolean {

@ -518,22 +518,28 @@ class AnalyzeUrl(
} }
} }
/**
*设置cookie urlOption的优先级大于书源保存的cookie
*@param tag 书源url 缺省为传入的url
*/
private fun setCookie(tag: String?) { private fun setCookie(tag: String?) {
if (tag != null) { val cookie = CookieStore.getCookie(tag ?: url)
val cookie = CookieStore.getCookie(tag) if (cookie.isNotEmpty()) {
if (cookie.isNotEmpty()) { val cookieMap = CookieStore.cookieToMap(cookie)
val cookieMap = CookieStore.cookieToMap(cookie) val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "")
val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "") cookieMap.putAll(customCookieMap)
cookieMap.putAll(customCookieMap) val newCookie = CookieStore.mapToCookie(cookieMap)
val newCookie = CookieStore.mapToCookie(cookieMap) newCookie?.let {
newCookie?.let { headerMap.put("Cookie", it)
headerMap.put("Cookie", it)
}
} }
} }
} }
/**
*获取处理过阅读定义的urlOption和cookie的GlideUrl
*/
fun getGlideUrl(): GlideUrl { fun getGlideUrl(): GlideUrl {
setCookie(source?.getKey())
val headers = LazyHeaders.Builder() val headers = LazyHeaders.Builder()
headerMap.forEach { (key, value) -> headerMap.forEach { (key, value) ->
headers.addHeader(key, value) headers.addHeader(key, value)

Loading…
Cancel
Save