Commit 04a13da9 authored by Chiraz Boukadida's avatar Chiraz Boukadida
Browse files

#31 Implementation Endpoint Hochladen einer DOCX-FAQ-Datei mit Änderungen

parent 3649239c
......@@ -29,11 +29,38 @@ suspend fun main() {
println(response.readText())
println(response.status)
val fileName ="Dok1.docx"
val PutClient = HttpClient(CIO)
val putResponse: HttpResponse = PutClient.submitFormWithBinaryData(
url = "http://localhost:8080/file?filename=Dok1.docx/",
// url = "http://localhost:8080/file/{fileName}/",
// url = "http://localhost:8080/file/{$fileName}/",
// url = "http://localhost:8080/file/$fileName/",
//url = "http://localhost:8080/file/Dok1.docx/",
formData = formData {
append("description", "docx Document")
append("fileName", File("uploads/Dok1.docx").readBytes(), Headers.build {
append(
HttpHeaders.ContentType,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
append(HttpHeaders.ContentDisposition, "filename=Dok1.docx")
})
}
)
println(putResponse.readText())
println(putResponse.status)
try {
val reader = DocxReader("uploads/Dok.docx")
val fileContent =reader.readFile()
println(fileContent)
val reader1 = DocxReader("uploads/Dok1.docx")
val fileContent1 = reader1.readFile()
println(fileContent1)
} catch (exception: Exception) {
print(exception.message)
}
......
package de.h_da.fbi.smebt.intentfinder.server
import de.h_da.fbi.smebt.intentfinder.server.nlp.PythonBridge
import de.h_da.fbi.smebt.intentfinder.server.sources.DocxReader
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.http.*
......@@ -60,6 +61,38 @@ fun Application.module(testing: Boolean = false) {
call.respondText("$fileDescription is uploaded to 'uploads/$fileName'")
}
put("/file/{filename}/{id}") {
val name = call.parameters["filename"].toString()
val file = File("uploads/$name ")
val multipartData = call.receiveMultipart()
if (!file.exists()) {
call.respond(HttpStatusCode.NotFound)
}else {
multipartData.forEachPart { part ->
when (part) {
is PartData.FormItem -> {
fileDescription = part.value
}
is PartData.FileItem -> {
val reader = DocxReader("uploads/Dok.docx")
if (!reader.readFile().equals(part.streamProvider().toString())) {
fileName = part.originalFileName as String
var fileBytes = part.streamProvider().readBytes()
File("uploads/$fileName").writeBytes(fileBytes)
} else {
println("no change detected")
}
}
}
}
}
val reader = DocxReader("uploads/Dok.docx")
val fileContent = reader.readFile()
call.respondText("$fileDescription is uploaded to 'uploads/$fileName and contains: \n $fileContent' ")
}
}
registerUploadRoutes()
}
......@@ -13,4 +13,19 @@ Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.doc
< ./Dok.docx
--FileBoundary--
###
\ No newline at end of file
###
PUT POST http://0.0.0.0:8080/file/Dok1.docx/
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="Dok1.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
< ./Dok1.docx
--FileBoundary--
\ 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