From 3a0cf21635d6af4e2a19ff04b8a6ab800ae79155 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 11 Jul 2020 11:30:14 +0800 Subject: [PATCH] =?UTF-8?q?epub=E5=B0=81=E9=9D=A2=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 5 ++-- .../io/legado/app/model/localBook/EPUBFile.kt | 25 +++++++++++++++++-- .../legado/app/model/localBook/LocalBook.kt | 9 ++++++- .../io/legado/app/utils/ContextExtensions.kt | 6 ++++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index d6d36bf9a..7a643b940 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -9,6 +9,7 @@ import io.legado.app.data.entities.ReplaceRule import io.legado.app.model.localBook.LocalBook import io.legado.app.utils.FileUtils import io.legado.app.utils.MD5Utils +import io.legado.app.utils.externalFilesDir import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -20,9 +21,7 @@ import kotlin.math.min object BookHelp { private const val cacheFolderName = "book_cache" - private val downloadDir: File = - App.INSTANCE.getExternalFilesDir(null) - ?: App.INSTANCE.cacheDir + private val downloadDir: File = App.INSTANCE.externalFilesDir private fun bookFolderName(book: Book): String { return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl) diff --git a/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt b/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt index 84876de18..d45973b92 100644 --- a/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt @@ -1,16 +1,18 @@ package io.legado.app.model.localBook +import android.graphics.Bitmap +import android.graphics.BitmapFactory import android.net.Uri import android.text.TextUtils import io.legado.app.App import io.legado.app.data.entities.BookChapter -import io.legado.app.utils.htmlFormat -import io.legado.app.utils.isContentPath +import io.legado.app.utils.* import nl.siegmann.epublib.domain.Book import nl.siegmann.epublib.domain.TOCReference import nl.siegmann.epublib.epub.EpubReader import org.jsoup.Jsoup import java.io.File +import java.io.FileOutputStream import java.io.IOException import java.nio.charset.Charset import java.util.* @@ -19,6 +21,7 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) { companion object { private var eFile: EPUBFile? = null + private val coverDir = App.INSTANCE.externalFilesDir @Synchronized private fun getEFile(book: io.legado.app.data.entities.Book): EPUBFile { @@ -53,7 +56,25 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) { File(book.bookUrl).inputStream() } epubBook = epubReader.readEpub(inputStream) + if (book.coverUrl.isNullOrEmpty()) { + book.coverUrl = FileUtils.getPath( + App.INSTANCE.externalFilesDir, + "${MD5Utils.md5Encode16(book.bookUrl)}.jpg", + "covers" + ) + } + if (!File(book.coverUrl!!).exists()) { + epubBook!!.coverImage?.inputStream?.let { + val cover = BitmapFactory.decodeStream(it) + val out = FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!)) + cover.compress(Bitmap.CompressFormat.JPEG, 90, out) + out.flush() + out.close() + it.close() + } + } } catch (e: Exception) { + e.printStackTrace() } } diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 7986f03ad..82df2b811 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -7,6 +7,8 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp import io.legado.app.utils.FileUtils +import io.legado.app.utils.MD5Utils +import io.legado.app.utils.externalFilesDir import io.legado.app.utils.isContentPath import java.io.File @@ -57,7 +59,12 @@ object LocalBook { bookUrl = path, name = name, author = author, - originName = fileName + originName = fileName, + coverUrl = FileUtils.getPath( + App.INSTANCE.externalFilesDir, + "${MD5Utils.md5Encode16(path)}.jpg", + "covers" + ) ) App.db.bookDao().insert(book) } diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 7a9d1e275..df9f457cd 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -20,6 +20,7 @@ import androidx.core.content.edit import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder import com.google.zxing.EncodeHintType import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel +import io.legado.app.App import io.legado.app.BuildConfig import io.legado.app.R import org.jetbrains.anko.defaultSharedPreferences @@ -157,7 +158,10 @@ val Context.sysBattery: Int val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) val batteryStatus = registerReceiver(null, iFilter) return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 -} + } + +val Context.externalFilesDir: File + get() = App.INSTANCE.getExternalFilesDir(null) ?: App.INSTANCE.filesDir fun Context.openUrl(url: String) { openUrl(Uri.parse(url))