dave08
10/18/2018, 10:43 AMApplicationCall.receiveFile/s()
be added to Ktor? Or is it too application specific?mp
10/18/2018, 10:44 AMreceiveMultipart()
enleur
10/18/2018, 10:44 AMdave08
10/18/2018, 10:45 AMmp
10/18/2018, 10:46 AMdave08
10/18/2018, 10:47 AMmp
10/18/2018, 10:48 AMcall.receiveMultipart().readAllParts().map { it.streamProvider().use { it.readAllBytes() } }
🤷♂️dave08
10/18/2018, 10:49 AMsuspend fun ApplicationCall.receiveFile(uploadDir: String): File =
receiveMultipart().readPart().let {
if (it is PartData.FileItem) {
val ext = File(it.originalFileName).extension
val uploadName = "upload-${System.currentTimeMillis()}-${it.originalFileName!!.hashCode()}.$ext"
File(uploadDir, uploadName).also { file ->
withContext(CommonPool) {
it.streamProvider().use { its -> file.outputStream().buffered().use { its.copyToSuspend(it) } }
}
}
} else error("Error retreiving upload")
}
suspend fun ApplicationCall.receiveFiles(uploadDir: String): Map<String, File> =
receiveMultipart().readAllParts().map {
if (it is PartData.FileItem) {
it.name!! to File(uploadDir, it.originalFileName).also { file ->
withContext(CommonPool) {
it.streamProvider().use { its -> file.outputStream().buffered().use { its.copyToSuspend(it) } }
}
}
} else error("Error retreiving upload")
}.toMap()
mp
10/18/2018, 10:51 AMdave08
10/18/2018, 11:24 AMmp
10/18/2018, 11:25 AM