aboutsummaryrefslogtreecommitdiff
path: root/app/bin/main/xyz/brysonsteck/serverfordummies/downloadFile.kt
diff options
context:
space:
mode:
authorBryson Steck <brysonsteck@protonmail.com>2023-05-09 16:31:47 -0600
committerBryson Steck <brysonsteck@protonmail.com>2023-05-09 16:31:47 -0600
commit7db4d246d920586edcc485d514d9651dcc370d67 (patch)
tree96707a77637d400f1e223122ed4c1afa137732a4 /app/bin/main/xyz/brysonsteck/serverfordummies/downloadFile.kt
parent47de8220d52dde525d107ee076d090a48ee02c2e (diff)
downloadServerCraft-7db4d246d920586edcc485d514d9651dcc370d67.tar
ServerCraft-7db4d246d920586edcc485d514d9651dcc370d67.tar.gz
ServerCraft-7db4d246d920586edcc485d514d9651dcc370d67.tar.bz2
logic seems fine?
Diffstat (limited to 'app/bin/main/xyz/brysonsteck/serverfordummies/downloadFile.kt')
-rw-r--r--app/bin/main/xyz/brysonsteck/serverfordummies/downloadFile.kt37
1 files changed, 0 insertions, 37 deletions
diff --git a/app/bin/main/xyz/brysonsteck/serverfordummies/downloadFile.kt b/app/bin/main/xyz/brysonsteck/serverfordummies/downloadFile.kt
deleted file mode 100644
index bbaeab5..0000000
--- a/app/bin/main/xyz/brysonsteck/serverfordummies/downloadFile.kt
+++ /dev/null
@@ -1,37 +0,0 @@
-import java.net.http.HttpClient
-import kotlinx.coroutines.flow
-
-sealed class DownloadStatus {
-
- object Success : DownloadStatus()
-
- data class Error(val message: String) : DownloadStatus()
-
- data class Progress(val progress: Int): DownloadStatus()
-
-}
-
-// function from https://gist.githubusercontent.com/SG-K/63e379efcc3d1cd3ce4fb56ee0e29c42/raw/cd9a4a016401b7c54ec01303415b5871ffa26066/downloadFile.kt
-suspend fun HttpClient.downloadFile(file: File, url: String): Flow<DownloadStatus> {
- return flow {
- val response = call {
- url(url)
- method = HttpMethod.Get
- }.response
- val byteArray = ByteArray(response.contentLength()!!.toInt())
- var offset = 0
- do {
- val currentRead = response.content.readAvailable(byteArray, offset, byteArray.size)
- offset += currentRead
- val progress = (offset * 100f / byteArray.size).roundToInt()
- emit(DownloadStatus.Progress(progress))
- } while (currentRead > 0)
- response.close()
- if (response.status.isSuccess()) {
- file.writeBytes(byteArray)
- emit(DownloadStatus.Success)
- } else {
- emit(DownloadStatus.Error("File not downloaded"))
- }
- }
-} \ No newline at end of file