From 5e2f4ad06b97c38a354c9b6717af64ad98eecdef Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 28 Jan 2020 23:42:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 94 +++++++++++++------ .../java/io/legado/app/utils/DocumentUtils.kt | 5 + .../java/io/legado/app/utils/FileUtils.kt | 18 +++- 3 files changed, 86 insertions(+), 31 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 bce22a823..2c1330b12 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -52,67 +52,101 @@ object BookHelp { DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { DocumentUtils.createFileIfNotExist( it, - bookChapterName(bookChapter), + "${bookChapterName(bookChapter)}.nb", subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) - ) + )?.uri?.writeText(App.INSTANCE, content) } } else { - FileUtils.getFolder(getBookFolder(book)).listFiles()?.forEach { + FileUtils.createFileIfNotExist( + File(downloadPath), + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).listFiles()?.forEach { if (it.name.startsWith(String.format("%05d", bookChapter.index))) { it.delete() return@forEach } } - val filePath = getChapterPath(book, bookChapter) - val file = FileUtils.getFile(filePath) - file.writeText(content) + FileUtils.createFileIfNotExist( + File(downloadPath), + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).writeText(content) } } fun getChapterCount(book: Book): Int { - return FileUtils.getFolder(getBookFolder(book)).list()?.size ?: 0 + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + return DocumentUtils.createFileIfNotExist( + it, + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + )?.listFiles()?.size ?: 0 + } + } else { + return FileUtils.createFileIfNotExist( + File(downloadPath), + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).list()?.size ?: 0 + } + return 0 } fun hasContent(book: Book, bookChapter: BookChapter): Boolean { - val filePath = getChapterPath(book, bookChapter) - runCatching { - val file = File(filePath) - if (file.exists()) { - return true + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + return DocumentUtils.exists( + it, + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ) } + } else { + return FileUtils.exists( + File(downloadPath), + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ) } return false } fun getContent(book: Book, bookChapter: BookChapter): String? { - val filePath = getChapterPath(book, bookChapter) - runCatching { - val file = File(filePath) - if (file.exists()) { - return file.readText() + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + return DocumentUtils.createFileIfNotExist( + it, + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + )?.uri?.readText(App.INSTANCE) } + } else { + return FileUtils.createFileIfNotExist( + File(downloadPath), + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).readText() } return null } fun delContent(book: Book, bookChapter: BookChapter) { - val filePath = getChapterPath(book, bookChapter) - kotlin.runCatching { - val file = File(filePath) - if (file.exists()) { - file.delete() + if (downloadUri.isDocumentUri(App.INSTANCE)) { + DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { + DocumentUtils.createFileIfNotExist( + it, + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + )?.delete() } + } else { + FileUtils.createFileIfNotExist( + File(downloadPath), + "${bookChapterName(bookChapter)}.nb", + subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) + ).delete() } } - private fun getBookFolder(book: Book): String { - return "${getBookCachePath()}${File.separator}${bookFolderName(book)}" - } - - private fun getChapterPath(book: Book, bookChapter: BookChapter): String { - return "${getBookFolder(book)}${File.separator}${bookChapterName(bookChapter)}.nb" - } - private fun formatFolderName(folderName: String): String { return folderName.replace("[\\\\/:*?\"<>|.]".toRegex(), "") } diff --git a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt index 99cf03f5e..3d1f53a58 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -6,6 +6,11 @@ import androidx.documentfile.provider.DocumentFile object DocumentUtils { + fun exists(root: DocumentFile, fileName: String, vararg subDirs: String): Boolean { + val parent = getDirDocument(root, *subDirs) ?: return false + return parent.findFile(fileName)?.exists() ?: false + } + fun createFileIfNotExist( root: DocumentFile, fileName: String, 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 c6061acb6..3d9923448 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -13,12 +13,28 @@ import java.io.IOException object FileUtils { + fun exists(file: File, fileName: String, vararg subDirs: String): Boolean { + val filePath = + file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName + return File(filePath).exists() + } + + fun createFileIfNotExist(file: File, fileName: String, vararg subDirs: String): File { + val filePath = + file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName + return getFile(filePath) + } + + fun createFileIfNotExist(file: File, vararg subDirs: String): File { + val filePath = file.absolutePath + File.separator + subDirs.joinToString(File.separator) + return getFolder(filePath) + } + fun getCachePath(): String { return App.INSTANCE.externalCacheDir?.absolutePath ?: App.INSTANCE.cacheDir.absolutePath } - //获取文件夹 fun getFolder(filePath: String): File { val file = File(filePath)