Fix delay between URL fetch and image download (#9452)

Fetch each source image URL immediately before downloading each image
instead of fetching all URLs and then downloading all images.

Source image URLs may change, so the downloader may fail if there is
too long a delay between fetching the image URL and downloading the
image.

(cherry picked from commit bbe0ab1dd0b51bcb91ff0a7f78555b078d73201e)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt
This commit is contained in:
Two-Ai 2023-05-05 22:17:51 -04:00 committed by Jobobby04
parent 20ee365bdc
commit 60cf10ff2e

View File

@ -349,8 +349,13 @@ class Downloader(
download.status = Download.State.DOWNLOADING download.status = Download.State.DOWNLOADING
// Get all the URLs to the source images, fetch pages if necessary // Start downloading images, consider we can have downloaded images already
pageList.filter { it.imageUrl.isNullOrEmpty() }.forEach { page -> // Concurrently do 2 pages at a time
pageList.asFlow()
.flatMapMerge(concurrency = 2) { page ->
flow {
// Fetch image URL if necessary
if (page.imageUrl.isNullOrEmpty()) {
page.status = Page.State.LOAD_PAGE page.status = Page.State.LOAD_PAGE
try { try {
page.imageUrl = download.source.fetchImageUrl(page).awaitSingle() page.imageUrl = download.source.fetchImageUrl(page).awaitSingle()
@ -359,11 +364,6 @@ class Downloader(
} }
} }
// Start downloading images, consider we can have downloaded images already
// Concurrently do 2 pages at a time
pageList.asFlow()
.flatMapMerge(concurrency = 2) { page ->
flow {
withIOContext { getOrDownloadImage(page, download, tmpDir, dataSaver) } withIOContext { getOrDownloadImage(page, download, tmpDir, dataSaver) }
emit(page) emit(page)
}.flowOn(Dispatchers.IO) }.flowOn(Dispatchers.IO)