Commit 0e711893 authored by Chiraz Boukadida's avatar Chiraz Boukadida
Browse files

[fix,31] versioning

parent 45dc3284
Pipeline #73287 passed with stages
in 2 minutes and 16 seconds
package de.h_da.fbi.smebt.intentfinder.server 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.nlp.PythonBridge
import de.h_da.fbi.smebt.intentfinder.server.sources.DocxReader
import io.ktor.application.* import io.ktor.application.*
import io.ktor.features.* import io.ktor.features.*
import io.ktor.http.* import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.* import io.ktor.response.*
import io.ktor.routing.* import io.ktor.routing.*
import io.ktor.serialization.* import io.ktor.serialization.*
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import registerUploadRoutes import registerUploadRoutes
import java.io.File
import java.lang.RuntimeException import java.lang.RuntimeException
import java.nio.file.Files
import java.nio.file.Paths
import kotlinx.serialization.Serializable
import java.text.SimpleDateFormat
import java.util.*
import io.ktor.http.content.*
@Serializable
data class FileStatus(val path: String, val status: String)
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args) fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
...@@ -42,52 +55,12 @@ fun Application.module(testing: Boolean = false) { ...@@ -42,52 +55,12 @@ fun Application.module(testing: Boolean = false) {
} }
// Definition eines Endpunkts zum Hochladen einer DOCX-Datei // Definition eines Endpunkts zum Hochladen einer DOCX-Datei
post("/file/{chatbotId}") { post("/{chatbotId}/file") {
call.respondText("file was successful uploaded") var fileDescription = ""
} var fileName = ""
var status = "error"
// 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("/faqRessource/{chatbotId}/{jsonStructure}"){
}
// get faq with Json Configuration
get("/faqRessource"){
//Rückgabe Json Object (vgl. #38)
}
// Definition eines Endpunkts zur Definition einer Docx-Datei mit JSON-Konfiguration
post("/docxRessource/{chatbotId}/{filename}"){
}
// get docx file with Json Configuration
get("/docxRessource"){
//Rückgabe Json Object (vgl. #37)
}
// Definition endpoint zum Auslesen aller hochgeladener docx mit status
get("/files"){
}
// Routen ohne Funktionalität
routing{
}
var fileDescription = ""
var fileName = ""
post("/file/{chatbotId}") {
var status ="error"
val multipartData = call.receiveMultipart() val multipartData = call.receiveMultipart()
val pathFile = call.parameters["chatbotId"].toString() val pathFile = call.parameters["chatbotId"].toString()
val pathAsFile = File("uploads/$pathFile") val pathAsFile = File("uploads/$pathFile")
if (!Files.exists(Paths.get(pathFile))) { if (!Files.exists(Paths.get(pathFile))) {
pathAsFile.mkdir() pathAsFile.mkdir()
...@@ -104,62 +77,58 @@ fun Application.module(testing: Boolean = false) { ...@@ -104,62 +77,58 @@ fun Application.module(testing: Boolean = false) {
if (!file.exists()) { if (!file.exists()) {
file.createNewFile() file.createNewFile()
file.writeBytes(fileBytes) file.writeBytes(fileBytes)
//call.respond("file not found but created") status = "created"
status ="created" } else {
}else { call.respond(HttpStatusCode.InternalServerError, "file alredy exist")
file.writeBytes(fileBytes)
status="updated"
} }
} }
} }
} }
call.respond(FileStatus(path="'uploads/$pathFile/$fileName'",status="$status")) call.respond(FileStatus(path = "'uploads/$pathFile/$fileName'", status = "$status"))
} }
put("/file/{chatbotId}/{id}/{filename}") { // Definition endpoint zur Änderung eine bereits existierende docx
val name = call.parameters["filename"].toString() put("/{chatbotId}/file/{id}/{filename}") {
var fileDescription = ""
var fileName = ""
var newName = ""
val name = call.parameters["filename"].toString()
val pathFile = call.parameters["chatbotId"].toString() val pathFile = call.parameters["chatbotId"].toString()
var status ="error" var status = "error"
val pathAsFile = File("uploads/$pathFile")
if (!Files.exists(Paths.get(pathFile))) {
pathAsFile.mkdir()
}
val file = File("uploads/$pathFile/$name ") val file = File("uploads/$pathFile/$name ")
val multipartData = call.receiveMultipart() val multipartData = call.receiveMultipart()
var fileContent ="" var fileContent = ""
multipartData.forEachPart { part -> multipartData.forEachPart { part ->
when (part) { when (part) {
is PartData.FormItem -> { is PartData.FormItem -> {
if (part.name.toString().equals("description")) if (part.name.toString().equals("description")) {
{ fileDescription = part.value
fileDescription = part.value }
} if (part.name.toString().equals("id")) {
if (part.name.toString().equals("id"))
{
var fileId = part.value var fileId = part.value
if ( !fileId.equals(call.parameters["id"].toString())) if (!fileId.equals(call.parameters["id"].toString()))
call.respond("file not found") call.respond(HttpStatusCode.NotFound)
} }
} }
is PartData.FileItem -> { is PartData.FileItem -> {
if (file.exists()) { if (file.exists()) {
fileName = part.originalFileName as String
if (file.readText().equals(part.streamProvider().toString())) { val time = System.currentTimeMillis()
call.respondText("file exist but no change detected") val date = Date(time)
} else { val format = SimpleDateFormat("yyMMddHHmm")
fileName = part.originalFileName as String val current = format.format(date)
var fileBytes = part.streamProvider().readBytes() newName = file.nameWithoutExtension +"_"+ current + "." + file.extension
File("uploads/$pathFile/$fileName").writeBytes(fileBytes)
fileContent= part.streamProvider().reader().readText()
status ="updated"
}
}else{
file.createNewFile()
var fileBytes = part.streamProvider().readBytes() var fileBytes = part.streamProvider().readBytes()
file.writeBytes(fileBytes) File("uploads/$pathFile/$newName").writeBytes(fileBytes)
status ="created" fileContent = part.streamProvider().reader().readText()
status = "updated"
} else {
call.respond(HttpStatusCode.NotFound, "file not found")
} }
} }
...@@ -168,13 +137,15 @@ fun Application.module(testing: Boolean = false) { ...@@ -168,13 +137,15 @@ fun Application.module(testing: Boolean = false) {
call.respond(FileStatus(path="'uploads/$pathFile/$fileName'",status="$status")) call.respond(FileStatus(path = "'uploads/$pathFile/$newName'", status = "$status"))
} }
} }
registerUploadRoutes() registerUploadRoutes()
} }
class InternalServerErrorException : RuntimeException() class InternalServerErrorException : RuntimeException()
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