我花了太多时间试图解决这个问题。因此,我在下面发布的代码在下载文件方面确实有效,但问题是,流程具有非常意外的行为。方法调用似乎会阻塞,response.content.readAvailable()
直到它完全完成下载整个文件,此时会发生发射进度,因此您最终要等待很长时间才能下载文件,然后在一瞬间您会获得所有进度更新。所以我想知道是否有办法做到这一点,我一次读取一定数量的字节,然后发出一个进度,然后重复直到文件完成下载?或者可能是一种挂钩 readAvailable() 方法并以这种方式更新进度的方法?对此的任何帮助将不胜感激。
这是我找到并修改的代码,但仍然无法正常工作:
suspend fun HttpClient.downloadFile(
output: File,
downloadUrl: String,
md5Hash: String,
) = flow {
try {
val response = get<HttpResponse> { url(downloadUrl) }
val data = ByteArray(response.contentLength()?.toInt() ?: 0)
val contentLn = response.contentLength()?.toInt() ?: 0
var offset = 0
var bytesRemaining = contentLn
do {
val chunkSize = min(maxChunkSize, bytesRemaining)
logger?.d { "Read Available:" }
val result = response.content.readAvailable(data, offset, length = chunkSize)
val progress = ((offset / contentLn.toDouble()) * 100).toInt()
emit(DownloadResult.Progress(progress))
logger?.d { "logged progress: $progress" }
// delay(6000L) this was to test my assumption that the readAvalible was blocking.
offset += chunkSize
bytesRemaining -= chunkSize
} while (result != -1)
if (response.status.isSuccess()) {
if (data.md5().hex == md5Hash) {
output.write(data)
emit(DownloadResult.Success)
} else {
emit(DownloadResult.ErrorCorruptFile)
}
} else {
emit(DownloadResult.ErrorBadResponseCode(response.status.value))
}
} catch (e: TimeoutCancellationException) {
emit(DownloadResult.ErrorRequestTimeout("Connection timed out", e))
}
}