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

(cherry picked from commit f75d63274089c5a0cfe0e1afde8703017b521870)
This commit is contained in:
S97 2022-05-12 05:36:16 +03:00 committed by Jobobby04
parent 054e6b839e
commit 3a2ef3f134

View File

@ -276,7 +276,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 }
@ -351,10 +351,7 @@ class Downloader(
.flatMap({ page -> getOrDownloadImage(page, download, tmpDir, dataSaver) }, 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
@ -399,8 +396,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++
@ -489,11 +487,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)
}
@ -515,13 +515,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 {
@ -530,6 +524,10 @@ class Downloader(
cache.addChapter(dirname, mangaDir, download.manga)
DiskUtil.createNoMediaFile(tmpDir, context)
Download.State.DOWNLOADED
} else {
Download.State.ERROR
}
}