Fix incorrect progress when resuming resumable image downloads (#3616)

This commit is contained in:
Mohannad
2026-07-21 14:46:57 +03:00
committed by GitHub
parent 47aa9190d4
commit 6981185f4b
3 changed files with 10 additions and 2 deletions
@@ -136,8 +136,9 @@ fun OkHttpClient.newCachelessCallWithProgress(
.build()
val originalResponse = chain.proceed(request)
val actualExistingSize = if (originalResponse.code == 206) existingSize else 0L
originalResponse.newBuilder()
.body(ProgressResponseBody(originalResponse.body, listener, existingSize))
.body(ProgressResponseBody(originalResponse.body, listener, actualExistingSize))
.build()
}
.build()
@@ -40,9 +40,15 @@ class ProgressResponseBody(
val bytesRead = super.read(sink, byteCount)
// read() returns the number of bytes read, or -1 if this source is exhausted.
totalBytesRead += if (bytesRead != -1L) bytesRead else 0
// contentLength() returns -1L if Content-Length is missing
val totalLength = responseBody.contentLength().let {
if (it != -1L) it + existingSize else -1L
}
progressListener.update(
totalBytesRead,
responseBody.contentLength(),
totalLength,
bytesRead == -1L,
)
return bytesRead