This commit is contained in:
Horis
2023-07-23 21:20:32 +08:00
parent 9af6f493c0
commit 5e208d56ce
4 changed files with 49 additions and 8 deletions
@@ -34,10 +34,10 @@ object ImportOldData {
kotlin.runCatching {
doc.uri.readText(context).let { json ->
val importCount = importOldBookshelf(json)
context.toastOnUi("成功导入书${importCount}")
context.toastOnUi("成功导入书${importCount}")
}
}.onFailure {
context.toastOnUi("导入书失败\n${it.localizedMessage}")
context.toastOnUi("导入书失败\n${it.localizedMessage}")
}
"myBookSource.json" ->
kotlin.runCatching {
@@ -67,9 +67,9 @@ object ImportOldData {
FileUtils.createFileIfNotExist(file, "myBookShelf.json")
val json = shelfFile.readText()
val importCount = importOldBookshelf(json)
context.toastOnUi("成功导入书${importCount}")
context.toastOnUi("成功导入书${importCount}")
}.onFailure {
context.toastOnUi("导入书失败\n${it.localizedMessage}")
context.toastOnUi("导入书失败\n${it.localizedMessage}")
}
kotlin.runCatching {// Book source
@@ -62,18 +62,23 @@ class FileAssociationActivity :
"bookSource" -> showDialogFragment(
ImportBookSourceDialog(it.second, true)
)
"rssSource" -> showDialogFragment(
ImportRssSourceDialog(it.second, true)
)
"replaceRule" -> showDialogFragment(
ImportReplaceRuleDialog(it.second, true)
)
"httpTts" -> showDialogFragment(
ImportHttpTtsDialog(it.second, true)
)
"theme" -> showDialogFragment(
ImportThemeDialog(it.second, true)
)
"txtRule" -> showDialogFragment(
ImportTxtTocRuleDialog(it.second, true)
)
@@ -148,9 +153,12 @@ class FileAssociationActivity :
if (treeUri.isContentScheme()) {
val treeDoc =
DocumentFile.fromTreeUri(this@FileAssociationActivity, treeUri)
if (!treeDoc!!.checkWrite()) {
throw SecurityException("请重新设置书籍保存位置\nPermission Denial")
}
readUri(uri) { fileDoc, inputStream ->
val name = fileDoc.name
var doc = treeDoc!!.findFile(name)
var doc = treeDoc.findFile(name)
if (doc == null || fileDoc.lastModified > doc.lastModified()) {
if (doc == null) {
doc = treeDoc.createFile(FileUtils.getMimeType(name), name)
@@ -165,6 +173,9 @@ class FileAssociationActivity :
}
} else {
val treeFile = File(treeUri.path ?: treeUri.toString())
if (!treeFile.checkWrite()) {
throw SecurityException("请重新设置书籍保存位置\nPermission Denial")
}
readUri(uri) { fileDoc, inputStream ->
val name = fileDoc.name
val file = treeFile.getFile(name)
@@ -184,9 +195,11 @@ class FileAssociationActivity :
title = getString(R.string.select_book_folder)
mode = HandleFileContract.DIR_SYS
}
else -> {
AppLog.put("导入书籍失败", it)
toastOnUi(it.localizedMessage)
val msg = "导入书籍失败\n${it.localizedMessage}"
AppLog.put(msg, it)
toastOnUi(msg)
finish()
}
}
@@ -309,3 +309,19 @@ fun DocumentFile.readBytes(context: Context): ByteArray {
return buffer
} ?: throw NoStackTraceException("打开文件失败\n${uri}")
}
fun DocumentFile.checkWrite(): Boolean {
return try {
val filename = System.currentTimeMillis().toString()
createFile(FileUtils.getMimeType(filename), filename)?.let {
it.openOutputStream()?.let { out ->
out.use { }
it.delete()
return true
}
}
false
} catch (e: Exception) {
false
}
}
@@ -66,4 +66,16 @@ fun File.createFolderReplace(): File {
}
mkdirs()
return this
}
}
fun File.checkWrite(): Boolean {
return try {
val filename = System.currentTimeMillis().toString()
val file = FileUtils.createFileIfNotExist(this, filename)
file.outputStream().use { }
file.delete()
true
} catch (e: Exception) {
false
}
}