mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
3dec62a628
commit
a5fbce7963
@ -44,7 +44,8 @@ object LocalBook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getContent(book: Book, chapter: BookChapter): String? {
|
fun getContent(book: Book, chapter: BookChapter): String? {
|
||||||
return when {
|
return try {
|
||||||
|
when {
|
||||||
book.isEpub() -> {
|
book.isEpub() -> {
|
||||||
EpubFile.getContent(book, chapter)
|
EpubFile.getContent(book, chapter)
|
||||||
}
|
}
|
||||||
@ -55,6 +56,9 @@ object LocalBook {
|
|||||||
TextFile.getContent(book, chapter)
|
TextFile.getContent(book, chapter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.localizedMessage
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun importFile(uri: Uri): Book {
|
fun importFile(uri: Uri): Book {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package io.legado.app.model.localBook
|
package io.legado.app.model.localBook
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.system.Os
|
||||||
|
import androidx.documentfile.provider.DocumentFile
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
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
|
||||||
@ -8,8 +10,7 @@ import io.legado.app.data.entities.TxtTocRule
|
|||||||
import io.legado.app.help.DefaultData
|
import io.legado.app.help.DefaultData
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import java.io.File
|
import java.io.*
|
||||||
import java.io.RandomAccessFile
|
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
import java.util.regex.Matcher
|
import java.util.regex.Matcher
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
@ -253,22 +254,33 @@ class TextFile(private val book: Book) {
|
|||||||
return TextFile(book).getChapterList()
|
return TextFile(book).getChapterList()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Throws(FileNotFoundException::class)
|
||||||
fun getContent(book: Book, bookChapter: BookChapter): String {
|
fun getContent(book: Book, bookChapter: BookChapter): String {
|
||||||
val bookFile = getBookFile(book)
|
val count = (bookChapter.end!! - bookChapter.start!!).toInt()
|
||||||
//获取文件流
|
val content = ByteArray(count)
|
||||||
val bookStream = RandomAccessFile(bookFile, "r")
|
Os.read(getBookFD(book), content, bookChapter.start!!.toInt(), count)
|
||||||
val content = ByteArray((bookChapter.end!! - bookChapter.start!!).toInt())
|
|
||||||
bookStream.seek(bookChapter.start!!)
|
|
||||||
bookStream.read(content)
|
|
||||||
return String(content, book.fileCharset())
|
return String(content, book.fileCharset())
|
||||||
.substringAfter(bookChapter.title)
|
.substringAfter(bookChapter.title)
|
||||||
.replace("^[\\n\\s]+".toRegex(), " ")
|
.replace("^[\\n\\s]+".toRegex(), " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Throws(FileNotFoundException::class)
|
||||||
|
private fun getBookFD(book: Book): FileDescriptor? {
|
||||||
|
if (book.bookUrl.isContentScheme()) {
|
||||||
|
val uri = Uri.parse(book.bookUrl)
|
||||||
|
return appCtx.contentResolver.openFileDescriptor(uri, "r")?.fileDescriptor
|
||||||
|
}
|
||||||
|
return FileInputStream(File(book.bookUrl)).fd
|
||||||
|
}
|
||||||
|
|
||||||
private fun getBookFile(book: Book): File {
|
private fun getBookFile(book: Book): File {
|
||||||
if (book.bookUrl.isContentScheme()) {
|
if (book.bookUrl.isContentScheme()) {
|
||||||
val uri = Uri.parse(book.bookUrl)
|
val uri = Uri.parse(book.bookUrl)
|
||||||
val bookFile = LocalBook.cacheFolder.getFile(book.originName)
|
val bookFile = LocalBook.cacheFolder.getFile(book.originName)
|
||||||
|
val doc = DocumentFile.fromSingleUri(appCtx, uri)!!
|
||||||
|
if (bookFile.exists() && bookFile.lastModified() >= doc.lastModified()) {
|
||||||
|
return bookFile
|
||||||
|
}
|
||||||
if (!bookFile.exists()) {
|
if (!bookFile.exists()) {
|
||||||
bookFile.createNewFile()
|
bookFile.createNewFile()
|
||||||
DocumentUtils.readBytes(appCtx, uri).let {
|
DocumentUtils.readBytes(appCtx, uri).let {
|
||||||
|
Loading…
Reference in New Issue
Block a user