kunfei 4 ani în urmă
părinte
comite
e8cea822a2

+ 3 - 3
app/src/main/java/io/legado/app/help/BookHelp.kt

@@ -31,14 +31,14 @@ object BookHelp {
     private val downloadImages = CopyOnWriteArraySet<String>()
 
     fun clearCache() {
-        FileUtils.deleteFile(
+        FileUtils.delete(
             FileUtils.getPath(downloadDir, cacheFolderName)
         )
     }
 
     fun clearCache(book: Book) {
         val filePath = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName())
-        FileUtils.deleteFile(filePath)
+        FileUtils.delete(filePath)
     }
 
     /**
@@ -53,7 +53,7 @@ object BookHelp {
             val file = downloadDir.getFile(cacheFolderName)
             file.listFiles()?.forEach { bookFile ->
                 if (!bookFolderNames.contains(bookFile.name)) {
-                    FileUtils.deleteFile(bookFile.absolutePath)
+                    FileUtils.delete(bookFile.absolutePath)
                 }
             }
         }

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

@@ -177,7 +177,7 @@ interface JsExtensions {
             FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
             "${MD5Utils.md5Encode16(url)}.${type}"
         )
-        FileUtils.deleteFile(zipPath)
+        FileUtils.delete(zipPath)
         val zipFile = FileUtils.createFileIfNotExist(zipPath)
         StringUtils.hexStringToByte(content).let {
             if (it.isNotEmpty()) {
@@ -363,11 +363,11 @@ interface JsExtensions {
             FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
             FileUtils.getNameExcludeExtension(zipPath)
         )
-        FileUtils.deleteFile(unzipPath)
+        FileUtils.delete(unzipPath)
         val zipFile = getFile(zipPath)
         val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath)
         ZipUtils.unzipFile(zipFile, unzipFolder)
-        FileUtils.deleteFile(zipFile.absolutePath)
+        FileUtils.delete(zipFile.absolutePath)
         return unzipPath.substring(FileUtils.getCachePath().length)
     }
 
@@ -388,7 +388,7 @@ interface JsExtensions {
                 contents.deleteCharAt(contents.length - 1)
             }
         }
-        FileUtils.deleteFile(unzipFolder.absolutePath)
+        FileUtils.delete(unzipFolder.absolutePath)
         return contents.toString()
     }
 

+ 75 - 3
app/src/main/java/io/legado/app/help/RuleBigDataHelp.kt

@@ -1,29 +1,101 @@
 package io.legado.app.help
 
+import io.legado.app.utils.FileUtils
+import io.legado.app.utils.MD5Utils
+import io.legado.app.utils.externalFiles
+import splitties.init.appCtx
+import java.io.File
+
 object RuleBigDataHelp {
 
-    fun putBookVariable(bookUrl: String, key: String, value: String?) {
+    private val ruleDataDir = FileUtils.createFolderIfNotExist(appCtx.externalFiles, "ruleData")
+    private val bookData = FileUtils.createFolderIfNotExist(ruleDataDir, "book")
+    private val rssData = FileUtils.createFolderIfNotExist(ruleDataDir, "rss")
 
+    fun putBookVariable(bookUrl: String, key: String, value: String?) {
+        val md5BookUrl = MD5Utils.md5Encode(bookUrl)
+        val md5Key = MD5Utils.md5Encode(key)
+        if (value == null) {
+            FileUtils.delete(FileUtils.getPath(bookData, md5BookUrl, "$md5Key.txt"), true)
+        } else {
+            val valueFile = FileUtils.createFileIfNotExist(bookData, md5BookUrl, "$md5Key.txt")
+            valueFile.writeText(value)
+            val bookUrlFile = File(FileUtils.getPath(bookData, md5BookUrl, "bookUrl.txt"))
+            if (!bookUrlFile.exists()) {
+                bookUrlFile.writeText(bookUrl)
+            }
+        }
     }
 
     fun getBookVariable(bookUrl: String, key: String?): String? {
+        val md5BookUrl = MD5Utils.md5Encode(bookUrl)
+        val md5Key = MD5Utils.md5Encode(key)
+        val file = File(FileUtils.getPath(bookData, md5BookUrl, "$md5Key.txt"))
+        if (file.exists()) {
+            return file.readText()
+        }
         return null
     }
 
 
     fun putChapterVariable(bookUrl: String, chapterUrl: String, key: String, value: String?) {
-
+        val md5BookUrl = MD5Utils.md5Encode(bookUrl)
+        val md5ChapterUrl = MD5Utils.md5Encode(chapterUrl)
+        val md5Key = MD5Utils.md5Encode(key)
+        if (value == null) {
+            FileUtils.delete(FileUtils.getPath(bookData, md5BookUrl, md5ChapterUrl, "$md5Key.txt"))
+        } else {
+            val valueFile =
+                FileUtils.createFileIfNotExist(bookData, md5BookUrl, md5ChapterUrl, "$md5Key.txt")
+            valueFile.writeText(value)
+            val bookUrlFile = File(FileUtils.getPath(bookData, md5BookUrl, "bookUrl.txt"))
+            if (!bookUrlFile.exists()) {
+                bookUrlFile.writeText(bookUrl)
+            }
+        }
     }
 
     fun getChapterVariable(bookUrl: String, chapterUrl: String, key: String): String? {
+        val md5BookUrl = MD5Utils.md5Encode(bookUrl)
+        val md5ChapterUrl = MD5Utils.md5Encode(chapterUrl)
+        val md5Key = MD5Utils.md5Encode(key)
+        val file = File(FileUtils.getPath(bookData, md5BookUrl, md5ChapterUrl, "$md5Key.txt"))
+        if (file.exists()) {
+            return file.readText()
+        }
         return null
     }
 
     fun putRssVariable(origin: String, link: String, key: String, value: String?) {
-
+        val md5Origin = MD5Utils.md5Encode(origin)
+        val md5Link = MD5Utils.md5Encode(link)
+        val md5Key = MD5Utils.md5Encode(key)
+        val filePath = FileUtils.getPath(rssData, md5Origin, md5Link, "$md5Key.txt")
+        if (value == null) {
+            FileUtils.delete(filePath)
+        } else {
+            val valueFile = FileUtils.createFileIfNotExist(filePath)
+            valueFile.writeText(value)
+            val originFile = File(FileUtils.getPath(rssData, md5Origin, "origin.txt"))
+            if (!originFile.exists()) {
+                originFile.writeText(origin)
+            }
+            val linFile = File(FileUtils.getPath(rssData, md5Origin, md5Link, "origin.txt"))
+            if (!linFile.exists()) {
+                linFile.writeText(link)
+            }
+        }
     }
 
     fun getRssVariable(origin: String, link: String, key: String): String? {
+        val md5Origin = MD5Utils.md5Encode(origin)
+        val md5Link = MD5Utils.md5Encode(link)
+        val md5Key = MD5Utils.md5Encode(key)
+        val filePath = FileUtils.getPath(rssData, md5Origin, md5Link, "$md5Key.txt")
+        val file = File(filePath)
+        if (file.exists()) {
+            return file.readText()
+        }
         return null
     }
 }

+ 4 - 4
app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt

@@ -105,11 +105,11 @@ object ReadBookConfig {
         Coroutine.async {
             synchronized(this) {
                 GSON.toJson(configList).let {
-                    FileUtils.deleteFile(configFilePath)
+                    FileUtils.delete(configFilePath)
                     FileUtils.createFileIfNotExist(configFilePath).writeText(it)
                 }
                 GSON.toJson(shareConfig).let {
-                    FileUtils.deleteFile(shareConfigFilePath)
+                    FileUtils.delete(shareConfigFilePath)
                     FileUtils.createFileIfNotExist(shareConfigFilePath).writeText(it)
                 }
             }
@@ -370,11 +370,11 @@ object ReadBookConfig {
         return kotlin.runCatching {
             withContext(IO) {
                 val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip")
-                FileUtils.deleteFile(configZipPath)
+                FileUtils.delete(configZipPath)
                 val zipFile = FileUtils.createFileIfNotExist(configZipPath)
                 zipFile.writeBytes(byteArray)
                 val configDirPath = FileUtils.getPath(appCtx.externalCache, "readConfig")
-                FileUtils.deleteFile(configDirPath)
+                FileUtils.delete(configDirPath)
                 @Suppress("BlockingMethodInNonBlockingContext")
                 ZipUtils.unzipFile(zipFile, FileUtils.createFolderIfNotExist(configDirPath))
                 val configDir = FileUtils.createFolderIfNotExist(configDirPath)

+ 1 - 1
app/src/main/java/io/legado/app/help/config/ThemeConfig.kt

@@ -74,7 +74,7 @@ object ThemeConfig {
 
     fun save() {
         val json = GSON.toJson(configList)
-        FileUtils.deleteFile(configFilePath)
+        FileUtils.delete(configFilePath)
         FileUtils.createFileIfNotExist(configFilePath).writeText(json)
     }
 

+ 1 - 1
app/src/main/java/io/legado/app/help/storage/AppWebDav.kt

@@ -124,7 +124,7 @@ object AppWebDav {
                 for (i in 0 until paths.size) {
                     paths[i] = path + File.separator + paths[i]
                 }
-                FileUtils.deleteFile(zipFilePath)
+                FileUtils.delete(zipFilePath)
                 if (ZipUtils.zipFiles(paths, zipFilePath)) {
                     val putUrl = "${rootWebDavUrl}${backupFileName}"
                     WebDav(putUrl).upload(zipFilePath)

+ 1 - 1
app/src/main/java/io/legado/app/help/storage/Backup.kt

@@ -66,7 +66,7 @@ object Backup {
     suspend fun backup(context: Context, path: String?, isAuto: Boolean = false) {
         context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis())
         withContext(IO) {
-            FileUtils.deleteFile(backupPath)
+            FileUtils.delete(backupPath)
             writeListToJson(appDb.bookDao.all, "bookshelf.json", backupPath)
             writeListToJson(appDb.bookmarkDao.all, "bookmark.json", backupPath)
             writeListToJson(appDb.bookGroupDao.all, "bookGroup.json", backupPath)

+ 3 - 3
app/src/main/java/io/legado/app/help/storage/Restore.kt

@@ -124,7 +124,7 @@ object Restore {
                 val file =
                     FileUtils.createFileIfNotExist("$path${File.separator}${ThemeConfig.configFileName}")
                 if (file.exists()) {
-                    FileUtils.deleteFile(ThemeConfig.configFilePath)
+                    FileUtils.delete(ThemeConfig.configFilePath)
                     file.copyTo(File(ThemeConfig.configFilePath))
                     ThemeConfig.upConfig()
                 }
@@ -137,7 +137,7 @@ object Restore {
                     val file =
                         FileUtils.createFileIfNotExist("$path${File.separator}${ReadBookConfig.configFileName}")
                     if (file.exists()) {
-                        FileUtils.deleteFile(ReadBookConfig.configFilePath)
+                        FileUtils.delete(ReadBookConfig.configFilePath)
                         file.copyTo(File(ReadBookConfig.configFilePath))
                         ReadBookConfig.initConfigs()
                     }
@@ -148,7 +148,7 @@ object Restore {
                     val file =
                         FileUtils.createFileIfNotExist("$path${File.separator}${ReadBookConfig.shareConfigFileName}")
                     if (file.exists()) {
-                        FileUtils.deleteFile(ReadBookConfig.shareConfigFilePath)
+                        FileUtils.delete(ReadBookConfig.shareConfigFilePath)
                         file.copyTo(File(ReadBookConfig.shareConfigFilePath))
                         ReadBookConfig.initShareConfig()
                     }

+ 1 - 1
app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@@ -167,7 +167,7 @@ object LocalBook {
                     val uri = Uri.parse(book.bookUrl)
                     DocumentFile.fromSingleUri(appCtx, uri)?.delete()
                 } else {
-                    FileUtils.deleteFile(book.bookUrl)
+                    FileUtils.delete(book.bookUrl)
                 }
             }
         }

+ 1 - 1
app/src/main/java/io/legado/app/service/HttpReadAloudService.kt

@@ -264,7 +264,7 @@ class HttpReadAloudService : BaseReadAloudService(),
         val titleMd5 = MD5Utils.md5Encode16(textChapter?.title ?: "")
         FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach {
             if (!it.name.startsWith(titleMd5) && Date().time - it.lastModified() > 600000) {
-                FileUtils.deleteFile(it.absolutePath)
+                FileUtils.delete(it.absolutePath)
             }
         }
     }

+ 3 - 3
app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt

@@ -231,10 +231,10 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
         execute {
             val exportFiles = arrayListOf<File>()
             val configDirPath = FileUtils.getPath(requireContext().externalCache, "readConfig")
-            FileUtils.deleteFile(configDirPath)
+            FileUtils.delete(configDirPath)
             val configDir = FileUtils.createFolderIfNotExist(configDirPath)
             val configExportPath = FileUtils.getPath(configDir, "readConfig.json")
-            FileUtils.deleteFile(configExportPath)
+            FileUtils.delete(configExportPath)
             val configExportFile = FileUtils.createFileIfNotExist(configExportPath)
             configExportFile.writeText(GSON.toJson(ReadBookConfig.getExportConfig()))
             exportFiles.add(configExportFile)
@@ -285,7 +285,7 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
                     }
                 } else {
                     val exportPath = FileUtils.getPath(File(uri.path!!), exportFileName)
-                    FileUtils.deleteFile(exportPath)
+                    FileUtils.delete(exportPath)
                     FileUtils.createFileIfNotExist(exportPath)
                         .writeBytes(File(configZipPath).readBytes())
                 }

+ 1 - 1
app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt

@@ -186,7 +186,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
         ) {
             okButton {
                 BookHelp.clearCache()
-                FileUtils.deleteFile(requireActivity().cacheDir.absolutePath)
+                FileUtils.delete(requireActivity().cacheDir.absolutePath)
                 toastOnUi(R.string.clear_cache_success)
             }
             noButton()

+ 1 - 18
app/src/main/java/io/legado/app/utils/FileUtils.kt

@@ -90,23 +90,6 @@ object FileUtils {
         return path.toString()
     }
 
-    //递归删除文件夹下的数据
-    @Synchronized
-    fun deleteFile(filePath: String) {
-        val file = File(filePath)
-        if (!file.exists()) return
-
-        if (file.isDirectory) {
-            val files = file.listFiles()
-            files?.forEach { subFile ->
-                val path = subFile.path
-                deleteFile(path)
-            }
-        }
-        //删除文件
-        file.delete()
-    }
-
     fun getCachePath(): String {
         return appCtx.externalCache.absolutePath
     }
@@ -360,7 +343,7 @@ object FileUtils {
      * 删除文件或目录
      */
     @JvmOverloads
-    fun delete(path: String, deleteRootDir: Boolean = false): Boolean {
+    fun delete(path: String, deleteRootDir: Boolean = true): Boolean {
         val file = File(path)
 
         return if (file.exists()) {