From bbe0ab1dd0b51bcb91ff0a7f78555b078d73201e Mon Sep 17 00:00:00 2001 From: Two-Ai <81279822+Two-Ai@users.noreply.github.com> Date: Fri, 5 May 2023 22:17:51 -0400 Subject: [PATCH] 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. --- .../tachiyomi/data/download/Downloader.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 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 69ae458310..5075f9ae15 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 @@ -338,21 +338,21 @@ class Downloader( download.status = Download.State.DOWNLOADING - // Get all the URLs to the source images, fetch pages if necessary - pageList.filter { it.imageUrl.isNullOrEmpty() }.forEach { page -> - page.status = Page.State.LOAD_PAGE - try { - page.imageUrl = download.source.fetchImageUrl(page).awaitSingle() - } catch (e: Throwable) { - page.status = Page.State.ERROR - } - } - // Start downloading images, consider we can have downloaded images already // 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 + try { + page.imageUrl = download.source.fetchImageUrl(page).awaitSingle() + } catch (e: Throwable) { + page.status = Page.State.ERROR + } + } + withIOContext { getOrDownloadImage(page, download, tmpDir) } emit(page) }.flowOn(Dispatchers.IO)