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
This commit is contained in:
S97 2022-05-12 05:36:16 +03:00 committed by GitHub
parent b26daf8824
commit f75d632740
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}
}