Merge pull request #1758 from Xwite/master

fix:识别content://download/1234的后缀
pull/1759/head
kunfei 3 years ago committed by GitHub
commit 63df9baaae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt

@ -4,6 +4,7 @@ import android.app.Application
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.constant.AppPattern.bookFileRegex
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.isJson import io.legado.app.utils.isJson
@ -23,15 +24,22 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
fun dispatchIndent(uri: Uri, finally: (title: String, msg: String) -> Unit) { fun dispatchIndent(uri: Uri, finally: (title: String, msg: String) -> Unit) {
execute { execute {
lateinit var fileName: String
lateinit var content: String
//如果是普通的url,需要根据返回的内容判断是什么 //如果是普通的url,需要根据返回的内容判断是什么
if (uri.scheme == "file" || uri.scheme == "content") { if (uri.scheme == "file" || uri.scheme == "content") {
val content = if (uri.scheme == "file") { if (uri.scheme == "file") {
File(uri.path.toString()).readText() val file = File(uri.path.toString())
content = file.readText()
fileName = file.name
} else { } else {
DocumentFile.fromSingleUri(context, uri)?.readText(context) val file = DocumentFile.fromSingleUri(context, uri)
} ?: throw NoStackTraceException("文件不存在") content = file?.readText(context) ?: throw NoStackTraceException("文件不存在")
fileName = file!!.name ?: ""
}
if (content.isJson()) {
//暂时根据文件内容判断属于什么
when { when {
content.isJson() -> when {
content.contains("bookSourceUrl") -> content.contains("bookSourceUrl") ->
importBookSourceLive.postValue(content) importBookSourceLive.postValue(content)
content.contains("sourceUrl") -> content.contains("sourceUrl") ->
@ -46,7 +54,10 @@ class FileAssociationViewModel(application: Application) : BaseAssociationViewMo
importHttpTTS(content, finally) importHttpTTS(content, finally)
else -> errorLiveData.postValue("格式不对") else -> errorLiveData.postValue("格式不对")
} }
else -> importBookLiveData.postValue(uri) } else if (fileName.matches(bookFileRegex)) {
importBookLiveData.postValue(uri)
} else {
throw NoStackTraceException("$fileName 暂未支持的本地书籍格式(TXT/UMD/EPUB)")
} }
} else { } else {
onLineImportLive.postValue(uri) onLineImportLive.postValue(uri)

Loading…
Cancel
Save