pull/2437/head
kunfei 2 years ago
parent 1013f33cf6
commit 8d6ad941b2
  1. 18
      app/src/main/java/io/legado/app/help/book/BookExtensions.kt
  2. 17
      app/src/main/java/io/legado/app/utils/DocumentExtensions.kt

@ -3,16 +3,13 @@
package io.legado.app.help.book
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.constant.BookSourceType
import io.legado.app.constant.BookType
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.config.AppConfig.defaultBookTreeUri
import io.legado.app.utils.getFile
import io.legado.app.utils.isContentScheme
import splitties.init.appCtx
import io.legado.app.utils.*
import java.io.File
@ -64,17 +61,8 @@ fun Book.getLocalUri(): Uri {
//不同的设备书籍保存路径可能不一样 优先尝试寻找当前保存路径下的文件
defaultBookTreeUri ?: return originBookUri
val treeUri = Uri.parse(defaultBookTreeUri)
return if (treeUri.isContentScheme()) {
DocumentFile.fromTreeUri(appCtx, treeUri)?.run {
findFile(originName)?.let {
if (it.exists()) it.uri else originBookUri
} ?: originBookUri
} ?: originBookUri
} else {
val treeFile = File(treeUri.path!!)
val file = treeFile.getFile(originName)
if (file.exists()) Uri.fromFile(file) else originBookUri
}
val treeFileDoc = FileDoc.fromUri(treeUri, true)
return treeFileDoc.find(originName)?.uri ?: originBookUri
}
throw NoStackTraceException("不是本地书籍")
}

@ -145,6 +145,10 @@ data class FileDoc(
companion object {
fun fromUri(uri: Uri, isDir: Boolean, name: String = ""): FileDoc {
return FileDoc(name, isDir, 0, 0, uri)
}
fun fromDocumentFile(doc: DocumentFile): FileDoc {
return FileDoc(
name = doc.name ?: "",
@ -168,6 +172,19 @@ data class FileDoc(
}
}
fun FileDoc.list(filter: ((file: FileDoc) -> Boolean)? = null): ArrayList<FileDoc>? {
if (isDir) {
return DocumentUtils.listFiles(uri, filter)
}
return null
}
fun FileDoc.find(name: String): FileDoc? {
return list {
it.name == name
}?.firstOrNull()
}
@Throws(Exception::class)
fun DocumentFile.writeText(context: Context, data: String, charset: Charset = Charsets.UTF_8) {
uri.writeText(context, data, charset)

Loading…
Cancel
Save