This commit is contained in:
InsanusMokrassar 2022-11-22 14:39:31 +06:00
parent 2b5380f8d6
commit 76e214fc08
3 changed files with 18 additions and 6 deletions

View File

@ -1,3 +1,3 @@
package dev.inmo.micro_utils.ktor.client package dev.inmo.micro_utils.ktor.client
typealias OnUploadCallback = (uploaded: Long, count: Long) -> Unit typealias OnUploadCallback = suspend (uploaded: Long, count: Long) -> Unit

View File

@ -1,6 +1,8 @@
package dev.inmo.micro_utils.ktor.client package dev.inmo.micro_utils.ktor.client
import dev.inmo.micro_utils.common.MPPFile import dev.inmo.micro_utils.common.MPPFile
import dev.inmo.micro_utils.coroutines.LinkedSupervisorJob
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.ktor.common.TemporalFileId import dev.inmo.micro_utils.ktor.common.TemporalFileId
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import kotlinx.coroutines.* import kotlinx.coroutines.*
@ -12,10 +14,11 @@ import org.w3c.xhr.XMLHttpRequest.Companion.DONE
suspend fun tempUpload( suspend fun tempUpload(
fullTempUploadDraftPath: String, fullTempUploadDraftPath: String,
file: MPPFile, file: MPPFile,
onUpload: (Long, Long) -> Unit onUpload: OnUploadCallback
): TemporalFileId { ): TemporalFileId {
val formData = FormData() val formData = FormData()
val answer = CompletableDeferred<TemporalFileId>(currentCoroutineContext().job) val answer = CompletableDeferred<TemporalFileId>(currentCoroutineContext().job)
val subscope = CoroutineScope(currentCoroutineContext().LinkedSupervisorJob())
formData.append( formData.append(
"data", "data",
@ -25,7 +28,7 @@ suspend fun tempUpload(
val request = XMLHttpRequest() val request = XMLHttpRequest()
request.responseType = XMLHttpRequestResponseType.TEXT request.responseType = XMLHttpRequestResponseType.TEXT
request.upload.onprogress = { request.upload.onprogress = {
onUpload(it.loaded.toLong(), it.total.toLong()) subscope.launchSafelyWithoutExceptions { onUpload(it.loaded.toLong(), it.total.toLong()) }
} }
request.onload = { request.onload = {
if (request.status == 200.toShort()) { if (request.status == 200.toShort()) {
@ -48,7 +51,9 @@ suspend fun tempUpload(
} }
} }
return answer.await() return answer.await().also {
subscope.cancel()
}
} }

View File

@ -2,10 +2,14 @@ package dev.inmo.micro_utils.ktor.client
import dev.inmo.micro_utils.common.MPPFile import dev.inmo.micro_utils.common.MPPFile
import dev.inmo.micro_utils.common.Progress import dev.inmo.micro_utils.common.Progress
import dev.inmo.micro_utils.coroutines.LinkedSupervisorJob
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.http.Headers import io.ktor.http.Headers
import io.ktor.utils.io.core.readBytes import io.ktor.utils.io.core.readBytes
import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.job import kotlinx.coroutines.job
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DeserializationStrategy
@ -35,6 +39,7 @@ actual suspend fun <T> HttpClient.uniUpload(
): T? { ): T? {
val formData = FormData() val formData = FormData()
val answer = CompletableDeferred<T?>(currentCoroutineContext().job) val answer = CompletableDeferred<T?>(currentCoroutineContext().job)
val subscope = CoroutineScope(currentCoroutineContext().LinkedSupervisorJob())
data.forEach { (k, v) -> data.forEach { (k, v) ->
when (v) { when (v) {
@ -60,7 +65,7 @@ actual suspend fun <T> HttpClient.uniUpload(
} }
request.responseType = XMLHttpRequestResponseType.TEXT request.responseType = XMLHttpRequestResponseType.TEXT
request.upload.onprogress = { request.upload.onprogress = {
onUpload(it.loaded.toLong(), it.total.toLong()) subscope.launchSafelyWithoutExceptions { onUpload(it.loaded.toLong(), it.total.toLong()) }
} }
request.onload = { request.onload = {
if (request.status == 200.toShort()) { if (request.status == 200.toShort()) {
@ -85,5 +90,7 @@ actual suspend fun <T> HttpClient.uniUpload(
} }
} }
return answer.await() return answer.await().also {
subscope.cancel()
}
} }