Преглед изворни кода

Merge pull request #1772 from Xwite/master

正文图片加载失败显示失败图,并删除图片文件
kunfei пре 4 година
родитељ
комит
b9d9c76c01

+ 6 - 38
app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt

@@ -86,19 +86,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
             ?: throw NoStackTraceException("创建文档失败")
         val stringBuilder = StringBuilder()
         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)))
                 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) {
@@ -114,21 +104,9 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
         val bookPath = FileUtils.getPath(file, filename)
         val bookFile = FileUtils.createFileWithReplace(bookPath)
         val stringBuilder = StringBuilder()
-        getAllContents(scope, book) { text, srcList ->
+        getAllContents(scope, book) { text ->
             bookFile.appendText(text, Charset.forName(AppConfig.exportCharset))
             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) {
             val byteArray =
@@ -140,7 +118,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
     private suspend fun getAllContents(
         scope: CoroutineScope,
         book: Book,
-        append: (text: String, srcList: ArrayList<Triple<String, Int, String>>?) -> Unit
+        append: (text: String) -> Unit
     ) {
         val useReplace = AppConfig.exportUseReplace && book.getUseReplaceRule()
         val contentProcessor = ContentProcessor.get(book.name, book.origin)
@@ -152,7 +130,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
                 "\n" + HtmlFormatter.format(book.getDisplayIntro())
             )
         }"
-        append(qy, null)
+        append(qy)
         appDb.bookChapterDao.getChapterList(book.bookUrl).forEachIndexed { index, chapter ->
             scope.ensureActive()
             upAdapterLiveData.postValue(book.bookUrl)
@@ -168,17 +146,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
                         chineseConvert = false,
                         reSegment = false
                     ).joinToString("\n")
-                val srcList = arrayListOf<Triple<String, Int, String>>()
-                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)
+                append.invoke("\n\n$content1")
             }
         }
     }
@@ -461,7 +429,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
             while (matcher.find()) {
                 matcher.group(1)?.let {
                     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 vFile = BookHelp.getImage(book, src)
                     val fp = FileResourceProvider(vFile.parent)

+ 36 - 4
app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt

@@ -1,6 +1,10 @@
 package io.legado.app.ui.book.read.page.provider
 
 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.BookSource
 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.utils.BitmapUtils
 import io.legado.app.utils.FileUtils
+import io.legado.app.R
 import kotlinx.coroutines.runBlocking
 import splitties.init.appCtx
 import java.io.FileOutputStream
+import java.io.File
 import java.util.concurrent.ConcurrentHashMap
 
 object ImageProvider {
@@ -37,10 +43,36 @@ object ImageProvider {
                 }
             }
         }
-        return try {
-            ImageLoader.loadBitmap(appCtx, vFile.absolutePath).submit().get()
-        } catch (e: Exception) {
-            null
+        return ImageLoader.loadBitmap(appCtx, vFile.absolutePath)
+            .error(R.drawable.image_loading_error)
+            .listener(glideListener)
+            .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
+            }
+
         }
     }
 

BIN
app/src/main/res/drawable/image_loading_error.jpg