From a5c9411f29a91e7cb266be544cc5067e9857d7e7 Mon Sep 17 00:00:00 2001 From: Carlos <2092019+CarlosEsco@users.noreply.github.com> Date: Sun, 24 May 2020 12:30:06 -0400 Subject: [PATCH] switch to check folders for downloads whenever delete chapters is called so the manga folder doesnt get deleted by accident --- .../tachiyomi/data/download/DownloadCache.kt | 53 ++++++++++++------- .../data/download/DownloadManager.kt | 10 ++-- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index 5444e20ba1..0667c66b4e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -48,8 +48,8 @@ class DownloadCache( init { preferences.downloadsDirectory().asObservable().skip(1).subscribe { - lastRenew = 0L // invalidate cache - } + lastRenew = 0L // invalidate cache + } } /** @@ -88,11 +88,24 @@ class DownloadCache( * * @param manga the manga to check. */ - fun getDownloadCount(manga: Manga): Int { + fun getDownloadCount(manga: Manga, forceCheckFolder: Boolean = false): Int { checkRenew() - val files = mangaFiles[manga.id] ?: return 0 - return files.filter { !it.endsWith(Downloader.TMP_DIR_SUFFIX) }.size + if (forceCheckFolder) { + + val mangaDir = provider.findMangaDir(manga, sourceManager.getMangadex()) + + if (mangaDir != null) { + val listFiles = mangaDir.listFiles { dir, filename -> !filename.endsWith(Downloader.TMP_DIR_SUFFIX) } + if (!listFiles.isNullOrEmpty()) { + return listFiles.size + } + } + return 0 + } else { + val files = mangaFiles[manga.id] ?: return 0 + return files.filter { !it.endsWith(Downloader.TMP_DIR_SUFFIX) }.size + } } /** @@ -126,9 +139,9 @@ class DownloadCache( sourceMangasRaw.filterNot { it.favorite }) val sourceDir = sourceValue.value val mangaDirs = sourceDir.dir.listFiles().orEmpty().mapNotNull { - val name = it.name ?: return@mapNotNull null - name to MangaDirectory(it) - }.toMap() + val name = it.name ?: return@mapNotNull null + name to MangaDirectory(it) + }.toMap() mangaDirs.values.forEach { mangaDir -> val chapterDirs = @@ -200,18 +213,18 @@ class DownloadCache( } } - /*fun renameFolder(from: String, to: String, source: Long) { - val sourceDir = rootDir.files[source] ?: return - val list = sourceDir.files.toMutableMap() - val mangaFiles = sourceDir.files[DiskUtil.buildValidFilename(from)] ?: return - val newFile = UniFile.fromFile(File(sourceDir.dir.filePath + "/" + DiskUtil - .buildValidFilename(to))) ?: return - val newDir = MangaDirectory(newFile) - newDir.files = mangaFiles.files - list.remove(DiskUtil.buildValidFilename(from)) - list[to] = newDir - sourceDir.files = list - }*/ +/*fun renameFolder(from: String, to: String, source: Long) { + val sourceDir = rootDir.files[source] ?: return + val list = sourceDir.files.toMutableMap() + val mangaFiles = sourceDir.files[DiskUtil.buildValidFilename(from)] ?: return + val newFile = UniFile.fromFile(File(sourceDir.dir.filePath + "/" + DiskUtil + .buildValidFilename(to))) ?: return + val newDir = MangaDirectory(newFile) + newDir.files = mangaFiles.files + list.remove(DiskUtil.buildValidFilename(from)) + list[to] = newDir + sourceDir.files = list +}*/ /** * Removes a manga that has been deleted from this cache. diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index 6d44a03455..3f02669c31 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -169,16 +169,16 @@ class DownloadManager(val context: Context) { private fun buildPageList(chapterDir: UniFile?): Observable> { return Observable.fromCallable { val files = chapterDir?.listFiles().orEmpty() - .filter { "image" in it.type.orEmpty() } + .filter { "image" in it.type.orEmpty() } if (files.isEmpty()) { throw Exception("Page list is empty") } files.sortedBy { it.name } - .mapIndexed { i, file -> - Page(i, uri = file.uri).apply { status = Page.READY } - } + .mapIndexed { i, file -> + Page(i, uri = file.uri).apply { status = Page.READY } + } } } @@ -247,7 +247,7 @@ class DownloadManager(val context: Context) { val chapterDirs = provider.findChapterDirs(chapters, manga, source) + provider.findTempChapterDirs(chapters, manga, source) chapterDirs.forEach { it.delete() } cache.removeChapters(chapters, manga) - if (cache.getDownloadCount(manga) == 0) { // Delete manga directory if empty + if (cache.getDownloadCount(manga, true) == 0) { // Delete manga directory if empty chapterDirs.firstOrNull()?.parentFile?.delete() } }