diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 9e45725ca..185bb2004 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -220,12 +220,11 @@ interface JsExtensions : JsEncodeUtils { val analyzeUrl = AnalyzeUrl(url, source = getSource()) val type = analyzeUrl.type ?: "zip" val path = FileUtils.getPath( - FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), + File(FileUtils.getCachePath()), "${MD5Utils.md5Encode16(url)}.${type}" ) - FileUtils.delete(path) + val file = File(path).createFileReplace() analyzeUrl.getInputStream().use { iStream -> - val file = FileUtils.createFileIfNotExist(path) FileOutputStream(file).use { oStream -> iStream.copyTo(oStream) } @@ -246,8 +245,8 @@ interface JsExtensions : JsEncodeUtils { FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), "${MD5Utils.md5Encode16(url)}.${type}" ) - FileUtils.delete(path) - val file = FileUtils.createFileIfNotExist(path) + val file = File(path) + file.createFileReplace() HexUtil.decodeHex(content).let { if (it.isNotEmpty()) { file.writeBytes(it) @@ -492,9 +491,8 @@ interface JsExtensions : JsEncodeUtils { FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.getNameExcludeExtension(zipPath) ) - FileUtils.delete(unzipPath) + val unzipFolder = File(unzipPath).createFolderReplace() val zipFile = getFile(zipPath) - val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath) ZipUtils.unzipFile(zipFile, unzipFolder) FileUtils.delete(zipFile.absolutePath) return unzipPath.substring(FileUtils.getCachePath().length) diff --git a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt index 6e51f3d26..29f310937 100644 --- a/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt @@ -386,11 +386,10 @@ object ReadBookConfig { FileUtils.delete(configZipPath) val zipFile = FileUtils.createFileIfNotExist(configZipPath) zipFile.writeBytes(byteArray) - val configDirPath = FileUtils.getPath(appCtx.externalCache, "readConfig") - FileUtils.delete(configDirPath) + val configDir = appCtx.externalCache.getFile("readConfig") + configDir.createFolderReplace() @Suppress("BlockingMethodInNonBlockingContext") - ZipUtils.unzipFile(zipFile, FileUtils.createFolderIfNotExist(configDirPath)) - val configDir = FileUtils.createFolderIfNotExist(configDirPath) + ZipUtils.unzipFile(zipFile, configDir) val configFile = configDir.getFile(configFileName) val config: Config = GSON.fromJsonObject(configFile.readText()).getOrThrow() ?: throw NoStackTraceException("排版配置格式错误") diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index 10becc903..9fd831bb6 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -27,9 +27,7 @@ import java.util.concurrent.TimeUnit object Backup { 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 { diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index e4aeab359..4fb66681b 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/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()}") } + 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) { if (exportProgress.contains(book.bookUrl)) return exportProgress[book.bookUrl] = 0 @@ -111,7 +123,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { ?: throw NoStackTraceException("获取导出文档失败") export(doc, book) } else { - export(FileUtils.createFolderIfNotExist(path), book) + export(File(path).createFolderIfNotExist(), book) } }.onError { exportProgress.remove(book.bookUrl) @@ -274,7 +286,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { ?: throw NoStackTraceException("获取导出文档失败") exportEpub(doc, book) } else { - exportEpub(FileUtils.createFolderIfNotExist(path), book) + exportEpub(File(path).createFolderIfNotExist(), book) } }.onError { exportProgress.remove(book.bookUrl) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index ea953a40a..8c59f7c65 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/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 { val exportFiles = arrayListOf() - 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 if (fontPath.isNotEmpty()) { val fontName = FileUtils.getName(fontPath) diff --git a/app/src/main/java/io/legado/app/utils/FileExtensions.kt b/app/src/main/java/io/legado/app/utils/FileExtensions.kt index 8c2c07f3a..0fe815c2a 100644 --- a/app/src/main/java/io/legado/app/utils/FileExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/FileExtensions.kt @@ -1,3 +1,5 @@ +@file:Suppress("unused") + package io.legado.app.utils import android.net.Uri @@ -31,4 +33,36 @@ fun File.listFileDocs(filter: FileDocFilter? = null): ArrayList { 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 +} \ No newline at end of file