Commit a956ae62 authored by amrabdou's avatar amrabdou
Browse files

[hotfix, #0] Hotfix for the demo

   - Added incomplete API Endpoint
   - Added DOCX Reader to API
   - Tested both locally and on docker
parent c564cad8
Pipeline #74075 passed with stages
in 2 minutes and 20 seconds
......@@ -47,64 +47,6 @@ fun Application.module() {
}
// Definition eines Endpunkts zum Hochladen einer DOCX-Datei
post("/file/{chatbotId}") {
call.respondText("file was successful uploaded")
}
// Definition endpoint zur Änderung eine bereits existierende docx
put("/file/{chatbotId}/{id}/{filename}") {
}
// Definition eines Endpunkts zur Definition einer FAQ-Webseite mit JSON-Konfiguration
post("/faqResource/{chatbotId}/{jsonStructure}") {
var jsonObj: JSONUrlConfig? = null
var qAaMap: HashMap<String, String>? = null
try {
//parse file title
var title = call.parameters["jsonStructure"]
if (title != null) {
title = title.replace(".json", "", false)
}
val multipart = call.receiveMultipart()
val fileReader = FileReader(multipart, title)
val file = fileReader.readFile()
//building json String from File
val stringList = file.readLines()
var json = ""
for (element in stringList) {
json += element
}
//Generate DataObject from json string
val moshiReader = MoshiReader("url")
jsonObj = moshiReader.getObjectFromJson(json) as JSONUrlConfig
//extract question answer pairs
val urlReader = UrlReader()
val urlDoc = urlReader.getDocument(jsonObj.name)
qAaMap = urlReader.extraction(
urlDoc,
jsonObj.question.css_selector,
jsonObj.answer.css_selector,
jsonObj.answer.type
)
} catch (exception: Exception) {
call.respondText(UrlReaderException("").getExceptionText(exception))
}
//speichern in db vgl. #39
call.respondText(
"{number_of_pairs: " +
qAaMap?.size +
"}"
)
}
// get faq with Json Configuration
get("/faqResource") {
......@@ -112,7 +54,7 @@ fun Application.module() {
}
// Definition eines Endpunkts zur Definition einer Docx-Datei mit JSON-Konfiguration
post("/docxResource/{chatbotId}/{filename}") {
post("{chatbotId}/docxResource/{filename}") {
}
......
package de.h_da.fbi.smebt.intentfinder.server.sources
data class JSONDocxConfig(
val type:String,
val name: String,
val question:QuestionDocx
val type:String,
var name: String,
val question:QuestionDocx
)
data class QuestionDocx(
val style:String,
......
package de.h_da.fbi.smebt.intentfinder.server.upload
import de.h_da.fbi.smebt.intentfinder.server.sources.*
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.http.content.*
......@@ -20,14 +21,20 @@ data class FileStatus(val path: String, val status: String)
fun Route.apiRouting() {
route("/") {
get("/") {
call.respondText("IntentFinder is available")
}
// Definition eines Endpunkts zum Hochladen einer DOCX-Datei
post("{chatbotId}/file") {
var fileDescription = ""
var fileName = ""
var status = "error"
var path=""
var path = ""
val multipartData = call.receiveMultipart()
val pathFile = call.parameters["chatbotId"].toString()
val pathAsFile = File("uploads/$pathFile")
println(pathAsFile.toPath().toAbsolutePath())
if (!Files.exists(Paths.get(pathFile))) {
pathAsFile.mkdir()
}
......@@ -45,7 +52,7 @@ fun Route.apiRouting() {
file.createNewFile()
file.writeBytes(fileBytes)
status = "created"
path= file.path
path = file.path
} else {
call.respond(HttpStatusCode.InternalServerError, "file alredy exist")
}
......@@ -55,6 +62,8 @@ fun Route.apiRouting() {
//'uploads/$pathFile/$fileName'
call.respond(FileStatus(path = "$path", status = "$status"))
}
// Definition endpoint zur Änderung eine bereits existierende docx
put("{chatbotId}/file/{id}/{filename}") {
var fileDescription = ""
......@@ -85,7 +94,7 @@ fun Route.apiRouting() {
val date = Date(time)
val format = SimpleDateFormat("yyMMddHHmm")
val current = format.format(date)
newName = file.nameWithoutExtension +"_"+ current + "." + file.extension
newName = file.nameWithoutExtension + "_" + current + "." + file.extension
var fileBytes = part.streamProvider().readBytes()
File("uploads/$pathFile/$newName").writeBytes(fileBytes)
fileContent = part.streamProvider().reader().readText()
......@@ -101,8 +110,105 @@ fun Route.apiRouting() {
call.respond(FileStatus(path = "'/uploads/$pathFile/$newName'", status = "$status"))
}
// Definition eines Endpunkts zur Definition einer FAQ-Webseite mit JSON-Konfiguration
post("{chatbotId}/faqResource/{jsonStructure}") {
var jsonObj: JSONUrlConfig? = null
var qAaMap: HashMap<String, String>? = null
try {
//parse file title
var title = call.parameters["jsonStructure"]
if (title != null) {
title = title.replace(".json", "", false)
}
val multipart = call.receiveMultipart()
val fileReader = FileReader(multipart, title)
val file = fileReader.readFile()
//building json String from File
val stringList = file.readLines()
var json = ""
for (element in stringList) {
json += element
}
//Generate DataObject from json string
val moshiReader = MoshiReader("url")
jsonObj = moshiReader.getObjectFromJson(json) as JSONUrlConfig
//extract question answer pairs
val urlReader = UrlReader()
val urlDoc = urlReader.getDocument(jsonObj.name)
qAaMap = urlReader.extraction(
urlDoc,
jsonObj.question.css_selector,
jsonObj.answer.css_selector,
jsonObj.answer.type
)
} catch (exception: Exception) {
call.respondText(UrlReaderException("").getExceptionText(exception))
}
//speichern in db vgl. #39
call.respondText(
"{number_of_pairs: " +
qAaMap?.size +
"}"
)
}
// get docx file with Json Configuration
post("{chatbotId}/docxResource/{jsonStructure}") {
var jsonObj: JSONDocxConfig? = null
var qAaMap: Map<String, String>? = null
try {
//parse file title
var title = call.parameters["jsonStructure"]
if (title != null) {
title = title!!.replace(".json", "", false)
}
println("Title => " + title)
val multipart = call.receiveMultipart()
val fileReader = FileReader(multipart, title)
val file = fileReader.readFile()
//building json String from File
val stringList = file.readLines()
var json = ""
for (element in stringList) {
json += element
}
//Generate DataObject from json string
val moshiReader = MoshiReader("docx")
jsonObj = moshiReader.getObjectFromJson(json) as JSONDocxConfig
val path = "/home/gradle/src/build/install/ktor-server/bin/uploads/" + call.parameters["chatbotId"].toString() + "/"
jsonObj!!.name = path + jsonObj!!.name
//extract question answer pairs
val reader = DocxReader(jsonObj!!)
qAaMap = reader.getQuestionAnswerPairs()
} catch (exception: Exception) {
call.respondText(UrlReaderException("").getExceptionText(exception))
}
//speichern in db vgl. #39
call.respondText(
"{number_of_pairs: " +
qAaMap?.size +
"}"
)
}
}
}
fun Application.registerApiRoutes() {
routing {
apiRouting()
......
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