使用ktor检查多部分请求主体中是否存在所有参数

check whether all parameter exist or not in multipart request body with ktor

我正在尝试用kt​​or创建一个多部分请求,其代码如下,

import com.firstapp.modal.response.SuccessResponse
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.PartData
import io.ktor.http.content.forEachPart
import io.ktor.http.content.streamProvider
import io.ktor.locations.Location
import io.ktor.locations.post
import io.ktor.request.isMultipart
import io.ktor.request.receive
import io.ktor.request.receiveMultipart
import io.ktor.response.respond
import io.ktor.routing.Route
import io.ktor.util.getOrFail
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
import java.io.File
import java.io.InputStream
import java.io.OutputStream
import java.lang.IllegalArgumentException

@Location("/uploadVideo/{title}")
class UploadVideo(val title:String)

fun Route.upload(uploadDir: File) {

    post<UploadVideo> {
        val multipart = call.receiveMultipart()
        var videoFile: File? = null

        // Processes each part of the multipart input content of the user
        multipart.forEachPart { part ->
            when (part) {
                is PartData.FormItem -> {
                    if (part.name != "title")
                        throw IllegalArgumentException("Title parameter not found")
                    //title = part.value
                }
                is PartData.FileItem -> {
                    if (part.name != "file")
                        throw IllegalArgumentException("file parameter not found")

                    val ext = File(part.originalFileName).extension
                    val file = File(uploadDir, "upload-${System.currentTimeMillis()}-${call.parameters.getOrFail("title").hashCode()}.$ext")
                    part.streamProvider().use { input -> file.outputStream().buffered().use { output -> input.copyToSuspend(output) } }
                    videoFile = file
                }
            }

            part.dispose()
        }

        call.respond(
            HttpStatusCode.OK,
            SuccessResponse(
                videoFile!!,
                HttpStatusCode.OK.value,
                "video file stored"
            )
        )
    }
}


suspend fun InputStream.copyToSuspend(
    out: OutputStream,
    bufferSize: Int = DEFAULT_BUFFER_SIZE,
    yieldSize: Int = 4 * 1024 * 1024,
    dispatcher: CoroutineDispatcher = Dispatchers.IO
): Long {
    return withContext(dispatcher) {
        val buffer = ByteArray(bufferSize)
        var bytesCopied = 0L
        var bytesAfterYield = 0L
        while (true) {
            val bytes = read(buffer).takeIf { it >= 0 } ?: break
            out.write(buffer, 0, bytes)
            if (bytesAfterYield >= yieldSize) {
                yield()
                bytesAfterYield %= yieldSize
            }
            bytesCopied += bytes
            bytesAfterYield += bytes
        }
        return@withContext bytesCopied
    }
}

上面的代码或其余的 api 工作正常,但问题是,我想检查所有参数是否可用,即我想发送附加参数以及格式如下的文件,

class VideoDetail(val type: String, val userId: String, val userName: String)

我在这里举个例子,我想要的是

post("/") { request ->
    val requestParamenter = call.receive<UserInsert>()
}

这里,不管我们传入什么参数,都会自动转换成pojo类,如果我们没有传入,就会抛出异常,

所以,我想用 multipart 实现类似的事情。

最后,我能够对问题进行排序,下面是代码,

 @Location("/uploadVideo/{id}")
class UploadVideo(val id: Int)

fun Route.upload(uploadDir: File) {

    post<UploadVideo> {

        val multipart = call.receiveMultipart().readAllParts()
        val multiMap = multipart.associateBy { it.name }.toMap()
        val data = PersonForm(multiMap)
        println(data)
        
        val ext = File(data.file.originalFileName).extension
        val file = File(uploadDir, "upload-${System.currentTimeMillis()}-${data.file.originalFileName}")
        data.file.streamProvider()
            .use { input -> file.outputStream().buffered().use { output -> input.copyToSuspend(output) } }

        call.respond(
            HttpStatusCode.OK,
            SuccessResponse(
                file,
                HttpStatusCode.OK.value,
                "video file stored"
            )
        )
    }
}

suspend fun InputStream.copyToSuspend(
    out: OutputStream,
    bufferSize: Int = DEFAULT_BUFFER_SIZE,
    yieldSize: Int = 4 * 1024 * 1024,
    dispatcher: CoroutineDispatcher = Dispatchers.IO
): Long {
    return withContext(dispatcher) {
        val buffer = ByteArray(bufferSize)
        var bytesCopied = 0L
        var bytesAfterYield = 0L
        while (true) {
            val bytes = read(buffer).takeIf { it >= 0 } ?: break
            out.write(buffer, 0, bytes)
            if (bytesAfterYield >= yieldSize) {
                yield()
                bytesAfterYield %= yieldSize
            }
            bytesCopied += bytes
            bytesAfterYield += bytes
        }
        return@withContext bytesCopied
    }
}

class PersonForm(map: Map<String?, PartData>) {
    val file: PartData.FileItem by map
    val type: PartData.FormItem by map
    val title: PartData.FormItem by map

    override fun toString() = "${file.originalFileName}, ${type.value}, ${title.value}"
}

这种方法的唯一问题是通过使用地图委托,您必须访问并有权知道地图中是否存在所有参数,即

 val data = PersonForm(multiMap)
        println(data)