更改图片缓存位置

pull/326/head
AndyBernie 4 years ago
parent bad4afb790
commit c2ed976bec
  1. 31
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 15
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt

@ -25,11 +25,12 @@ import kotlin.math.min
object BookHelp { object BookHelp {
private const val cacheFolderName = "book_cache" private const val cacheFolderName = "book_cache"
private const val cacheImageFolderName = "images"
private val downloadDir: File = App.INSTANCE.externalFilesDir private val downloadDir: File = App.INSTANCE.externalFilesDir
private val srcPattern = private val srcPattern =
Pattern.compile("<img .*?src.*?=.*?\"(.*?(?:,\\{.*\\})?)\".*?>", Pattern.CASE_INSENSITIVE) Pattern.compile("<img .*?src.*?=.*?\"(.*?(?:,\\{.*\\})?)\".*?>", Pattern.CASE_INSENSITIVE)
private fun bookFolderName(book: Book): String { fun bookFolderName(book: Book): String {
return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl) return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl)
} }
@ -81,20 +82,32 @@ object BookHelp {
var src = matcher.group(1) var src = matcher.group(1)
src = NetworkUtils.getAbsoluteURL(bookChapter.url, src) src = NetworkUtils.getAbsoluteURL(bookChapter.url, src)
src?.let { src?.let {
val analyzeUrl = AnalyzeUrl(src, null, null, null, null) saveImage(book, src)
analyzeUrl.getImageBytes(book.origin)?.let {
FileUtils.createFileIfNotExist(
downloadDir,
"${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}",
"images", book.name
).writeBytes(it)
}
} }
} }
} }
postEvent(EventBus.SAVE_CONTENT, bookChapter) postEvent(EventBus.SAVE_CONTENT, bookChapter)
} }
fun saveImage(book: Book, src:String) {
val analyzeUrl = AnalyzeUrl(src, null, null, null, null)
analyzeUrl.getImageBytes(book.origin)?.let {
FileUtils.createFileIfNotExist(
downloadDir,
"${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName)
).writeBytes(it)
}
}
fun getImage(book:Book, src:String): File {
return FileUtils.getFile(
downloadDir,
"${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName)
)
}
fun getChapterFiles(book: Book): List<String> { fun getChapterFiles(book: Book): List<String> {
val fileNameList = arrayListOf<String>() val fileNameList = arrayListOf<String>()
FileUtils.createFolderIfNotExist( FileUtils.createFolderIfNotExist(

@ -3,15 +3,13 @@ package io.legado.app.ui.book.read.page.provider
import android.graphics.Bitmap import android.graphics.Bitmap
import io.legado.app.App import io.legado.app.App
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.help.http.HttpHelper import io.legado.app.help.BookHelp
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.utils.MD5Utils
import io.legado.app.utils.externalFilesDir import io.legado.app.utils.externalFilesDir
import java.io.FileOutputStream import java.io.FileOutputStream
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import io.legado.app.model.analyzeRule.AnalyzeUrl
object ImageProvider { object ImageProvider {
@ -36,11 +34,7 @@ object ImageProvider {
getCache(chapterIndex, src)?.let { getCache(chapterIndex, src)?.let {
return it return it
} }
val vFile = FileUtils.getFile( val vFile = BookHelp.getImage(book, src)
App.INSTANCE.externalFilesDir,
"${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}",
"images", book.name
)
if (!vFile.exists()) { if (!vFile.exists()) {
if (book.isEpub()) { if (book.isEpub()) {
EPUBFile.getImage(book, src).use { EPUBFile.getImage(book, src).use {
@ -50,10 +44,7 @@ object ImageProvider {
out.close() out.close()
} }
} else if (!onUi) { } else if (!onUi) {
val analyzeUrl = AnalyzeUrl(src, null, null, null, null) BookHelp.saveImage(book, src)
analyzeUrl.getImageBytes(book.origin)?.let {
FileUtils.createFileIfNotExist(vFile.absolutePath).writeBytes(it)
}
} }
} }
return try { return try {

Loading…
Cancel
Save