Hi. While receiving multipart uploads, do I need t...
# ktor
j
Hi. While receiving multipart uploads, do I need to close any of the resources? Basically my routing looks like this:
Copy code
post("/fileimport") {
                val multipartData = call.receiveMultipart()
                val parts = multipartData.readAllParts()
                    .filterIsInstance<PartData.FileItem>()
                    .mapIndexed { index, item ->
                        UploadedContent(
                            name = item.name ?: "unnamed$index",
                            filename = item.originalFileName ?: "unnamed$index",
                            contentType = item.contentType,
                            bytes = item.provider.invoke().readBytes()
                        )
                    }
                doSomething(parts)
                call.respond("OK")
            }
so the questions is - do I need to somehow close resources created by
call.receiveMultipart()
or
item.provider.invoke().readBytes()
?
a
none of them seem to implement
Closeable
, so I guess the answer is no 🙂
j
there is this
.dispose()
method but I’m not sure if this is something to be called manually, or it’s internal mechanism
and provider.invoke() returns closeable, so I’m thinking
Copy code
bytes = item.provider.invoke().use { it.readBytes() }
🙂
a
You should call the
dispose()
method on the binary and file items to free associated resources.
j
Thanks!