handleUniUpload now is inline

This commit is contained in:
InsanusMokrassar 2022-11-24 12:29:48 +06:00
parent 8f25c123dd
commit 58d754bbde

View File

@ -6,24 +6,27 @@ import io.ktor.http.content.*
import io.ktor.server.application.ApplicationCall import io.ktor.server.application.ApplicationCall
import io.ktor.server.request.receiveMultipart import io.ktor.server.request.receiveMultipart
import io.ktor.utils.io.core.* import io.ktor.utils.io.core.*
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.isActive
/** /**
* Server-side part which receives [dev.inmo.micro_utils.ktor.client.uniUpload] request * Server-side part which receives [dev.inmo.micro_utils.ktor.client.uniUpload] request
*/ */
suspend fun ApplicationCall.handleUniUpload( suspend inline fun ApplicationCall.handleUniUpload(
onFormItem: (PartData.FormItem) -> Unit = {}, onFormItem: (PartData.FormItem) -> Unit = {},
onFileItem: (PartData.FileItem) -> Unit = {}, onBinaryContent: (PartData.BinaryItem) -> Unit = {},
onBinaryChannelItem: (PartData.BinaryChannelItem) -> Unit = {}, onBinaryChannelItem: (PartData.BinaryChannelItem) -> Unit = {},
onBinaryContent: (PartData.BinaryItem) -> Unit = {} onFileItem: (PartData.FileItem) -> Unit = {}
) { ) {
val multipartData = receiveMultipart() val multipartData = receiveMultipart()
multipartData.forEachPart { while (currentCoroutineContext().isActive) {
when (it) { val partData = multipartData.readPart() ?: break
is PartData.FormItem -> onFormItem(it) when (partData) {
is PartData.FileItem -> onFileItem(it) is PartData.FormItem -> onFormItem(partData)
is PartData.BinaryItem -> onBinaryContent(it) is PartData.FileItem -> onFileItem(partData)
is PartData.BinaryChannelItem -> onBinaryChannelItem(it) is PartData.BinaryItem -> onBinaryContent(partData)
is PartData.BinaryChannelItem -> onBinaryChannelItem(partData)
} }
} }
} }
@ -38,15 +41,14 @@ suspend fun ApplicationCall.uniloadMultipart(
handleUniUpload( handleUniUpload(
onFormItem, onFormItem,
{ onBinaryContent,
when (it.name) { onBinaryChannelItem
"bytes" -> resultInput = it.provider() ) {
else -> onCustomFileItem(it) when (it.name) {
} "bytes" -> resultInput = it.provider()
}, else -> onCustomFileItem(it)
onBinaryChannelItem, }
onBinaryContent }
)
resultInput ?: error("Bytes has not been received") resultInput ?: error("Bytes has not been received")
} }