Horis 3 лет назад
Родитель
Сommit
7cc3874de1

+ 4 - 4
app/src/main/java/io/legado/app/help/storage/ImportOldData.kt

@@ -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

+ 16 - 3
app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt

@@ -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()
                     }
                 }

+ 16 - 0
app/src/main/java/io/legado/app/utils/FileDocExtensions.kt

@@ -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
+    }
+}

+ 13 - 1
app/src/main/java/io/legado/app/utils/FileExtensions.kt

@@ -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
+    }
+}