kunfei 3 年之前
父節點
當前提交
91ac997608

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

@@ -220,12 +220,11 @@ interface JsExtensions : JsEncodeUtils {
         val analyzeUrl = AnalyzeUrl(url, source = getSource())
         val analyzeUrl = AnalyzeUrl(url, source = getSource())
         val type = analyzeUrl.type ?: "zip"
         val type = analyzeUrl.type ?: "zip"
         val path = FileUtils.getPath(
         val path = FileUtils.getPath(
-            FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
+            File(FileUtils.getCachePath()),
             "${MD5Utils.md5Encode16(url)}.${type}"
             "${MD5Utils.md5Encode16(url)}.${type}"
         )
         )
-        FileUtils.delete(path)
+        val file = File(path).createFileReplace()
         analyzeUrl.getInputStream().use { iStream ->
         analyzeUrl.getInputStream().use { iStream ->
-            val file = FileUtils.createFileIfNotExist(path)
             FileOutputStream(file).use { oStream ->
             FileOutputStream(file).use { oStream ->
                 iStream.copyTo(oStream)
                 iStream.copyTo(oStream)
             }
             }
@@ -246,8 +245,8 @@ interface JsExtensions : JsEncodeUtils {
             FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
             FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
             "${MD5Utils.md5Encode16(url)}.${type}"
             "${MD5Utils.md5Encode16(url)}.${type}"
         )
         )
-        FileUtils.delete(path)
-        val file = FileUtils.createFileIfNotExist(path)
+        val file = File(path)
+        file.createFileReplace()
         HexUtil.decodeHex(content).let {
         HexUtil.decodeHex(content).let {
             if (it.isNotEmpty()) {
             if (it.isNotEmpty()) {
                 file.writeBytes(it)
                 file.writeBytes(it)
@@ -492,9 +491,8 @@ interface JsExtensions : JsEncodeUtils {
             FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
             FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
             FileUtils.getNameExcludeExtension(zipPath)
             FileUtils.getNameExcludeExtension(zipPath)
         )
         )
-        FileUtils.delete(unzipPath)
+        val unzipFolder = File(unzipPath).createFolderReplace()
         val zipFile = getFile(zipPath)
         val zipFile = getFile(zipPath)
-        val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath)
         ZipUtils.unzipFile(zipFile, unzipFolder)
         ZipUtils.unzipFile(zipFile, unzipFolder)
         FileUtils.delete(zipFile.absolutePath)
         FileUtils.delete(zipFile.absolutePath)
         return unzipPath.substring(FileUtils.getCachePath().length)
         return unzipPath.substring(FileUtils.getCachePath().length)

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

@@ -386,11 +386,10 @@ object ReadBookConfig {
                 FileUtils.delete(configZipPath)
                 FileUtils.delete(configZipPath)
                 val zipFile = FileUtils.createFileIfNotExist(configZipPath)
                 val zipFile = FileUtils.createFileIfNotExist(configZipPath)
                 zipFile.writeBytes(byteArray)
                 zipFile.writeBytes(byteArray)
-                val configDirPath = FileUtils.getPath(appCtx.externalCache, "readConfig")
-                FileUtils.delete(configDirPath)
+                val configDir = appCtx.externalCache.getFile("readConfig")
+                configDir.createFolderReplace()
                 @Suppress("BlockingMethodInNonBlockingContext")
                 @Suppress("BlockingMethodInNonBlockingContext")
-                ZipUtils.unzipFile(zipFile, FileUtils.createFolderIfNotExist(configDirPath))
-                val configDir = FileUtils.createFolderIfNotExist(configDirPath)
+                ZipUtils.unzipFile(zipFile, configDir)
                 val configFile = configDir.getFile(configFileName)
                 val configFile = configDir.getFile(configFileName)
                 val config: Config = GSON.fromJsonObject<Config>(configFile.readText()).getOrThrow()
                 val config: Config = GSON.fromJsonObject<Config>(configFile.readText()).getOrThrow()
                     ?: throw NoStackTraceException("排版配置格式错误")
                     ?: throw NoStackTraceException("排版配置格式错误")

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

@@ -27,9 +27,7 @@ import java.util.concurrent.TimeUnit
 object Backup {
 object Backup {
 
 
     val backupPath: String by lazy {
     val backupPath: String by lazy {
-        val path = appCtx.filesDir.getFile("backup").absolutePath
-        FileUtils.createFolderIfNotExist(path)
-        path
+        appCtx.filesDir.getFile("backup").createFolderIfNotExist().absolutePath
     }
     }
 
 
     val backupFileNames by lazy {
     val backupFileNames by lazy {

+ 14 - 2
app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt

@@ -93,6 +93,18 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
         }.getOrDefault("${book.name} 作者:${book.getRealAuthor()}")
         }.getOrDefault("${book.name} 作者:${book.getRealAuthor()}")
     }
     }
 
 
+    fun exportFileExist(path: String, book: Book): Boolean {
+        val fileName = getExportFileName(book)
+        return if (path.isContentScheme()) {
+            val uri = Uri.parse(path)
+            val doc = DocumentFile.fromTreeUri(context, uri) ?: return false
+            doc.findFile(fileName) ?: return false
+            return true
+        } else {
+            File(path).exists(fileName)
+        }
+    }
+
     fun export(path: String, book: Book) {
     fun export(path: String, book: Book) {
         if (exportProgress.contains(book.bookUrl)) return
         if (exportProgress.contains(book.bookUrl)) return
         exportProgress[book.bookUrl] = 0
         exportProgress[book.bookUrl] = 0
@@ -111,7 +123,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
                     ?: throw NoStackTraceException("获取导出文档失败")
                     ?: throw NoStackTraceException("获取导出文档失败")
                 export(doc, book)
                 export(doc, book)
             } else {
             } else {
-                export(FileUtils.createFolderIfNotExist(path), book)
+                export(File(path).createFolderIfNotExist(), book)
             }
             }
         }.onError {
         }.onError {
             exportProgress.remove(book.bookUrl)
             exportProgress.remove(book.bookUrl)
@@ -274,7 +286,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
                     ?: throw NoStackTraceException("获取导出文档失败")
                     ?: throw NoStackTraceException("获取导出文档失败")
                 exportEpub(doc, book)
                 exportEpub(doc, book)
             } else {
             } else {
-                exportEpub(FileUtils.createFolderIfNotExist(path), book)
+                exportEpub(File(path).createFolderIfNotExist(), book)
             }
             }
         }.onError {
         }.onError {
             exportProgress.remove(book.bookUrl)
             exportProgress.remove(book.bookUrl)

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

@@ -234,14 +234,12 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
         }
         }
         execute {
         execute {
             val exportFiles = arrayListOf<File>()
             val exportFiles = arrayListOf<File>()
-            val configDirPath = FileUtils.getPath(requireContext().externalCache, "readConfig")
-            FileUtils.delete(configDirPath)
-            val configDir = FileUtils.createFolderIfNotExist(configDirPath)
-            val configExportPath = FileUtils.getPath(configDir, "readConfig.json")
-            FileUtils.delete(configExportPath)
-            val configExportFile = FileUtils.createFileIfNotExist(configExportPath)
-            configExportFile.writeText(GSON.toJson(ReadBookConfig.getExportConfig()))
-            exportFiles.add(configExportFile)
+            val configDir = requireContext().externalCache.getFile("readConfig")
+            configDir.createFolderReplace()
+            val configFile = configDir.getFile("readConfig.json")
+            configFile.createFileReplace()
+            configFile.writeText(GSON.toJson(ReadBookConfig.getExportConfig()))
+            exportFiles.add(configFile)
             val fontPath = ReadBookConfig.textFont
             val fontPath = ReadBookConfig.textFont
             if (fontPath.isNotEmpty()) {
             if (fontPath.isNotEmpty()) {
                 val fontName = FileUtils.getName(fontPath)
                 val fontName = FileUtils.getName(fontPath)

+ 34 - 0
app/src/main/java/io/legado/app/utils/FileExtensions.kt

@@ -1,3 +1,5 @@
+@file:Suppress("unused")
+
 package io.legado.app.utils
 package io.legado.app.utils
 
 
 import android.net.Uri
 import android.net.Uri
@@ -31,4 +33,36 @@ fun File.listFileDocs(filter: FileDocFilter? = null): ArrayList<FileDoc> {
     return docList
     return docList
 }
 }
 
 
+fun File.createFileIfNotExist(): File {
+    if (!exists()) {
+        parentFile?.createFileIfNotExist()
+        createNewFile()
+    }
+    return this
+}
+
+fun File.createFileReplace(): File {
+    if (!exists()) {
+        parentFile?.createFileIfNotExist()
+        createNewFile()
+    } else {
+        delete()
+        createNewFile()
+    }
+    return this
+}
+
+fun File.createFolderIfNotExist(): File {
+    if (!exists()) {
+        mkdirs()
+    }
+    return this
+}
 
 
+fun File.createFolderReplace(): File {
+    if (exists()) {
+        FileUtils.delete(this, true)
+    }
+    mkdirs()
+    return this
+}