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
pull/2237/head
Xwite 2 years ago
parent 19e7b58dd8
commit fe2271f357
  1. 23
      app/src/main/java/io/legado/app/help/BookHelp.kt

@ -31,6 +31,7 @@ object BookHelp {
private val downloadDir: File = appCtx.externalFiles private val downloadDir: File = appCtx.externalFiles
private const val cacheFolderName = "book_cache" private const val cacheFolderName = "book_cache"
private const val cacheImageFolderName = "images" private const val cacheImageFolderName = "images"
private const val cacheEpubFolderName = "epub"
private val downloadImages = CopyOnWriteArraySet<String>() private val downloadImages = CopyOnWriteArraySet<String>()
fun clearCache() { fun clearCache() {
@ -55,15 +56,24 @@ object BookHelp {
*/ */
suspend fun clearInvalidCache() { suspend fun clearInvalidCache() {
withContext(IO) { withContext(IO) {
val bookFolderNames = appDb.bookDao.all.map { val bookFolderNames = ArrayList<String>()
it.getFolderName() val originNames = ArrayList<String>()
} appDb.bookDao.all.forEach {
val file = downloadDir.getFile(cacheFolderName) bookFolderNames.add(it.getFolderName())
file.listFiles()?.forEach { bookFile -> if (it.isEpub()) originNames.add(it.originName)
}
downloadDir.getFile(cacheFolderName)
.listFiles()?.forEach { bookFile ->
if (!bookFolderNames.contains(bookFile.name)) { if (!bookFolderNames.contains(bookFile.name)) {
FileUtils.delete(bookFile.absolutePath) 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 { fun getEpubFile(book: Book): ZipFile {
val uri = Uri.parse(book.bookUrl) val uri = Uri.parse(book.bookUrl)
if (uri.isContentScheme()) { 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 file = File(path)
val doc = DocumentFile.fromSingleUri(appCtx, uri) val doc = DocumentFile.fromSingleUri(appCtx, uri)
?: throw IOException("文件不存在") ?: throw IOException("文件不存在")

Loading…
Cancel
Save