Răsfoiți Sursa

Merge pull request #2237 from Xwite/master

修复epub封面和加载
kunfei 4 ani în urmă
părinte
comite
e850b43a41

+ 19 - 8
app/src/main/java/io/legado/app/help/BookHelp.kt

@@ -31,6 +31,7 @@ object BookHelp {
     private val downloadDir: File = appCtx.externalFiles
     private val downloadDir: File = appCtx.externalFiles
     private const val cacheFolderName = "book_cache"
     private const val cacheFolderName = "book_cache"
     private const val cacheImageFolderName = "images"
     private const val cacheImageFolderName = "images"
+    private const val cacheEpubFolderName = "epub"
     private val downloadImages = CopyOnWriteArraySet<String>()
     private val downloadImages = CopyOnWriteArraySet<String>()
 
 
     fun clearCache() {
     fun clearCache() {
@@ -55,15 +56,24 @@ object BookHelp {
      */
      */
     suspend fun clearInvalidCache() {
     suspend fun clearInvalidCache() {
         withContext(IO) {
         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 {
     fun getEpubFile(book: Book): ZipFile {
         val uri = Uri.parse(book.bookUrl)
         val uri = Uri.parse(book.bookUrl)
         if (uri.isContentScheme()) {
         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 file = File(path)
             val doc = DocumentFile.fromSingleUri(appCtx, uri)
             val doc = DocumentFile.fromSingleUri(appCtx, uri)
                 ?: throw IOException("文件不存在")
                 ?: throw IOException("文件不存在")

+ 6 - 9
app/src/main/java/io/legado/app/model/localBook/EpubFile.kt

@@ -1,6 +1,7 @@
 package io.legado.app.model.localBook
 package io.legado.app.model.localBook
 
 
 import android.graphics.Bitmap
 import android.graphics.Bitmap
+import android.graphics.BitmapFactory
 import android.text.TextUtils
 import android.text.TextUtils
 import io.legado.app.data.entities.Book
 import io.legado.app.data.entities.Book
 import io.legado.app.data.entities.BookChapter
 import io.legado.app.data.entities.BookChapter
@@ -85,13 +86,11 @@ class EpubFile(var book: Book) {
                 if (!File(book.coverUrl!!).exists()) {
                 if (!File(book.coverUrl!!).exists()) {
                     /*部分书籍DRM处理后,封面获取异常,待优化*/
                     /*部分书籍DRM处理后,封面获取异常,待优化*/
                     it.coverImage?.inputStream?.use { input ->
                     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
         return chapterList
     }
     }
 
 

+ 6 - 3
app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@@ -78,9 +78,12 @@ object LocalBook {
         if (chapters.isEmpty()) {
         if (chapters.isEmpty()) {
             throw TocEmptyException(appCtx.getString(R.string.chapter_list_empty))
             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? {
     fun getContent(book: Book, chapter: BookChapter): String? {

+ 0 - 3
app/src/main/java/io/legado/app/model/localBook/TextFile.kt

@@ -77,9 +77,6 @@ class TextFile(private val book: Book) {
             bookChapter.bookUrl = book.bookUrl
             bookChapter.bookUrl = book.bookUrl
             bookChapter.url = MD5Utils.md5Encode16(book.originName + index + bookChapter.title)
             bookChapter.url = MD5Utils.md5Encode16(book.originName + index + bookChapter.title)
         }
         }
-        book.latestChapterTitle = toc.last().title
-        book.totalChapterNum = toc.size
-        book.save()
         return toc
         return toc
     }
     }
 
 

+ 0 - 2
app/src/main/java/io/legado/app/model/localBook/UmdFile.kt

@@ -112,8 +112,6 @@ class UmdFile(var book: Book) {
             DebugLog.d(javaClass.name, chapter.url)
             DebugLog.d(javaClass.name, chapter.url)
             chapterList.add(chapter)
             chapterList.add(chapter)
         }
         }
-        book.latestChapterTitle = chapterList.lastOrNull()?.title
-        book.totalChapterNum = chapterList.size
         return chapterList
         return chapterList
     }
     }
 
 

+ 0 - 18
app/src/main/java/io/legado/app/utils/BitmapUtils.kt

@@ -10,7 +10,6 @@ import android.graphics.Color
 import com.google.android.renderscript.Toolkit
 import com.google.android.renderscript.Toolkit
 import java.io.FileInputStream
 import java.io.FileInputStream
 import java.io.IOException
 import java.io.IOException
-import java.io.InputStream
 import kotlin.math.*
 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 设备上下文
      * @param context 设备上下文