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