mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 13:53:49 +00:00
handleUniUpload now is inline
This commit is contained in:
parent
8f25c123dd
commit
58d754bbde
@ -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,
|
||||||
|
onBinaryChannelItem
|
||||||
|
) {
|
||||||
when (it.name) {
|
when (it.name) {
|
||||||
"bytes" -> resultInput = it.provider()
|
"bytes" -> resultInput = it.provider()
|
||||||
else -> onCustomFileItem(it)
|
else -> onCustomFileItem(it)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
onBinaryChannelItem,
|
|
||||||
onBinaryContent
|
|
||||||
)
|
|
||||||
|
|
||||||
resultInput ?: error("Bytes has not been received")
|
resultInput ?: error("Bytes has not been received")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user