From 13934582a444b5526f34002cc9537d80a5e553d7 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 31 Aug 2020 10:24:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 47 +++++++++++-------- .../java/io/legado/app/help/ReadBookConfig.kt | 2 +- .../io/legado/app/help/storage/Restore.kt | 2 +- .../app/model/localBook/AnalyzeTxtFile.kt | 4 +- .../io/legado/app/model/localBook/EPUBFile.kt | 4 +- .../legado/app/model/localBook/LocalBook.kt | 4 +- .../io/legado/app/ui/main/MainViewModel.kt | 3 +- .../java/io/legado/app/utils/FileUtils.kt | 27 +++++------ .../main/java/io/legado/app/utils/LogUtils.kt | 2 +- 9 files changed, 50 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 68b9f37b9..b6822303c 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -34,13 +34,12 @@ object BookHelp { fun clearCache() { FileUtils.deleteFile( - FileUtils.getPath(root = downloadDir, subDirs = arrayOf(cacheFolderName)) + FileUtils.getPath(downloadDir, cacheFolderName) ) } fun clearCache(book: Book) { - val filePath = FileUtils.getPath(root = downloadDir, - subDirs = arrayOf(cacheFolderName, book.getFolderName())) + val filePath = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName()) FileUtils.deleteFile(filePath) } @@ -67,8 +66,9 @@ object BookHelp { //保存文本 FileUtils.createFileIfNotExist( downloadDir, + cacheFolderName, + book.getFolderName(), formatChapterName(bookChapter), - subDirs = arrayOf(cacheFolderName, book.getFolderName()) ).writeText(content) //保存图片 content.split("\n").forEach { @@ -78,19 +78,21 @@ object BookHelp { src = NetworkUtils.getAbsoluteURL(bookChapter.url, src) src?.let { saveImage(book, src) - } - } + } + } } postEvent(EventBus.SAVE_CONTENT, bookChapter) } - - fun saveImage(book: Book, src:String) { + + fun saveImage(book: Book, src: String) { val analyzeUrl = AnalyzeUrl(src) analyzeUrl.getImageBytes(book.origin)?.let { FileUtils.createFileIfNotExist( downloadDir, - "${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}", - subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName) + cacheFolderName, + book.getFolderName(), + cacheImageFolderName, + "${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}" ).writeBytes(it) } } @@ -98,8 +100,10 @@ object BookHelp { fun getImage(book: Book, src: String): File { return FileUtils.getFile( downloadDir, - "${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}", - subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName) + cacheFolderName, + book.getFolderName(), + cacheImageFolderName, + "${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}" ) } @@ -131,8 +135,9 @@ object BookHelp { } else { FileUtils.exists( downloadDir, - formatChapterName(bookChapter), - subDirs = arrayOf(cacheFolderName, book.getFolderName()) + cacheFolderName, + book.getFolderName(), + formatChapterName(bookChapter) ) } } @@ -143,8 +148,9 @@ object BookHelp { } else { val file = FileUtils.getFile( downloadDir, - formatChapterName(bookChapter), - subDirs = arrayOf(cacheFolderName, book.getFolderName()) + cacheFolderName, + book.getFolderName(), + formatChapterName(bookChapter) ) if (file.exists()) { return file.readText() @@ -159,8 +165,9 @@ object BookHelp { } else { FileUtils.createFileIfNotExist( downloadDir, - formatChapterName(bookChapter), - subDirs = arrayOf(cacheFolderName, book.getFolderName()) + cacheFolderName, + book.getFolderName(), + formatChapterName(bookChapter) ).delete() } } @@ -183,7 +190,7 @@ object BookHelp { fun getDurChapterIndexByChapterTitle( title: String?, index: Int, - chapters: List + chapters: List, ): Int { if (title.isNullOrEmpty()) { return min(index, chapters.lastIndex) @@ -253,7 +260,7 @@ object BookHelp { name: String, origin: String?, content: String, - enableReplace: Boolean + enableReplace: Boolean, ): List { var c = content if (enableReplace) { diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index abbc1c2e8..c6c71598c 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -19,7 +19,7 @@ import java.io.File @Keep object ReadBookConfig { const val readConfigFileName = "readConfig.json" - private val configFilePath = FileUtils.getPath(readConfigFileName, App.INSTANCE.filesDir) + private val configFilePath = FileUtils.getPath(App.INSTANCE.filesDir, readConfigFileName) val configList: ArrayList = arrayListOf() private val defaultConfigs by lazy { val json = String(App.INSTANCE.assets.open(readConfigFileName).readBytes()) diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 0dbfeef01..5deeac5dd 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -27,7 +27,7 @@ import org.jetbrains.anko.toast import java.io.File object Restore { - private val ignoreConfigPath = FileUtils.getPath("restoreIgnore.json", App.INSTANCE.filesDir) + private val ignoreConfigPath = FileUtils.getPath(App.INSTANCE.filesDir, "restoreIgnore.json") val ignoreConfig: HashMap by lazy { val file = FileUtils.createFileIfNotExist(ignoreConfigPath) val json = file.readText() diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt index 41caca4a9..b7f1b015e 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt @@ -250,7 +250,7 @@ class AnalyzeTxtFile { val rootFile = App.INSTANCE.getExternalFilesDir(null) ?: App.INSTANCE.externalCacheDir ?: App.INSTANCE.cacheDir - FileUtils.createFolderIfNotExist(rootFile, subDirs = arrayOf(folderName)) + FileUtils.createFolderIfNotExist(rootFile, folderName) } fun getContent(book: Book, bookChapter: BookChapter): String { @@ -266,7 +266,7 @@ class AnalyzeTxtFile { private fun getBookFile(book: Book): File { if (book.bookUrl.isContentPath()) { val uri = Uri.parse(book.bookUrl) - val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = arrayOf()) + val bookFile = FileUtils.getFile(cacheFolder, book.originName) if (!bookFile.exists()) { bookFile.createNewFile() DocumentUtils.readBytes(App.INSTANCE, uri)?.let { diff --git a/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt b/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt index 9047e3fea..067837f17 100644 --- a/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt @@ -66,9 +66,9 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) { epubBook = epubReader.readEpub(inputStream) if (book.coverUrl.isNullOrEmpty()) { book.coverUrl = FileUtils.getPath( - "${MD5Utils.md5Encode16(book.bookUrl)}.jpg", App.INSTANCE.externalFilesDir, - "covers" + "covers", + "${MD5Utils.md5Encode16(book.bookUrl)}.jpg" ) } if (!File(book.coverUrl!!).exists()) { diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index dc53a3f0c..1af6f027e 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -61,9 +61,9 @@ object LocalBook { author = author, originName = fileName, coverUrl = FileUtils.getPath( - "${MD5Utils.md5Encode16(path)}.jpg", App.INSTANCE.externalFilesDir, - "covers" + "covers", + "${MD5Utils.md5Encode16(path)}.jpg" ) ) App.db.bookDao().insert(book) diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index ef6155fde..c9fc9066a 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -131,8 +131,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { fun postLoad() { execute { - FileUtils.deleteFile(FileUtils.getPath(root = context.cacheDir, - subDirs = arrayOf("Fonts"))) + FileUtils.deleteFile(FileUtils.getPath(context.cacheDir, "Fonts")) if (App.db.httpTTSDao().count == 0) { DefaultValueHelp.initHttpTTS() } diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 06616b4bd..e84e6c9b7 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -8,17 +8,17 @@ import java.io.IOException @Suppress("unused") object FileUtils { - fun exists(root: File, fileName: String, vararg subDirs: String): Boolean { - return getFile(root, fileName, subDirs = subDirs).exists() + fun exists(root: File, vararg subDirFiles: String): Boolean { + return getFile(root, *subDirFiles).exists() } - fun createFileIfNotExist(root: File, fileName: String, vararg subDirs: String): File { - val filePath = getPath(fileName, root, *subDirs) + fun createFileIfNotExist(root: File, vararg subDirFiles: String): File { + val filePath = getPath(root, *subDirFiles) return createFileIfNotExist(filePath) } fun createFolderIfNotExist(root: File, vararg subDirs: String): File { - val filePath = getPath(root = root, subDirs = subDirs) + val filePath = getPath(root, *subDirs) return createFolderIfNotExist(filePath) } @@ -49,23 +49,22 @@ object FileUtils { return file } - fun getFile(root: File, fileName: String, vararg subDirs: String): File { - val filePath = getPath(fileName, root, *subDirs) + fun getFile(root: File, vararg subDirFiles: String): File { + val filePath = getPath(root, *subDirFiles) return File(filePath) } fun getDirFile(root: File, vararg subDirs: String): File { - val filePath = getPath(root = root, subDirs = subDirs) + val filePath = getPath(root = root, *subDirs) return File(filePath) } - fun getPath(fileName: String? = null, root: File, vararg subDirs: String): String { + fun getPath(root: File, vararg subDirFiles: String): String { val path = StringBuilder(root.absolutePath) - subDirs.forEach { - path.append(File.separator).append(it) - } - if (!fileName.isNullOrEmpty()) { - path.append(File.separator).append(fileName) + subDirFiles.forEach { + if (it.isNotEmpty()) { + path.append(File.separator).append(it) + } } return path.toString() } diff --git a/app/src/main/java/io/legado/app/utils/LogUtils.kt b/app/src/main/java/io/legado/app/utils/LogUtils.kt index 8a901ac6f..a96bc7c75 100644 --- a/app/src/main/java/io/legado/app/utils/LogUtils.kt +++ b/app/src/main/java/io/legado/app/utils/LogUtils.kt @@ -32,7 +32,7 @@ object LogUtils { private val fileHandler by lazy { val root = App.INSTANCE.externalCacheDir ?: return@lazy null val logFolder = FileUtils.createFolderIfNotExist(root, "logs") - val logPath = FileUtils.getPath(root = logFolder, subDirs = arrayOf("appLog")) + val logPath = FileUtils.getPath(root = logFolder, "appLog") FileHandler(logPath, 10240, 10).apply { formatter = object : Formatter() { override fun format(record: LogRecord): String {