pull/2731/head
Horis 2 years ago
parent 06fce75725
commit aa0f7a0c70
  1. 35
      app/src/main/java/io/legado/app/help/book/BookHelp.kt

@ -1,5 +1,6 @@
package io.legado.app.help.book package io.legado.app.help.book
import android.graphics.BitmapFactory
import android.os.ParcelFileDescriptor import android.os.ParcelFileDescriptor
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
@ -16,6 +17,7 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import org.apache.commons.text.similarity.JaccardSimilarity import org.apache.commons.text.similarity.JaccardSimilarity
import splitties.init.appCtx import splitties.init.appCtx
import java.io.ByteArrayInputStream
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.FileOutputStream import java.io.FileOutputStream
@ -147,6 +149,9 @@ object BookHelp {
ImageUtils.decode( ImageUtils.decode(
src, bytes, isCover = false, bookSource, book src, bytes, isCover = false, bookSource, book
)?.let { )?.let {
if (!checkImage(bytes)) {
AppLog.put("图片 $src 下载错误,数据异常")
}
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
cacheFolderName, cacheFolderName,
@ -157,7 +162,7 @@ object BookHelp {
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
AppLog.put("${src}下载错误", e) AppLog.put("图片 $src 下载错误", e)
} finally { } finally {
downloadImages.remove(src) downloadImages.remove(src)
} }
@ -255,16 +260,40 @@ object BookHelp {
if (!hasContent(book, bookChapter)) { if (!hasContent(book, bookChapter)) {
return false return false
} }
var ret = true
val op = BitmapFactory.Options()
op.inJustDecodeBounds = true
getContent(book, bookChapter)?.let { getContent(book, bookChapter)?.let {
val matcher = AppPattern.imgPattern.matcher(it) val matcher = AppPattern.imgPattern.matcher(it)
while (matcher.find()) { while (matcher.find()) {
matcher.group(1)?.let { src -> val src = matcher.group(1)!!
val image = getImage(book, src) val image = getImage(book, src)
if (!image.exists()) { if (!image.exists()) {
return false 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 return true
} }

Loading…
Cancel
Save