Hello all, I’m trying to write a file download met...
# kotlin-native
a
Hello all, I’m trying to write a file download method using Ktor on a native/mpp app. I hope this is the place to ask this… if not please direct me to the right channel. here’s my method so far:
Copy code
suspend fun HttpClient.downloadFile(
    output: File,
    downloadUrl: Url,
    md5Hash: String
): Flow<DownloadResult> {
    return flow {
        try {
            val response = get<HttpResponse> { url(downloadUrl) }
            val data = ByteArray(response.contentLength()?.toInt() ?: 0)
            var offset = 0
            do {
                val progress = (offset.toDouble() / data.size) * 100
                emit(DownloadResult.Progress(progress))
                val currentRead = response.content.readAvailable(data, offset, data.size)
                offset += currentRead
            } while (currentRead > 0)

            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))
        } catch (t: Throwable) {
            emit(DownloadResult.Error("Failed to connect", t))
        }
    }
}
The problem is that the
response.content.readAvailable()
method always seems to read the whole file at once without chunking it up, which means my progress emission only gets outputted once. I’m wondering if there’s something I’m doing wrong here? or if that’s normal behaviour? Also as a side note the file I’m testing on is 25MB so it should emit a few times I’d think.
j
a
@Julius thanks for your response… much appreciated. 🙂