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 const val cacheFolderName = "book_cache"
private const val cacheImageFolderName = "images"
private const val cacheEpubFolderName = "epub"
private val downloadImages = CopyOnWriteArraySet<String>()
fun clearCache() {
@ -55,15 +56,24 @@ object BookHelp {
*/
suspend fun clearInvalidCache() {
withContext(IO) {
val bookFolderNames = appDb.bookDao.all.map {
it.getFolderName()
}
val file = downloadDir.getFile(cacheFolderName)
file.listFiles()?.forEach { bookFile ->
val bookFolderNames = ArrayList<String>()
val originNames = ArrayList<String>()
appDb.bookDao.all.forEach {
bookFolderNames.add(it.getFolderName())
if (it.isEpub()) originNames.add(it.originName)
}
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("文件不存在")

Loading…
Cancel
Save