From 0347d3970a31d072fe58186b7dd35c1aed1c9270 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Wed, 23 Nov 2022 20:23:29 +0600 Subject: [PATCH] Cleanup [Downloader.ensureSuccessfulDownload] (#8602) --- .../tachiyomi/data/download/Downloader.kt | 41 ++++++++++++------- .../kanade/tachiyomi/util/storage/DiskUtil.kt | 6 ++- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index 04ddf20114..d0c43a6ad4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -28,6 +28,7 @@ import eu.kanade.tachiyomi.util.lang.launchNow import eu.kanade.tachiyomi.util.lang.plusAssign import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.storage.DiskUtil +import eu.kanade.tachiyomi.util.storage.DiskUtil.NOMEDIA_FILE import eu.kanade.tachiyomi.util.storage.saveTo import eu.kanade.tachiyomi.util.system.ImageUtil import eu.kanade.tachiyomi.util.system.logcat @@ -389,7 +390,7 @@ class Downloader( tmpFile?.delete() // Try to find the image file. - val imageFile = tmpDir.listFiles()!!.find { it.name!!.startsWith("$filename.") || it.name!!.contains("${filename}__001") } + val imageFile = tmpDir.listFiles()?.firstOrNull { it.name!!.startsWith("$filename.") || it.name!!.startsWith("${filename}__001") } // If the image is already downloaded, do nothing. Otherwise download from network val pageObservable = when { @@ -496,8 +497,8 @@ class Downloader( val imageFile = tmpDir.listFiles()?.firstOrNull { it.name.orEmpty().startsWith(filenamePrefix) } ?: throw Error(context.getString(R.string.download_notifier_split_page_not_found, page.number)) - // check if the original page was previously splitted before then skip. - if (imageFile.name!!.contains("__")) return true + // Check if the original page was previously splitted before then skip. + if (imageFile.name.orEmpty().startsWith("${filenamePrefix}__")) return true return try { ImageUtil.splitTallImage(tmpDir, imageFile, filenamePrefix) @@ -521,18 +522,30 @@ class Downloader( tmpDir: UniFile, dirname: String, ) { - // Ensure that the chapter folder has all the images. - val downloadedImages = tmpDir.listFiles().orEmpty().filterNot { it.name!!.endsWith(".tmp") || (it.name!!.contains("__") && !it.name!!.contains("__001.jpg")) } + // Page list hasn't been initialized + val downloadPageCount = download.pages?.size ?: return + // Ensure that all pages has been downloaded + if (download.downloadedImages < downloadPageCount) return + // Ensure that the chapter folder has all the pages. + val downloadedImagesCount = tmpDir.listFiles().orEmpty().count { + val fileName = it.name.orEmpty() + when { + fileName in listOf(COMIC_INFO_FILE, NOMEDIA_FILE) -> false + fileName.endsWith(".tmp") -> false + // Only count the first split page and not the others + fileName.contains("__") && !fileName.endsWith("__001.jpg") -> false + else -> true + } + } - val chapterUrl = download.source.getChapterUrl(download.chapter) - createComicInfoFile( - tmpDir, - download.manga, - download.chapter.toDomainChapter()!!, - chapterUrl, - ) - - download.status = if (downloadedImages.size == download.pages!!.size) { + download.status = if (downloadedImagesCount == downloadPageCount) { + val chapterUrl = download.source.getChapterUrl(download.chapter) + createComicInfoFile( + tmpDir, + download.manga, + download.chapter.toDomainChapter()!!, + chapterUrl, + ) // Only rename the directory if it's downloaded. if (downloadPreferences.saveChaptersAsCBZ().get()) { archiveChapter(mangaDir, dirname, tmpDir) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt index 50a5099e90..8e3141a277 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/storage/DiskUtil.kt @@ -62,9 +62,9 @@ object DiskUtil { */ fun createNoMediaFile(dir: UniFile?, context: Context?) { if (dir != null && dir.exists()) { - val nomedia = dir.findFile(".nomedia") + val nomedia = dir.findFile(NOMEDIA_FILE) if (nomedia == null) { - dir.createFile(".nomedia") + dir.createFile(NOMEDIA_FILE) context?.let { scanMedia(it, dir.uri) } } } @@ -112,4 +112,6 @@ object DiskUtil { else -> true } } + + const val NOMEDIA_FILE = ".nomedia" }