Commit 3649239c authored by Chiraz Boukadida's avatar Chiraz Boukadida
Browse files

#30 Implementation Endpoint Hochladen einer DOCX-Datei

parent 20974698
package de.h_da.fbi.smebt.intentfinder.client
import de.h_da.fbi.smebt.intentfinder.server.sources.DocxReader
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
import io.ktor.http.*
import java.io.File
suspend fun main() {
val client = HttpClient(CIO)
val response: HttpResponse = client.submitFormWithBinaryData(
url = "http://localhost:8080/file",
formData = formData {
append("description", "docx Document")
append("Dok", File("uploads/Dok.docx").readBytes(), Headers.build {
append(
HttpHeaders.ContentType,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
append(HttpHeaders.ContentDisposition, "filename=Dok.docx")
})
}
)
println(response.readText())
println(response.status)
try {
val reader = DocxReader("uploads/Dok.docx")
val fileContent =reader.readFile()
println(fileContent)
} catch (exception: Exception) {
print(exception.message)
}
}
\ No newline at end of file
......@@ -3,17 +3,21 @@ package de.h_da.fbi.smebt.intentfinder.server
import de.h_da.fbi.smebt.intentfinder.server.nlp.PythonBridge
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.serialization.*
import kotlinx.serialization.json.Json
import registerUploadRoutes
import java.io.File
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module() {
fun Application.module(testing: Boolean = false) {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
......@@ -28,6 +32,34 @@ fun Application.module() {
get("/") {
call.respondText("IntentFinder is available")
}
var fileDescription = ""
var fileName = ""
post("/file") {
val multipartData = call.receiveMultipart()
multipartData.forEachPart { part ->
when (part) {
is PartData.FormItem -> {
fileDescription = part.value
}
is PartData.FileItem -> {
fileName = part.originalFileName as String
var fileBytes = part.streamProvider().readBytes()
val file = File("uploads/$fileName")
if (!file.exists()) {
call.respond(HttpStatusCode.NotFound)
}else {
File("uploads/$fileName").writeBytes(fileBytes)
}
}
}
}
call.respondText("$fileDescription is uploaded to 'uploads/$fileName'")
}
}
registerUploadRoutes()
}
POST http://0.0.0.0:8080/file
Content-Type: multipart/form-data;boundary=FileBoundary
--FileBoundary
Content-Disposition: form-data; name="description"
Content-Type: text/plain
docx Document
--FileBoundary
Content-Disposition: form-data; name="Dok"; filename="Dok.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
< ./Dok.docx
--FileBoundary--
###
\ No newline at end of file
package de.h_da.fbi.smebt.intentfinder.server.sources
import de.h_da.fbi.smebt.intentfinder.server.module
import io.ktor.http.*
import io.ktor.server.testing.*
import org.junit.Test
import kotlin.test.assertEquals
class UploadTest {
@Test
fun testUploadFile() {
withTestApplication({ module(testing = true) }) {
handleRequest(HttpMethod.Post, "/file").apply {
assertEquals(HttpStatusCode.OK, response.status())
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment