xiongzhu 10 月之前
父節點
當前提交
4f02c7db5c
共有 2 個文件被更改,包括 16 次插入15 次删除
  1. 1 1
      app/build.gradle
  2. 15 14
      app/src/main/java/com/example/modifier/ui/utils/UtilsFragment.kt

+ 1 - 1
app/build.gradle

@@ -24,7 +24,7 @@ android {
         applicationId "com.example.modifier"
         minSdk 31
         targetSdk 34
-        versionCode 172
+        versionCode 173
         versionName "1.0.1"
         archivesBaseName = "${applicationId}-${versionCode}"
 

+ 15 - 14
app/src/main/java/com/example/modifier/ui/utils/UtilsFragment.kt

@@ -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}")