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 { kotlin.runCatching {
doc.uri.readText(context).let { json -> doc.uri.readText(context).let { json ->
val importCount = importOldBookshelf(json) val importCount = importOldBookshelf(json)
context.toastOnUi("成功导入书籍${importCount}") context.toastOnUi("成功导入书架${importCount}")
} }
}.onFailure { }.onFailure {
context.toastOnUi("导入书籍失败\n${it.localizedMessage}") context.toastOnUi("导入书架失败\n${it.localizedMessage}")
} }
"myBookSource.json" -> "myBookSource.json" ->
kotlin.runCatching { kotlin.runCatching {
@@ -67,9 +67,9 @@ object ImportOldData {
FileUtils.createFileIfNotExist(file, "myBookShelf.json") FileUtils.createFileIfNotExist(file, "myBookShelf.json")
val json = shelfFile.readText() val json = shelfFile.readText()
val importCount = importOldBookshelf(json) val importCount = importOldBookshelf(json)
context.toastOnUi("成功导入书籍${importCount}") context.toastOnUi("成功导入书架${importCount}")
}.onFailure { }.onFailure {
context.toastOnUi("导入书籍失败\n${it.localizedMessage}") context.toastOnUi("导入书架失败\n${it.localizedMessage}")
} }
kotlin.runCatching {// Book source kotlin.runCatching {// Book source
@@ -62,18 +62,23 @@ class FileAssociationActivity :
"bookSource" -> showDialogFragment( "bookSource" -> showDialogFragment(
ImportBookSourceDialog(it.second, true) ImportBookSourceDialog(it.second, true)
) )
"rssSource" -> showDialogFragment( "rssSource" -> showDialogFragment(
ImportRssSourceDialog(it.second, true) ImportRssSourceDialog(it.second, true)
) )
"replaceRule" -> showDialogFragment( "replaceRule" -> showDialogFragment(
ImportReplaceRuleDialog(it.second, true) ImportReplaceRuleDialog(it.second, true)
) )
"httpTts" -> showDialogFragment( "httpTts" -> showDialogFragment(
ImportHttpTtsDialog(it.second, true) ImportHttpTtsDialog(it.second, true)
) )
"theme" -> showDialogFragment( "theme" -> showDialogFragment(
ImportThemeDialog(it.second, true) ImportThemeDialog(it.second, true)
) )
"txtRule" -> showDialogFragment( "txtRule" -> showDialogFragment(
ImportTxtTocRuleDialog(it.second, true) ImportTxtTocRuleDialog(it.second, true)
) )
@@ -148,9 +153,12 @@ class FileAssociationActivity :
if (treeUri.isContentScheme()) { if (treeUri.isContentScheme()) {
val treeDoc = val treeDoc =
DocumentFile.fromTreeUri(this@FileAssociationActivity, treeUri) DocumentFile.fromTreeUri(this@FileAssociationActivity, treeUri)
if (!treeDoc!!.checkWrite()) {
throw SecurityException("请重新设置书籍保存位置\nPermission Denial")
}
readUri(uri) { fileDoc, inputStream -> readUri(uri) { fileDoc, inputStream ->
val name = fileDoc.name val name = fileDoc.name
var doc = treeDoc!!.findFile(name) var doc = treeDoc.findFile(name)
if (doc == null || fileDoc.lastModified > doc.lastModified()) { if (doc == null || fileDoc.lastModified > doc.lastModified()) {
if (doc == null) { if (doc == null) {
doc = treeDoc.createFile(FileUtils.getMimeType(name), name) doc = treeDoc.createFile(FileUtils.getMimeType(name), name)
@@ -165,6 +173,9 @@ class FileAssociationActivity :
} }
} else { } else {
val treeFile = File(treeUri.path ?: treeUri.toString()) val treeFile = File(treeUri.path ?: treeUri.toString())
if (!treeFile.checkWrite()) {
throw SecurityException("请重新设置书籍保存位置\nPermission Denial")
}
readUri(uri) { fileDoc, inputStream -> readUri(uri) { fileDoc, inputStream ->
val name = fileDoc.name val name = fileDoc.name
val file = treeFile.getFile(name) val file = treeFile.getFile(name)
@@ -184,9 +195,11 @@ class FileAssociationActivity :
title = getString(R.string.select_book_folder) title = getString(R.string.select_book_folder)
mode = HandleFileContract.DIR_SYS mode = HandleFileContract.DIR_SYS
} }
else -> { else -> {
AppLog.put("导入书籍失败", it) val msg = "导入书籍失败\n${it.localizedMessage}"
toastOnUi(it.localizedMessage) AppLog.put(msg, it)
toastOnUi(msg)
finish() finish()
} }
} }
@@ -309,3 +309,19 @@ fun DocumentFile.readBytes(context: Context): ByteArray {
return buffer return buffer
} ?: throw NoStackTraceException("打开文件失败\n${uri}") } ?: 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
}
}
@@ -67,3 +67,15 @@ fun File.createFolderReplace(): File {
mkdirs() mkdirs()
return this 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
}
}