From fe2271f357955df3a2a4f6a7897409b5cfa73a2c Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Sun, 4 Sep 2022 09:54:34 +0800 Subject: [PATCH] fix: change epub file copy position book.getFolderName returned value may change after function EpubFile.upBookInfo called, move a folder which is not related with bookName epub file copied will be deleted after BookHelp.clearInvalidCache called --- .../main/java/io/legado/app/help/BookHelp.kt | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 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 5bb49d90a..8fccb7723 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -31,6 +31,7 @@ object BookHelp { private val downloadDir: File = appCtx.externalFiles private const val cacheFolderName = "book_cache" private const val cacheImageFolderName = "images" + private const val cacheEpubFolderName = "epub" private val downloadImages = CopyOnWriteArraySet() fun clearCache() { @@ -55,15 +56,24 @@ object BookHelp { */ suspend fun clearInvalidCache() { withContext(IO) { - val bookFolderNames = appDb.bookDao.all.map { - it.getFolderName() + val bookFolderNames = ArrayList() + val originNames = ArrayList() + appDb.bookDao.all.forEach { + bookFolderNames.add(it.getFolderName()) + if (it.isEpub()) originNames.add(it.originName) } - val file = downloadDir.getFile(cacheFolderName) - file.listFiles()?.forEach { bookFile -> - if (!bookFolderNames.contains(bookFile.name)) { - FileUtils.delete(bookFile.absolutePath) + downloadDir.getFile(cacheFolderName) + .listFiles()?.forEach { bookFile -> + if (!bookFolderNames.contains(bookFile.name)) { + FileUtils.delete(bookFile.absolutePath) + } + } + downloadDir.getFile(cacheEpubFolderName) + .listFiles()?.forEach { epubFile -> + if (!originNames.contains(epubFile.name)) { + FileUtils.delete(epubFile.absolutePath) + } } - } } } @@ -165,7 +175,8 @@ object BookHelp { fun getEpubFile(book: Book): ZipFile { val uri = Uri.parse(book.bookUrl) if (uri.isContentScheme()) { - val path = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName(), book.originName) + FileUtils.createFolderIfNotExist(downloadDir, cacheEpubFolderName) + val path = FileUtils.getPath(downloadDir, cacheEpubFolderName, book.originName) val file = File(path) val doc = DocumentFile.fromSingleUri(appCtx, uri) ?: throw IOException("文件不存在")