From f75d63274089c5a0cfe0e1afde8703017b521870 Mon Sep 17 00:00:00 2001 From: S97 <39028181+saud-97@users.noreply.github.com> Date: Thu, 12 May 2022 05:36:16 +0300 Subject: [PATCH] Fixed splitting logic and CBZ compression logic (#7098) * Fixes: - spliiting fails when the page was already been split and processed before - Moved CBZ logic a little earlier to avoid marking a download as complete before the CBZ compression was completed * Added a single space for readablity * Added 2 spaces for readability * Moved the splitting logic to happen inside getOrDownloadImage() * Minor cleanup * - Improved error handling when splitting fails due to OOM exception caused by BitmapFactory.decodeFile. - Changed logic from throwing error to only notify to allow the download to complete even if splitting failed. * reverted auto formatting changes * removed an extra loop * Merged to Upstream, cleaned up * Removed unused localized string * Minor cleanup --- .../tachiyomi/data/download/Downloader.kt | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 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 7e62a35a1..fc18b9f27 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 @@ -274,7 +274,7 @@ class Downloader( // Start downloader if needed if (autoStart && wasEmpty) { - val queuedDownloads = queue.filter { it.source !is UnmeteredSource }.count() + val queuedDownloads = queue.count { it.source !is UnmeteredSource } val maxDownloadsFromSource = queue .groupBy { it.source } .filterKeys { it !is UnmeteredSource } @@ -345,10 +345,7 @@ class Downloader( .flatMap({ page -> getOrDownloadImage(page, download, tmpDir) }, 5) .onBackpressureLatest() // Do when page is downloaded. - .doOnNext { page -> - splitTallImageIfNeeded(page, tmpDir) - notifier.onProgressChange(download) - } + .doOnNext { notifier.onProgressChange(download) } .toList() .map { download } // Do after download completes @@ -393,8 +390,9 @@ class Downloader( } return pageObservable - // When the image is ready, set image path, progress (just in case) and status + // When the page is ready, set page path, progress (just in case) and status .doOnNext { file -> + splitTallImageIfNeeded(page, tmpDir) page.uri = file.uri page.progress = 100 download.downloadedImages++ @@ -483,11 +481,13 @@ class Downloader( if (!preferences.splitTallImages().get()) return val filename = String.format("%03d", page.number) - val imageFile = tmpDir.listFiles()?.find { it.name!!.startsWith("$filename.") } + val imageFile = tmpDir.listFiles()?.find { it.name!!.startsWith(filename) } ?: throw Error(context.getString(R.string.download_notifier_split_page_not_found, page.number)) val imageFilePath = imageFile.filePath ?: throw Error(context.getString(R.string.download_notifier_split_page_path_not_found, page.number)) + // check if the original page was previously splitted before then skip. + if (imageFile.name!!.contains("__")) return ImageUtil.splitTallImage(imageFile, imageFilePath) } @@ -509,13 +509,7 @@ class Downloader( val downloadedImages = tmpDir.listFiles().orEmpty().filterNot { it.name!!.endsWith(".tmp") || (it.name!!.contains("__") && !it.name!!.contains("__001.jpg")) } download.status = if (downloadedImages.size == download.pages!!.size) { - Download.State.DOWNLOADED - } else { - Download.State.ERROR - } - - // Only rename the directory if it's downloaded. - if (download.status == Download.State.DOWNLOADED) { + // Only rename the directory if it's downloaded. if (preferences.saveChaptersAsCBZ().get()) { archiveChapter(mangaDir, dirname, tmpDir) } else { @@ -524,6 +518,10 @@ class Downloader( cache.addChapter(dirname, mangaDir, download.manga) DiskUtil.createNoMediaFile(tmpDir, context) + + Download.State.DOWNLOADED + } else { + Download.State.ERROR } }