Commit 2ae87e62 authored by Chiraz Boukadida's avatar Chiraz Boukadida
Browse files

[feat/#31] Implementation Endpoint Hochladen einer DOCX-FAQ-Datei mit Änderungen

parent 04a13da9
Pipeline #72885 passed with stages
in 2 minutes and 17 seconds
......@@ -21,5 +21,26 @@ Persistierung der ausgelesenen Daten.
**2. Sprint: 12.5. - 9.6**
**Sprintziel:**
Extrahierung einzelner Fragen und dazugehörigen Antworten von unterschiedlichen Medien.
Fragemöglichkeit generieren. Persistierung der ausgelesenen Daten.
#27 Definition guter Testfälle und Beispiele (FAQ, ...)
#41 Spike: Fragengenerierung
#25 Recherche/Definition Endpoints
#26 Chatbot-Vokabeln
#33 Abbildung der JSON-Konfiguration in eine geeignete Kotlin-Datenstruktur und Implementation des zugehörigen Endpoints
#30 Implementation Endpoint Hochladen einer DOCX-Datei
#31 Implementation Endpoint Hochladen einer DOCX-FAQ-Datei mit Änderungen
#34 Definition einer MongoDB-Struktur
#36 Methoden zum Lesen, Schreiben und Ändern der MongoDB
#38 Extraction von Fragen und Antworten einer HTML-FAQ-Datei auf Basis einer Konfiguration
#37 Extraction von Fragen und Antworten einer DOCX-FAQ-Datei auf Basis einer Konfiguration
#39 Daten in die MongoDB schreiben und Zusammenfassung einbinden
#28 Überarbeitung ID-Generierung
#40 Einlesen und Parsen einer HTML-Seite mit JSOUP, Zerlegen in semantische Blöcke
#43 Question Generator
#55 Fragemöglichkeit mit Chatette generieren
#56 Fragemöglichkeit mit Bibliothek Question Generator generieren
**3. Sprint: 9.6. - 7.7**
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)
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)
}
}
\ No newline at end of file
......@@ -14,6 +14,13 @@ import io.ktor.serialization.*
import kotlinx.serialization.json.Json
import registerUploadRoutes
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import kotlinx.serialization.Serializable
@Serializable
data class FileStatus(val path: String, val status: String)
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
......@@ -36,10 +43,14 @@ fun Application.module(testing: Boolean = false) {
var fileDescription = ""
var fileName = ""
post("/file") {
post("/file/{chatbotId}") {
var status ="error"
val multipartData = call.receiveMultipart()
val pathFile = call.parameters["chatbotId"].toString()
val pathAsFile = File("uploads/$pathFile")
if (!Files.exists(Paths.get(pathFile))) {
pathAsFile.mkdir()
}
multipartData.forEachPart { part ->
when (part) {
is PartData.FormItem -> {
......@@ -48,49 +59,75 @@ fun Application.module(testing: Boolean = false) {
is PartData.FileItem -> {
fileName = part.originalFileName as String
var fileBytes = part.streamProvider().readBytes()
val file = File("uploads/$fileName")
val file = File("uploads/$pathFile/$fileName")
if (!file.exists()) {
call.respond(HttpStatusCode.NotFound)
file.createNewFile()
file.writeBytes(fileBytes)
//call.respond("file not found but created")
status ="created"
}else {
File("uploads/$fileName").writeBytes(fileBytes)
file.writeBytes(fileBytes)
status="updated"
}
}
}
}
call.respondText("$fileDescription is uploaded to 'uploads/$fileName'")
call.respond(FileStatus(path="'uploads/$pathFile/$fileName'",status="$status"))
}
put("/file/{filename}/{id}") {
put("/file/{chatbotId}/{id}/{filename}") {
val name = call.parameters["filename"].toString()
val file = File("uploads/$name ")
val pathFile = call.parameters["chatbotId"].toString()
var status ="error"
val pathAsFile = File("uploads/$pathFile")
if (!Files.exists(Paths.get(pathFile))) {
pathAsFile.mkdir()
}
val file = File("uploads/$pathFile/$name ")
val multipartData = call.receiveMultipart()
if (!file.exists()) {
call.respond(HttpStatusCode.NotFound)
}else {
var fileContent =""
multipartData.forEachPart { part ->
when (part) {
is PartData.FormItem -> {
if (part.name.toString().equals("description"))
{
fileDescription = part.value
}
if (part.name.toString().equals("id"))
{
var fileId = part.value
if ( !fileId.equals(call.parameters["id"].toString()))
call.respond("file not found")
}
}
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)
if (file.exists()) {
if (file.readText().equals(part.streamProvider().toString())) {
call.respondText("file exist but no change detected")
} else {
println("no change detected")
fileName = part.originalFileName as String
var fileBytes = part.streamProvider().readBytes()
File("uploads/$pathFile/$fileName").writeBytes(fileBytes)
fileContent= part.streamProvider().reader().readText()
status ="updated"
}
}else{
file.createNewFile()
var fileBytes = part.streamProvider().readBytes()
file.writeBytes(fileBytes)
status ="created"
}
}
}
}
val reader = DocxReader("uploads/Dok.docx")
val fileContent = reader.readFile()
call.respondText("$fileDescription is uploaded to 'uploads/$fileName and contains: \n $fileContent' ")
call.respond(FileStatus(path="'uploads/$pathFile/$fileName'",status="$status"))
}
}
......
......@@ -7,15 +7,20 @@ Content-Type: text/plain
docx Document
--FileBoundary
Content-Disposition: form-data; name="id"
Content-Type: text/plain
1
--FileBoundary
Content-Disposition: form-data; name="Dok"; filename="Dok.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
< ./Dok.docx
--FileBoundary--
###
PUT POST http://0.0.0.0:8080/file/Dok1.docx/
PUT http://0.0.0.0:8080/file/Dok1.docx
Content-Type: multipart/form-data;boundary=FileBoundary
--FileBoundary
......@@ -24,8 +29,16 @@ Content-Type: text/plain
docx Document
--FileBoundary
Content-Disposition: form-data; name="id"
Content-Type: text/plain
1
--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
......@@ -16,4 +16,12 @@ class UploadTest {
}
}
}
@Test
fun testUploadFileChange() {
withTestApplication({ module(testing = true) }) {
handleRequest(HttpMethod.Put, "/file/Dok1.docx").apply {
assertEquals(HttpStatusCode.OK, response.status())
}
}
}
}
\ No newline at end of file
Supports Markdown
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