本地书籍无权限则保存到自己选定的文件夹

pull/1486/head
gedoor 3 years ago
parent c33eba9423
commit d118a2551e
  1. 32
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt

@ -1,13 +1,15 @@
package io.legado.app.model.localBook package io.legado.app.model.localBook
import android.net.Uri import android.net.Uri
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
import io.legado.app.data.entities.TxtTocRule 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.EncodingDetect
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.StringUtils
import io.legado.app.utils.isContentScheme
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
@ -24,9 +26,11 @@ class TextFile(private val book: Book) {
@Throws(FileNotFoundException::class) @Throws(FileNotFoundException::class)
fun getChapterList(): ArrayList<BookChapter> { fun getChapterList(): ArrayList<BookChapter> {
val bookFile = getBookFile(book) return getBookInputStream(book).use {
val buffer = ByteArray(BUFFER_SIZE)
it.read(buffer, 0, buffer.size)
if (book.charset == null) { if (book.charset == null) {
book.charset = EncodingDetect.getEncode(bookFile) book.charset = EncodingDetect.getEncode(buffer)
} }
charset = book.fileCharset() charset = book.fileCharset()
val rulePattern = if (book.tocUrl.isNotEmpty()) { val rulePattern = if (book.tocUrl.isNotEmpty()) {
@ -35,7 +39,6 @@ class TextFile(private val book: Book) {
tocRules.addAll(getTocRules()) tocRules.addAll(getTocRules())
null null
} }
return getBookInputStream(book).use {
analyze(it, book, rulePattern) analyze(it, book, rulePattern)
} }
} }
@ -276,25 +279,6 @@ class TextFile(private val book: Book) {
return FileInputStream(File(book.bookUrl)) return FileInputStream(File(book.bookUrl))
} }
private fun getBookFile(book: Book): File {
if (book.bookUrl.isContentScheme()) {
val uri = Uri.parse(book.bookUrl)
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()) {
bookFile.createNewFile()
DocumentUtils.readBytes(appCtx, uri).let {
bookFile.writeBytes(it)
}
}
return bookFile
}
return File(book.bookUrl)
}
private fun getTocRules(): List<TxtTocRule> { private fun getTocRules(): List<TxtTocRule> {
var rules = appDb.txtTocRuleDao.enabled var rules = appDb.txtTocRuleDao.enabled
if (rules.isEmpty()) { if (rules.isEmpty()) {

Loading…
Cancel
Save