Support resumable image downloads if supported by source (#3167)
Co-authored-by: MajorTanya <39014446+MajorTanya@users.noreply.github.com> Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>
This commit is contained in:
@@ -118,13 +118,26 @@ suspend fun Call.awaitSuccess(): Response {
|
||||
return response
|
||||
}
|
||||
|
||||
fun OkHttpClient.newCachelessCallWithProgress(request: Request, listener: ProgressListener): Call {
|
||||
fun OkHttpClient.newCachelessCallWithProgress(
|
||||
request: Request,
|
||||
listener: ProgressListener,
|
||||
existingSize: Long = 0L,
|
||||
): Call {
|
||||
val progressClient = newBuilder()
|
||||
.cache(null)
|
||||
.addNetworkInterceptor { chain ->
|
||||
val originalResponse = chain.proceed(chain.request())
|
||||
val request = chain.request()
|
||||
.newBuilder()
|
||||
.apply {
|
||||
if (existingSize > 0 && request.header("Range") == null) {
|
||||
header("Range", "bytes=$existingSize-")
|
||||
}
|
||||
}
|
||||
.build()
|
||||
|
||||
val originalResponse = chain.proceed(request)
|
||||
originalResponse.newBuilder()
|
||||
.body(ProgressResponseBody(originalResponse.body, listener))
|
||||
.body(ProgressResponseBody(originalResponse.body, listener, existingSize))
|
||||
.build()
|
||||
}
|
||||
.build()
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.io.IOException
|
||||
class ProgressResponseBody(
|
||||
private val responseBody: ResponseBody,
|
||||
private val progressListener: ProgressListener,
|
||||
private val existingSize: Long, // bytes already downloaded
|
||||
) : ResponseBody() {
|
||||
|
||||
private val bufferedSource: BufferedSource by lazy {
|
||||
@@ -32,7 +33,7 @@ class ProgressResponseBody(
|
||||
|
||||
private fun source(source: Source): Source {
|
||||
return object : ForwardingSource(source) {
|
||||
var totalBytesRead = 0L
|
||||
var totalBytesRead = existingSize
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun read(sink: Buffer, byteCount: Long): Long {
|
||||
|
||||
Reference in New Issue
Block a user