From eb562d8784d7d823596030a2ea3cb6a63978925f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 14 Dec 2021 22:03:29 +0600 Subject: [PATCH] add stream using for multipart --- .../ktor/server/ServerRoutingShortcuts.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt index d4dd303616f..344092679c9 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt @@ -14,9 +14,10 @@ import io.ktor.request.receiveMultipart import io.ktor.response.respond import io.ktor.response.respondBytes import io.ktor.routing.Route +import io.ktor.util.asStream +import io.ktor.util.cio.writeChannel import io.ktor.util.pipeline.PipelineContext -import io.ktor.utils.io.core.Input -import io.ktor.utils.io.core.readBytes +import io.ktor.utils.io.core.* import kotlinx.coroutines.flow.Flow import kotlinx.serialization.* import java.io.File @@ -182,7 +183,9 @@ suspend fun ApplicationCall.uniloadMultipartFile( name.nameWithoutExtension, ".${name.extension}" ).apply { - writeBytes(it.provider().readBytes()) + outputStream().use { fileStream -> + it.provider().asStream().copyTo(fileStream) + } } } "data" -> data = standardKtorSerialFormat.decodeDefault(deserializer, it.provider().readBytes()).optional @@ -216,7 +219,9 @@ suspend fun ApplicationCall.uniloadMultipartFile( name.nameWithoutExtension, ".${name.extension}" ).apply { - writeBytes(it.provider().readBytes()) + outputStream().use { fileStream -> + it.provider().asStream().copyTo(fileStream) + } } } else { onCustomFileItem(it)