Created
October 27, 2019 14:38
-
-
Save francescogatto/648ad2c22f8094421d5cc712054cca50 to your computer and use it in GitHub Desktop.
#kkd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun HttpClient.downloadFile(file: File, url: String, callback: suspend (boolean: Boolean) -> Unit) { | |
val response = call { | |
url(url) | |
method = HttpMethod.Get | |
}.response | |
val data = ByteArray(response.contentLength()!!.toInt()) | |
var offset = 0 | |
do { | |
val currentRead = response.content.readAvailable(data, offset, data.size) | |
offset += currentRead | |
val progress = (offset.toFloat() * 100f / data.size.toFloat()).roundToInt() | |
} while (currentRead > 0) | |
response.close() | |
if (!response.status.isSuccess()) { | |
callback(false) | |
} | |
file.writeBytes(data) | |
callback(true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment