From b7b5159e9c1526e77e6fed021f7e0290d699ecea Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 22 Nov 2022 09:51:10 +0600 Subject: [PATCH] Revert "Revert "start adding uniupload"" This reverts commit 0d1aae0ef7bb25ef154f2ce951ceb7fa7a6d3418. --- .../inmo/micro_utils/ktor/client/UniUpload.kt | 28 ++++++++++++++++ .../ktor/server/QueryParameters.kt | 25 +++++++++++++++ ...outingShortcuts.kt => UniloadMultipart.kt} | 32 ------------------- 3 files changed, 53 insertions(+), 32 deletions(-) create mode 100644 ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/UniUpload.kt create 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/{ServerRoutingShortcuts.kt => UniloadMultipart.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 new file mode 100644 index 00000000000..fe3f32a9b41 --- /dev/null +++ b/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/ktor/client/UniUpload.kt @@ -0,0 +1,28 @@ +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 new file mode 100644 index 00000000000..053d316c631 --- /dev/null +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/QueryParameters.kt @@ -0,0 +1,25 @@ +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/ServerRoutingShortcuts.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/UniloadMultipart.kt similarity index 73% rename from ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/ServerRoutingShortcuts.kt rename to ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/UniloadMultipart.kt index 968a3607d11..76a34745070 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/UniloadMultipart.kt @@ -2,22 +2,10 @@ 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 = {}, @@ -89,23 +77,3 @@ 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") - } -}