|
|
@@ -42,11 +42,14 @@ import com.example.modifier.utils.syncTime
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
import io.ktor.client.call.body
|
|
|
import io.ktor.client.plugins.resources.get
|
|
|
+import io.ktor.client.plugins.timeout
|
|
|
import io.ktor.client.request.prepareGet
|
|
|
+import io.ktor.client.statement.bodyAsChannel
|
|
|
import io.ktor.http.contentLength
|
|
|
import io.ktor.utils.io.ByteReadChannel
|
|
|
import io.ktor.utils.io.core.isEmpty
|
|
|
import io.ktor.utils.io.core.readBytes
|
|
|
+import io.ktor.utils.io.jvm.javaio.copyTo
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.Job
|
|
|
@@ -353,21 +356,19 @@ class UtilsFragment : Fragment() {
|
|
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
|
|
|
)
|
|
|
ktorClient.prepareGet(url)
|
|
|
+ {
|
|
|
+ timeout { requestTimeoutMillis = 30000 }
|
|
|
+ }
|
|
|
.execute { httpResponse ->
|
|
|
- val channel: ByteReadChannel = httpResponse.body()
|
|
|
- while (!channel.isClosedForRead) {
|
|
|
- val packet = channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong())
|
|
|
- while (!packet.isEmpty) {
|
|
|
- val bytes = packet.readBytes()
|
|
|
- file.appendBytes(bytes)
|
|
|
- withContext(
|
|
|
- Dispatchers.Main
|
|
|
- ) {
|
|
|
- dialogBinding.progressBar.isIndeterminate = false
|
|
|
- dialogBinding.progressBar.progress =
|
|
|
- (file.length() * 100 / httpResponse.contentLength()!!).toInt()
|
|
|
- }
|
|
|
- }
|
|
|
+ if (httpResponse.status.value in 200..299) {
|
|
|
+ val byteReadChannel = httpResponse.bodyAsChannel()
|
|
|
+ byteReadChannel.copyTo(file.outputStream())
|
|
|
+ } else {
|
|
|
+ Log.e(
|
|
|
+ "App",
|
|
|
+ "Failed to download file. HTTP Status: ${httpResponse.status.value}"
|
|
|
+ )
|
|
|
+ throw Exception("Failed to download file")
|
|
|
}
|
|
|
}
|
|
|
Log.i(TAG, "A file saved to ${file.path}")
|