Search code examples
kotlinvert.xvertexvertx-verticle

Vertex upload file: BodyHandler methods not working



I want to upload avatars. My endpoints are set with OpenAPI3RouterFactory(api.yaml file).

api.yaml:

#other endpoints

/api/v1/upload-avatar:
  post:
    summary: Uploading avatars endpoint
    operationId: upload-avatar
    tags:
      - sign up
      - registration

#other ones


HandlerVerticle.kt:

//OpenApi3Router is set
//other endpoints
routerFactory.addHandlerByOperationId("upload-avatar", BodyHandler.create().setDeleteUploadedFilesOnEnd(true).setUploadsDirectory("mp-upload").setMergeFormAttributes(true))
routerFactory.addHandlerByOperationId("upload-avatar", { routingContext -> 
        val fileUploadSet = routingContext.fileUploads()
        val fileUploadIterator = fileUploadSet.iterator()
        while (fileUploadIterator.hasNext()) {
            val fileUpload = fileUploadIterator.next()
            val uploadedFile = vertx.fileSystem().readFileBlocking(fileUpload.uploadedFileName())

            try {
                val fileName = URLDecoder.decode(fileUpload.fileName(), "UTF-8")
                vertx.fileSystem().writeFileBlocking(fileName , uploadedFile)
                routingContext.response().end()
            } catch (e: Exception) {
                e.printStackTrace()
            }


        }
})

//other routes

when I upload the image it uploads successful, but in server side vertx creates wrong upload directory, and it doesn't delete temporary uploaded files. Can anyone help me?


Solution

  • Not sure if this should be considered a bug, or something intentional, but...

    OpenAPI3RouterFactoryImpl.getRouter() always overrides BodyHandler

    No matter what you set there before.

    I opened a new issue for now: https://github.com/vert-x3/vertx-web/issues/860