Merge pull request #1772 from Xwite/master

正文图片加载失败显示失败图,并删除图片文件
pull/1776/head
kunfei 3 years ago committed by GitHub
commit 1ec7dd1981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  2. 40
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt
  3. BIN
      app/src/main/res/drawable/image_loading_error.jpg

@ -86,19 +86,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
?: throw NoStackTraceException("创建文档失败") ?: throw NoStackTraceException("创建文档失败")
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
context.contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs -> context.contentResolver.openOutputStream(bookDoc.uri, "wa")?.use { bookOs ->
getAllContents(scope, book) { text, srcList -> getAllContents(scope, book) { text ->
bookOs.write(text.toByteArray(Charset.forName(AppConfig.exportCharset))) bookOs.write(text.toByteArray(Charset.forName(AppConfig.exportCharset)))
stringBuilder.append(text) stringBuilder.append(text)
srcList?.forEach {
val vFile = BookHelp.getImage(book, it.third)
if (vFile.exists()) {
DocumentUtils.createFileIfNotExist(
doc,
"${it.second}-${MD5Utils.md5Encode16(it.third)}.jpg",
subDirs = arrayOf("${book.name}_${book.author}", "images", it.first)
)?.writeBytes(context, vFile.readBytes())
}
}
} }
} }
if (AppConfig.exportToWebDav) { if (AppConfig.exportToWebDav) {
@ -114,21 +104,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
val bookPath = FileUtils.getPath(file, filename) val bookPath = FileUtils.getPath(file, filename)
val bookFile = FileUtils.createFileWithReplace(bookPath) val bookFile = FileUtils.createFileWithReplace(bookPath)
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
getAllContents(scope, book) { text, srcList -> getAllContents(scope, book) { text ->
bookFile.appendText(text, Charset.forName(AppConfig.exportCharset)) bookFile.appendText(text, Charset.forName(AppConfig.exportCharset))
stringBuilder.append(text) stringBuilder.append(text)
srcList?.forEach {
val vFile = BookHelp.getImage(book, it.third)
if (vFile.exists()) {
FileUtils.createFileIfNotExist(
file,
"${book.name}_${book.author}",
"images",
it.first,
"${it.second}-${MD5Utils.md5Encode16(it.third)}.jpg"
).writeBytes(vFile.readBytes())
}
}
} }
if (AppConfig.exportToWebDav) { if (AppConfig.exportToWebDav) {
val byteArray = val byteArray =
@ -140,7 +118,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
private suspend fun getAllContents( private suspend fun getAllContents(
scope: CoroutineScope, scope: CoroutineScope,
book: Book, book: Book,
append: (text: String, srcList: ArrayList<Triple<String, Int, String>>?) -> Unit append: (text: String) -> Unit
) { ) {
val useReplace = AppConfig.exportUseReplace && book.getUseReplaceRule() val useReplace = AppConfig.exportUseReplace && book.getUseReplaceRule()
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
@ -152,7 +130,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
"\n" + HtmlFormatter.format(book.getDisplayIntro()) "\n" + HtmlFormatter.format(book.getDisplayIntro())
) )
}" }"
append(qy, null) append(qy)
appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter -> appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter ->
scope.ensureActive() scope.ensureActive()
upAdapterLiveData.postValue(book.bookUrl) upAdapterLiveData.postValue(book.bookUrl)
@ -168,17 +146,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
chineseConvert = false, chineseConvert = false,
reSegment = false reSegment = false
).joinToString("\n") ).joinToString("\n")
val srcList = arrayListOf<Triple<String, Int, String>>() append.invoke("\n\n$content1")
content?.split("\n")?.forEachIndexed { index, text ->
val matcher = AppPattern.imgPattern.matcher(text)
while (matcher.find()) {
matcher.group(1)?.let {
val src = NetworkUtils.getAbsoluteURL(chapter.url, it)
srcList.add(Triple(chapter.title, index, src))
}
}
}
append.invoke("\n\n$content1", srcList)
} }
} }
} }
@ -461,7 +429,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
while (matcher.find()) { while (matcher.find()) {
matcher.group(1)?.let { matcher.group(1)?.let {
val src = NetworkUtils.getAbsoluteURL(chapter.url, it) val src = NetworkUtils.getAbsoluteURL(chapter.url, it)
val originalHref = "${MD5Utils.md5Encode16(src)}${BookHelp.getImageSuffix(src)}" val originalHref = "${MD5Utils.md5Encode16(src)}.${BookHelp.getImageSuffix(src)}"
val href = "Images/${MD5Utils.md5Encode16(src)}.${BookHelp.getImageSuffix(src)}" val href = "Images/${MD5Utils.md5Encode16(src)}.${BookHelp.getImageSuffix(src)}"
val vFile = BookHelp.getImage(book, src) val vFile = BookHelp.getImage(book, src)
val fp = FileResourceProvider(vFile.parent) val fp = FileResourceProvider(vFile.parent)

@ -1,6 +1,10 @@
package io.legado.app.ui.book.read.page.provider package io.legado.app.ui.book.read.page.provider
import android.graphics.Bitmap import android.graphics.Bitmap
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
@ -8,9 +12,11 @@ import io.legado.app.help.glide.ImageLoader
import io.legado.app.model.localBook.EpubFile import io.legado.app.model.localBook.EpubFile
import io.legado.app.utils.BitmapUtils import io.legado.app.utils.BitmapUtils
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.R
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import splitties.init.appCtx import splitties.init.appCtx
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.File
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
object ImageProvider { object ImageProvider {
@ -37,10 +43,36 @@ object ImageProvider {
} }
} }
} }
return try { return ImageLoader.loadBitmap(appCtx, vFile.absolutePath)
ImageLoader.loadBitmap(appCtx, vFile.absolutePath).submit().get() .error(R.drawable.image_loading_error)
} catch (e: Exception) { .listener(glideListener)
null .submit()
.get()
}
private val glideListener by lazy {
object : RequestListener<Bitmap> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Bitmap>?,
isFirstResource: Boolean
): Boolean {
File(model as String).delete()
return false
}
override fun onResourceReady(
resource: Bitmap?,
model: Any?,
target: Target<Bitmap>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
return false
}
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Loading…
Cancel
Save