mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -164,7 +164,7 @@ interface JsExtensions {
|
||||
fun importScript(path: String): String {
|
||||
val result = when {
|
||||
path.startsWith("http") -> cacheFile(path) ?: ""
|
||||
path.isContentScheme() -> DocumentUtils.readText(appCtx, Uri.parse(path))
|
||||
path.isUri() -> Uri.parse(path).readText(appCtx)
|
||||
path.startsWith("/storage") -> FileUtils.readText(path)
|
||||
else -> readTxtFile(path)
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ object ImportOldData {
|
||||
when (doc.name) {
|
||||
"myBookShelf.json" ->
|
||||
kotlin.runCatching {
|
||||
DocumentUtils.readText(context, doc.uri).let { json ->
|
||||
doc.uri.readText(context).let { json ->
|
||||
val importCount = importOldBookshelf(json)
|
||||
context.toastOnUi("成功导入书籍${importCount}")
|
||||
}
|
||||
@@ -28,7 +28,7 @@ object ImportOldData {
|
||||
}
|
||||
"myBookSource.json" ->
|
||||
kotlin.runCatching {
|
||||
DocumentUtils.readText(context, doc.uri).let { json ->
|
||||
doc.uri.readText(context).let { json ->
|
||||
val importCount = importOldSource(json)
|
||||
context.toastOnUi("成功导入书源${importCount}")
|
||||
}
|
||||
@@ -37,7 +37,7 @@ object ImportOldData {
|
||||
}
|
||||
"myBookReplaceRule.json" ->
|
||||
kotlin.runCatching {
|
||||
DocumentUtils.readText(context, doc.uri).let { json ->
|
||||
doc.uri.readText(context).let { json ->
|
||||
val importCount = importOldReplaceRule(json)
|
||||
context.toastOnUi("成功导入替换规则${importCount}")
|
||||
}
|
||||
|
||||
@@ -53,44 +53,14 @@ object DocumentUtils {
|
||||
return parent
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Throws(Exception::class)
|
||||
fun writeText(
|
||||
context: Context,
|
||||
data: String,
|
||||
fileUri: Uri,
|
||||
charset: Charset = Charsets.UTF_8
|
||||
): Boolean {
|
||||
return writeBytes(context, data.toByteArray(charset), fileUri)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Throws(Exception::class)
|
||||
fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean {
|
||||
context.contentResolver.openOutputStream(fileUri)?.let {
|
||||
it.write(data)
|
||||
it.close()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Throws(Exception::class)
|
||||
fun readText(context: Context, uri: Uri): String {
|
||||
return String(readBytes(context, uri))
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Throws(Exception::class)
|
||||
fun readBytes(context: Context, uri: Uri): ByteArray {
|
||||
context.contentResolver.openInputStream(uri)?.let {
|
||||
val len: Int = it.available()
|
||||
val buffer = ByteArray(len)
|
||||
it.read(buffer)
|
||||
it.close()
|
||||
return buffer
|
||||
} ?: throw NoStackTraceException("打开文件失败\n${uri}")
|
||||
private val projection by lazy {
|
||||
arrayOf(
|
||||
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
|
||||
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
|
||||
DocumentsContract.Document.COLUMN_LAST_MODIFIED,
|
||||
DocumentsContract.Document.COLUMN_SIZE,
|
||||
DocumentsContract.Document.COLUMN_MIME_TYPE
|
||||
)
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
@@ -104,13 +74,7 @@ object DocumentUtils {
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = appCtx.contentResolver.query(
|
||||
childrenUri, arrayOf(
|
||||
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
|
||||
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
|
||||
DocumentsContract.Document.COLUMN_LAST_MODIFIED,
|
||||
DocumentsContract.Document.COLUMN_SIZE,
|
||||
DocumentsContract.Document.COLUMN_MIME_TYPE
|
||||
), null, null, DocumentsContract.Document.COLUMN_DISPLAY_NAME
|
||||
childrenUri, projection, null, null, DocumentsContract.Document.COLUMN_DISPLAY_NAME
|
||||
)
|
||||
cursor?.let {
|
||||
val ici = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
||||
@@ -206,20 +170,26 @@ data class FileDoc(
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun DocumentFile.writeText(context: Context, data: String, charset: Charset = Charsets.UTF_8) {
|
||||
DocumentUtils.writeText(context, data, this.uri, charset)
|
||||
uri.writeText(context, data, charset)
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun DocumentFile.writeBytes(context: Context, data: ByteArray) {
|
||||
DocumentUtils.writeBytes(context, data, this.uri)
|
||||
uri.writeBytes(context, data)
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun DocumentFile.readText(context: Context): String {
|
||||
return DocumentUtils.readText(context, this.uri)
|
||||
return String(readBytes(context))
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun DocumentFile.readBytes(context: Context): ByteArray {
|
||||
return DocumentUtils.readBytes(context, this.uri)
|
||||
return context.contentResolver.openInputStream(uri)?.let {
|
||||
val len: Int = it.available()
|
||||
val buffer = ByteArray(len)
|
||||
it.read(buffer)
|
||||
it.close()
|
||||
return buffer
|
||||
} ?: throw NoStackTraceException("打开文件失败\n${uri}")
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import io.legado.app.lib.permission.PermissionsCompat
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
import java.nio.charset.Charset
|
||||
|
||||
fun Uri.isContentScheme() = this.scheme == "content"
|
||||
|
||||
@@ -100,7 +101,13 @@ fun Fragment.readUri(uri: Uri?, success: (fileDoc: FileDoc, inputStream: InputSt
|
||||
@Throws(Exception::class)
|
||||
fun Uri.readBytes(context: Context): ByteArray {
|
||||
return if (this.isContentScheme()) {
|
||||
DocumentUtils.readBytes(context, this)
|
||||
context.contentResolver.openInputStream(this)?.let {
|
||||
val len: Int = it.available()
|
||||
val buffer = ByteArray(len)
|
||||
it.read(buffer)
|
||||
it.close()
|
||||
return buffer
|
||||
} ?: throw NoStackTraceException("打开文件失败\n${this}")
|
||||
} else {
|
||||
val path = RealPathUtil.getPath(context, this)
|
||||
if (path?.isNotEmpty() == true) {
|
||||
@@ -124,7 +131,12 @@ fun Uri.writeBytes(
|
||||
byteArray: ByteArray
|
||||
): Boolean {
|
||||
if (this.isContentScheme()) {
|
||||
return DocumentUtils.writeBytes(context, byteArray, this)
|
||||
context.contentResolver.openOutputStream(this)?.let {
|
||||
it.write(byteArray)
|
||||
it.close()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
} else {
|
||||
val path = RealPathUtil.getPath(context, this)
|
||||
if (path?.isNotEmpty() == true) {
|
||||
@@ -136,8 +148,8 @@ fun Uri.writeBytes(
|
||||
}
|
||||
|
||||
@Throws(Exception::class)
|
||||
fun Uri.writeText(context: Context, text: String): Boolean {
|
||||
return writeBytes(context, text.toByteArray())
|
||||
fun Uri.writeText(context: Context, text: String, charset: Charset = Charsets.UTF_8): Boolean {
|
||||
return writeBytes(context, text.toByteArray(charset))
|
||||
}
|
||||
|
||||
fun Uri.writeBytes(
|
||||
|
||||
Reference in New Issue
Block a user