mirror of
https://git.mihon.tech/mihonapp/mihon
synced 2024-11-23 05:35:51 +03:00
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.
This commit is contained in:
parent
cb2d43c0d1
commit
bbe0ab1dd0
1 changed files with 10 additions and 10 deletions
|
@ -338,21 +338,21 @@ class Downloader(
|
||||||
|
|
||||||
download.status = Download.State.DOWNLOADING
|
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
|
// Start downloading images, consider we can have downloaded images already
|
||||||
// Concurrently do 2 pages at a time
|
// Concurrently do 2 pages at a time
|
||||||
pageList.asFlow()
|
pageList.asFlow()
|
||||||
.flatMapMerge(concurrency = 2) { page ->
|
.flatMapMerge(concurrency = 2) { page ->
|
||||||
flow {
|
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) }
|
withIOContext { getOrDownloadImage(page, download, tmpDir) }
|
||||||
emit(page)
|
emit(page)
|
||||||
}.flowOn(Dispatchers.IO)
|
}.flowOn(Dispatchers.IO)
|
||||||
|
|
Loading…
Reference in a new issue