mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package io.legado.app.help.glide
|
||||
|
||||
import com.bumptech.glide.Priority
|
||||
import com.bumptech.glide.load.DataSource
|
||||
import com.bumptech.glide.load.Options
|
||||
import com.bumptech.glide.load.data.DataFetcher
|
||||
import com.bumptech.glide.load.model.ModelLoader
|
||||
import com.bumptech.glide.load.model.ModelLoaderFactory
|
||||
import com.bumptech.glide.load.model.MultiModelLoaderFactory
|
||||
import com.bumptech.glide.signature.ObjectKey
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.model.ReadManga
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.utils.ImageUtils
|
||||
import java.io.InputStream
|
||||
|
||||
class LegadoDataUrlLoader : ModelLoader<String, InputStream> {
|
||||
|
||||
override fun buildLoadData(
|
||||
model: String,
|
||||
width: Int,
|
||||
height: Int,
|
||||
options: Options
|
||||
): ModelLoader.LoadData<InputStream>? {
|
||||
if (options.get(OkHttpModelLoader.mangaOption) == false) {
|
||||
return null
|
||||
}
|
||||
return ModelLoader.LoadData(ObjectKey(model), LegadoDataUrlFetcher(model))
|
||||
}
|
||||
|
||||
override fun handles(model: String): Boolean {
|
||||
return model.startsWith("data:")
|
||||
}
|
||||
|
||||
class LegadoDataUrlFetcher(private val model: String) : DataFetcher<InputStream> {
|
||||
override fun loadData(
|
||||
priority: Priority,
|
||||
callback: DataFetcher.DataCallback<in InputStream>
|
||||
) {
|
||||
try {
|
||||
val bytes = AnalyzeUrl(model, source = ReadManga.bookSource).getByteArray()
|
||||
val decoded = ImageUtils.decode(
|
||||
model, bytes, isCover = false, ReadManga.bookSource, ReadManga.book
|
||||
)?.inputStream()
|
||||
if (decoded == null) {
|
||||
throw NoStackTraceException("漫画图片解密失败")
|
||||
}
|
||||
callback.onDataReady(decoded)
|
||||
} catch (e: Exception) {
|
||||
callback.onLoadFailed(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun cleanup() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
override fun cancel() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
override fun getDataClass(): Class<InputStream> {
|
||||
return InputStream::class.java
|
||||
}
|
||||
|
||||
override fun getDataSource(): DataSource {
|
||||
return DataSource.LOCAL
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Factory : ModelLoaderFactory<String, InputStream> {
|
||||
override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<String, InputStream> {
|
||||
return LegadoDataUrlLoader()
|
||||
}
|
||||
|
||||
override fun teardown() {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,11 @@ class LegadoGlideModule : AppGlideModule() {
|
||||
InputStream::class.java,
|
||||
OkHttpModeLoaderFactory
|
||||
)
|
||||
registry.prepend(
|
||||
String::class.java,
|
||||
InputStream::class.java,
|
||||
LegadoDataUrlLoader.Factory()
|
||||
)
|
||||
}
|
||||
|
||||
override fun applyOptions(context: Context, builder: GlideBuilder) {
|
||||
|
||||
@@ -113,9 +113,7 @@ class OkHttpStreamFetcher(
|
||||
isCover = false,
|
||||
source,
|
||||
ReadManga.book
|
||||
)?.let {
|
||||
ByteArrayInputStream(it)
|
||||
}
|
||||
)?.inputStream()
|
||||
} else {
|
||||
ImageUtils.decode(
|
||||
url.toStringUrl(), responseBody!!.byteStream(),
|
||||
|
||||
Reference in New Issue
Block a user