mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
31ee3400b4
commit
cdda7c3191
@ -1,5 +1,6 @@
|
||||
package io.legado.app.help
|
||||
|
||||
import android.net.Uri
|
||||
import io.legado.app.constant.AppPattern
|
||||
import io.legado.app.constant.EventBus
|
||||
import io.legado.app.data.appDb
|
||||
@ -21,9 +22,9 @@ import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
object BookHelp {
|
||||
const val cacheFolderName = "book_cache"
|
||||
private const val cacheFolderName = "book_cache"
|
||||
private const val cacheImageFolderName = "images"
|
||||
val downloadDir: File = appCtx.externalFilesDir
|
||||
private val downloadDir: File = appCtx.externalFilesDir
|
||||
private val downloadImages = CopyOnWriteArraySet<String>()
|
||||
|
||||
fun clearCache() {
|
||||
@ -55,6 +56,28 @@ object BookHelp {
|
||||
}
|
||||
}
|
||||
|
||||
fun getEpubFile(book: Book,): File {
|
||||
val file = FileUtils.getFile(
|
||||
downloadDir,
|
||||
cacheFolderName,
|
||||
book.getFolderName(),
|
||||
"index.epubx"
|
||||
)
|
||||
if(!file.exists()){
|
||||
val input = if (book.bookUrl.isContentScheme()) {
|
||||
val uri = Uri.parse(book.bookUrl)
|
||||
appCtx.contentResolver.openInputStream(uri)
|
||||
} else {
|
||||
File(book.bookUrl).inputStream()
|
||||
}
|
||||
if (input != null) {
|
||||
FileUtils.writeInputStream(file, input)
|
||||
}
|
||||
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
||||
suspend fun saveContent(book: Book, bookChapter: BookChapter, content: String) {
|
||||
if (content.isEmpty()) return
|
||||
//保存文本
|
||||
|
@ -2,12 +2,14 @@ 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.data.entities.Book
|
||||
import io.legado.app.data.entities.BookChapter
|
||||
import io.legado.app.help.BookHelp
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.FileUtils
|
||||
import io.legado.app.utils.HtmlFormatter
|
||||
import io.legado.app.utils.MD5Utils
|
||||
import io.legado.app.utils.externalFilesDir
|
||||
import me.ag2s.epublib.domain.EpubBook
|
||||
import me.ag2s.epublib.epub.EpubReader
|
||||
import org.jsoup.Jsoup
|
||||
@ -27,6 +29,7 @@ class EpubFile(var book: Book) {
|
||||
|
||||
@Synchronized
|
||||
private fun getEFile(book: Book): EpubFile {
|
||||
BookHelp.getEpubFile(book)
|
||||
if (eFile == null || eFile?.book?.bookUrl != book.bookUrl) {
|
||||
eFile = EpubFile(book)
|
||||
return eFile!!
|
||||
@ -98,28 +101,8 @@ class EpubFile(var book: Book) {
|
||||
/*重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误*/
|
||||
private fun readEpub(): EpubBook? {
|
||||
try {
|
||||
//f
|
||||
val file = FileUtils.getFile(
|
||||
BookHelp.downloadDir,
|
||||
BookHelp.cacheFolderName,
|
||||
book.getFolderName(),
|
||||
"index.epubx"
|
||||
)
|
||||
if (!file.exists()) {
|
||||
val input = if (book.bookUrl.isContentScheme()) {
|
||||
val uri = Uri.parse(book.bookUrl)
|
||||
appCtx.contentResolver.openInputStream(uri)
|
||||
} else {
|
||||
File(book.bookUrl).inputStream()
|
||||
}
|
||||
input ?: return null
|
||||
FileUtils.writeInputStream(file, input)
|
||||
if (!file.exists()){
|
||||
return EpubReader().readEpub(input)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
val file = BookHelp.getEpubFile(book)
|
||||
//通过懒加载读取epub
|
||||
return EpubReader().readEpubLazy(ZipFile(file), "utf-8")
|
||||
|
||||
|
@ -138,6 +138,13 @@ object LocalBook {
|
||||
val bookFile = FileUtils.getFile(cacheFolder, book.originName)
|
||||
bookFile.delete()
|
||||
}
|
||||
if(book.isEpub()){
|
||||
val bookFile=BookHelp.getEpubFile(book).parentFile
|
||||
if (bookFile!=null&&bookFile.exists()){
|
||||
FileUtils.delete(bookFile,true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (deleteOriginal) {
|
||||
if (book.bookUrl.isContentScheme()) {
|
||||
|
@ -28,6 +28,7 @@ object BitmapUtils {
|
||||
*/
|
||||
fun decodeBitmap(path: String, width: Int, height: Int): Bitmap? {
|
||||
val op = BitmapFactory.Options()
|
||||
op.inPreferredConfig=Config.RGB_565
|
||||
var ips=FileInputStream(path)
|
||||
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
|
||||
op.inJustDecodeBounds = true
|
||||
@ -54,6 +55,7 @@ object BitmapUtils {
|
||||
*/
|
||||
fun decodeBitmap(path: String): Bitmap? {
|
||||
val opts = BitmapFactory.Options()
|
||||
opts.inPreferredConfig=Config.RGB_565
|
||||
var ips=FileInputStream(path)
|
||||
opts.inJustDecodeBounds = true
|
||||
BitmapFactory.decodeStream(ips,null,opts)
|
||||
|
@ -450,7 +450,7 @@ public class IOUtil {
|
||||
output.write(buffer, 0, n);
|
||||
count += n;
|
||||
}
|
||||
input.close();
|
||||
//input.close();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user