mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
@@ -31,6 +31,7 @@ object BookHelp {
|
||||
private val downloadDir: File = appCtx.externalFiles
|
||||
private const val cacheFolderName = "book_cache"
|
||||
private const val cacheImageFolderName = "images"
|
||||
private const val cacheEpubFolderName = "epub"
|
||||
private val downloadImages = CopyOnWriteArraySet<String>()
|
||||
|
||||
fun clearCache() {
|
||||
@@ -55,15 +56,24 @@ object BookHelp {
|
||||
*/
|
||||
suspend fun clearInvalidCache() {
|
||||
withContext(IO) {
|
||||
val bookFolderNames = appDb.bookDao.all.map {
|
||||
it.getFolderName()
|
||||
val bookFolderNames = ArrayList<String>()
|
||||
val originNames = ArrayList<String>()
|
||||
appDb.bookDao.all.forEach {
|
||||
bookFolderNames.add(it.getFolderName())
|
||||
if (it.isEpub()) originNames.add(it.originName)
|
||||
}
|
||||
val file = downloadDir.getFile(cacheFolderName)
|
||||
file.listFiles()?.forEach { bookFile ->
|
||||
if (!bookFolderNames.contains(bookFile.name)) {
|
||||
FileUtils.delete(bookFile.absolutePath)
|
||||
downloadDir.getFile(cacheFolderName)
|
||||
.listFiles()?.forEach { bookFile ->
|
||||
if (!bookFolderNames.contains(bookFile.name)) {
|
||||
FileUtils.delete(bookFile.absolutePath)
|
||||
}
|
||||
}
|
||||
downloadDir.getFile(cacheEpubFolderName)
|
||||
.listFiles()?.forEach { epubFile ->
|
||||
if (!originNames.contains(epubFile.name)) {
|
||||
FileUtils.delete(epubFile.absolutePath)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +175,8 @@ object BookHelp {
|
||||
fun getEpubFile(book: Book): ZipFile {
|
||||
val uri = Uri.parse(book.bookUrl)
|
||||
if (uri.isContentScheme()) {
|
||||
val path = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName(), book.originName)
|
||||
FileUtils.createFolderIfNotExist(downloadDir, cacheEpubFolderName)
|
||||
val path = FileUtils.getPath(downloadDir, cacheEpubFolderName, book.originName)
|
||||
val file = File(path)
|
||||
val doc = DocumentFile.fromSingleUri(appCtx, uri)
|
||||
?: throw IOException("文件不存在")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.legado.app.model.localBook
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.text.TextUtils
|
||||
import io.legado.app.data.entities.Book
|
||||
import io.legado.app.data.entities.BookChapter
|
||||
@@ -85,13 +86,11 @@ class EpubFile(var book: Book) {
|
||||
if (!File(book.coverUrl!!).exists()) {
|
||||
/*部分书籍DRM处理后,封面获取异常,待优化*/
|
||||
it.coverImage?.inputStream?.use { input ->
|
||||
BitmapUtils.decodeBitmap(input)?.let { cover ->
|
||||
val out =
|
||||
FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!))
|
||||
cover.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
||||
out.flush()
|
||||
out.close()
|
||||
}
|
||||
val cover = BitmapFactory.decodeStream(input)
|
||||
val out = FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!))
|
||||
cover.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
||||
out.flush()
|
||||
out.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -262,8 +261,6 @@ class EpubFile(var book: Book) {
|
||||
}
|
||||
}
|
||||
}
|
||||
book.latestChapterTitle = chapterList.lastOrNull()?.title
|
||||
book.totalChapterNum = chapterList.size
|
||||
return chapterList
|
||||
}
|
||||
|
||||
|
||||
@@ -78,9 +78,12 @@ object LocalBook {
|
||||
if (chapters.isEmpty()) {
|
||||
throw TocEmptyException(appCtx.getString(R.string.chapter_list_empty))
|
||||
}
|
||||
val lh = LinkedHashSet(chapters)
|
||||
lh.forEachIndexed { index, bookChapter -> bookChapter.index = index }
|
||||
return ArrayList(lh)
|
||||
val list = ArrayList(LinkedHashSet(chapters))
|
||||
list.forEachIndexed { index, bookChapter -> bookChapter.index = index }
|
||||
book.latestChapterTitle = list.last().title
|
||||
book.totalChapterNum = list.size
|
||||
book.save()
|
||||
return list
|
||||
}
|
||||
|
||||
fun getContent(book: Book, chapter: BookChapter): String? {
|
||||
|
||||
@@ -77,9 +77,6 @@ class TextFile(private val book: Book) {
|
||||
bookChapter.bookUrl = book.bookUrl
|
||||
bookChapter.url = MD5Utils.md5Encode16(book.originName + index + bookChapter.title)
|
||||
}
|
||||
book.latestChapterTitle = toc.last().title
|
||||
book.totalChapterNum = toc.size
|
||||
book.save()
|
||||
return toc
|
||||
}
|
||||
|
||||
|
||||
@@ -112,8 +112,6 @@ class UmdFile(var book: Book) {
|
||||
DebugLog.d(javaClass.name, chapter.url)
|
||||
chapterList.add(chapter)
|
||||
}
|
||||
book.latestChapterTitle = chapterList.lastOrNull()?.title
|
||||
book.totalChapterNum = chapterList.size
|
||||
return chapterList
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import android.graphics.Color
|
||||
import com.google.android.renderscript.Toolkit
|
||||
import java.io.FileInputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import kotlin.math.*
|
||||
|
||||
|
||||
@@ -82,23 +81,6 @@ object BitmapUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/** 从path中获取Bitmap图片
|
||||
* @param path 图片路径
|
||||
* @return
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
fun decodeBitmap(inputStream: InputStream): Bitmap? {
|
||||
return inputStream.use {
|
||||
val opts = BitmapFactory.Options()
|
||||
opts.inJustDecodeBounds = true
|
||||
|
||||
BitmapFactory.decodeStream(inputStream, null, opts)
|
||||
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128)
|
||||
opts.inJustDecodeBounds = false
|
||||
BitmapFactory.decodeStream(inputStream, null, opts)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以最省内存的方式读取本地资源的图片
|
||||
* @param context 设备上下文
|
||||
|
||||
Reference in New Issue
Block a user