Merge remote-tracking branch 'origin/master'

pull/1486/head
gedoor 3 years ago
commit 8922dfafd4
  1. 19
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  2. 8
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt

@ -34,7 +34,7 @@ object LocalBook {
UmdFile.getChapterList(book) UmdFile.getChapterList(book)
} }
else -> { else -> {
TextFile().analyze(book) TextFile.getChapterList(book)
} }
} }
if (chapters.isEmpty()) { if (chapters.isEmpty()) {
@ -59,23 +59,25 @@ object LocalBook {
fun importFile(uri: Uri): Book { fun importFile(uri: Uri): Book {
val path: String val path: String
val updateTime: Long
//这个变量不要修改,否则会导致读取不到缓存 //这个变量不要修改,否则会导致读取不到缓存
val fileName = (if (uri.isContentScheme()) { val fileName = (if (uri.isContentScheme()) {
path = uri.toString() path = uri.toString()
val doc = DocumentFile.fromSingleUri(appCtx, uri) val doc = DocumentFile.fromSingleUri(appCtx, uri)!!
doc?.let { updateTime = doc.lastModified()
val bookFile = cacheFolder.getFile(it.name!!) val bookFile = cacheFolder.getFile(doc.name!!)
if (!bookFile.exists()) { if (!bookFile.exists()) {
bookFile.createNewFile() bookFile.createNewFile()
doc.readBytes(appCtx).let { bytes -> doc.readBytes(appCtx).let { bytes ->
bookFile.writeBytes(bytes) bookFile.writeBytes(bytes)
} }
} }
} doc.name!!
doc?.name!!
} else { } else {
path = uri.path!! path = uri.path!!
File(path).name val file = File(path)
updateTime = file.lastModified()
file.name
}) })
var book = appDb.bookDao.getBook(path) var book = appDb.bookDao.getBook(path)
if (book == null) { if (book == null) {
@ -89,7 +91,8 @@ object LocalBook {
appCtx.externalFiles, appCtx.externalFiles,
"covers", "covers",
"${MD5Utils.md5Encode16(path)}.jpg" "${MD5Utils.md5Encode16(path)}.jpg"
) ),
latestChapterTime = updateTime
) )
if (book.isEpub()) EpubFile.upBookInfo(book) if (book.isEpub()) EpubFile.upBookInfo(book)
if (book.isUmd()) UmdFile.upBookInfo(book) if (book.isUmd()) UmdFile.upBookInfo(book)

@ -14,13 +14,13 @@ 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
class TextFile { class TextFile(private val book: Book) {
private val tocRules = arrayListOf<TxtTocRule>() private val tocRules = arrayListOf<TxtTocRule>()
private lateinit var charset: Charset private lateinit var charset: Charset
@Throws(Exception::class) @Throws(Exception::class)
fun analyze(book: Book): ArrayList<BookChapter> { fun getChapterList(): ArrayList<BookChapter> {
val bookFile = getBookFile(book) val bookFile = getBookFile(book)
if (book.charset == null) { if (book.charset == null) {
book.charset = EncodingDetect.getEncode(bookFile) book.charset = EncodingDetect.getEncode(bookFile)
@ -249,6 +249,10 @@ class TextFile {
//没有标题的时候,每个章节的最大长度 //没有标题的时候,每个章节的最大长度
private const val MAX_LENGTH_WITH_NO_CHAPTER = 10 * 1024 private const val MAX_LENGTH_WITH_NO_CHAPTER = 10 * 1024
fun getChapterList(book: Book): ArrayList<BookChapter> {
return TextFile(book).getChapterList()
}
fun getContent(book: Book, bookChapter: BookChapter): String { fun getContent(book: Book, bookChapter: BookChapter): String {
val bookFile = getBookFile(book) val bookFile = getBookFile(book)
//获取文件流 //获取文件流

Loading…
Cancel
Save