From 0d1aae0ef7bb25ef154f2ce951ceb7fa7a6d3418 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 21 Nov 2022 20:20:37 +0600 Subject: [PATCH] Revert "start adding uniupload" This reverts commit 4d022f0480c8f63f2dbd8988b14504cffcc0dec7. --- .../inmo/micro_utils/ktor/client/UniUpload.kt | 28 ---------------- .../ktor/server/QueryParameters.kt | 25 --------------- ...Multipart.kt => ServerRoutingShortcuts.kt} | 32 +++++++++++++++++++ 3 files changed, 32 insertions(+), 53 deletions(-) delete mode 100644 ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/UniUpload.kt delete mode 100644 ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt rename ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/{UniloadMultipart.kt => ServerRoutingShortcuts.kt} (73%) diff --git a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/UniUpload.kt b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/UniUpload.kt deleted file mode 100644 index fe3f32a9b41..00000000000 --- a/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/UniUpload.kt +++ /dev/null @@ -1,28 +0,0 @@ -package dev.inmo.micro_utils.ktor.client - -import dev.inmo.micro_utils.common.FileName -import io.ktor.client.HttpClient -import io.ktor.http.Headers -import io.ktor.utils.io.core.Input -import kotlinx.serialization.DeserializationStrategy -import kotlinx.serialization.StringFormat - -data class UniUploadFileInfo( - val fileName: FileName, - val bytesAllocator: Input -) - -/** - * Will execute submitting of multipart data request - * - * @param data [Map] where keys will be used as names for multipart parts and values as values. If you will pass - * [dev.inmo.micro_utils.common.MPPFile] (File from JS or JVM platform). Also you may pass [UniUploadFileInfo] as value - * in case you wish to pass other source of multipart binary data than regular file - */ -expect suspend fun HttpClient.uniupload( - url: String, - data: Map, - stringFormat: StringFormat, - resultDeserializer: DeserializationStrategy, - headers: Headers = Headers.Empty -): T? diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt deleted file mode 100644 index 053d316c631..00000000000 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt +++ /dev/null @@ -1,25 +0,0 @@ -package dev.inmo.micro_utils.ktor.server - -import io.ktor.http.HttpStatusCode -import io.ktor.server.application.ApplicationCall -import io.ktor.server.response.respond - -suspend fun ApplicationCall.getParameterOrSendError( - field: String -) = parameters[field].also { - if (it == null) { - respond(HttpStatusCode.BadRequest, "Request must contains $field") - } -} - -fun ApplicationCall.getQueryParameter( - field: String -) = request.queryParameters[field] - -suspend fun ApplicationCall.getQueryParameterOrSendError( - field: String -) = getQueryParameter(field).also { - if (it == null) { - respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field") - } -} diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/UniloadMultipart.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt similarity index 73% rename from ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/UniloadMultipart.kt rename to ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt index 76a34745070..968a3607d11 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/UniloadMultipart.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt @@ -2,10 +2,22 @@ package dev.inmo.micro_utils.ktor.server import dev.inmo.micro_utils.common.* import dev.inmo.micro_utils.coroutines.safely +import dev.inmo.micro_utils.ktor.common.* +import io.ktor.http.* import io.ktor.http.content.* import io.ktor.server.application.ApplicationCall +import io.ktor.server.application.call +import io.ktor.server.request.receive import io.ktor.server.request.receiveMultipart +import io.ktor.server.response.respond +import io.ktor.server.response.respondBytes +import io.ktor.server.routing.Route +import io.ktor.server.websocket.WebSocketServerSession +import io.ktor.util.pipeline.PipelineContext import io.ktor.utils.io.core.* +import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.SerializationStrategy suspend fun ApplicationCall.uniloadMultipart( onFormItem: (PartData.FormItem) -> Unit = {}, @@ -77,3 +89,23 @@ suspend fun ApplicationCall.uniloadMultipartFile( resultInput ?: error("Bytes has not been received") } + +suspend fun ApplicationCall.getParameterOrSendError( + field: String +) = parameters[field].also { + if (it == null) { + respond(HttpStatusCode.BadRequest, "Request must contains $field") + } +} + +fun ApplicationCall.getQueryParameter( + field: String +) = request.queryParameters[field] + +suspend fun ApplicationCall.getQueryParameterOrSendError( + field: String +) = getQueryParameter(field).also { + if (it == null) { + respond(HttpStatusCode.BadRequest, "Request query parameters must contains $field") + } +}