From aa0f7a0c70974d6929c1598ebd70404d6b678cb7 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Thu, 26 Jan 2023 14:02:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/book/BookHelp.kt | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/book/BookHelp.kt b/app/src/main/java/io/legado/app/help/book/BookHelp.kt index 9089e2329..4136fa779 100644 --- a/app/src/main/java/io/legado/app/help/book/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/book/BookHelp.kt @@ -1,5 +1,6 @@ package io.legado.app.help.book +import android.graphics.BitmapFactory import android.os.ParcelFileDescriptor import androidx.documentfile.provider.DocumentFile import io.legado.app.constant.AppLog @@ -16,6 +17,7 @@ import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO import org.apache.commons.text.similarity.JaccardSimilarity import splitties.init.appCtx +import java.io.ByteArrayInputStream import java.io.File import java.io.FileNotFoundException import java.io.FileOutputStream @@ -147,6 +149,9 @@ object BookHelp { ImageUtils.decode( src, bytes, isCover = false, bookSource, book )?.let { + if (!checkImage(bytes)) { + AppLog.put("图片 $src 下载错误,数据异常") + } FileUtils.createFileIfNotExist( downloadDir, cacheFolderName, @@ -157,7 +162,7 @@ object BookHelp { } } catch (e: Exception) { e.printStackTrace() - AppLog.put("${src}下载错误", e) + AppLog.put("图片 $src 下载错误", e) } finally { downloadImages.remove(src) } @@ -255,17 +260,41 @@ object BookHelp { if (!hasContent(book, bookChapter)) { return false } + var ret = true + val op = BitmapFactory.Options() + op.inJustDecodeBounds = true getContent(book, bookChapter)?.let { val matcher = AppPattern.imgPattern.matcher(it) while (matcher.find()) { - matcher.group(1)?.let { src -> - val image = getImage(book, src) - if (!image.exists()) { - return false - } + val src = matcher.group(1)!! + val image = getImage(book, src) + if (!image.exists()) { + ret = false + continue + } + if (SvgUtils.getSize(image.absolutePath) != null) { + continue + } + BitmapFactory.decodeFile(image.absolutePath, op) + if (op.outWidth < 1 && op.outHeight < 1) { + ret = false + image.delete() } } } + return ret + } + + private fun checkImage(bytes: ByteArray): Boolean { + if (SvgUtils.getSize(ByteArrayInputStream(bytes)) != null) { + return true + } + val op = BitmapFactory.Options() + op.inJustDecodeBounds = true + BitmapFactory.decodeByteArray(bytes, 0, bytes.size, op) + if (op.outWidth < 1 && op.outHeight < 1) { + return false + } return true }