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
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.*
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
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)
......@@ -42,52 +55,12 @@ fun Application.module(testing: Boolean = false) {
}
// 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("/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"
post("/{chatbotId}/file") {
var fileDescription = ""
var fileName = ""
var status = "error"
val multipartData = call.receiveMultipart()
val pathFile = call.parameters["chatbotId"].toString()
val pathFile = call.parameters["chatbotId"].toString()
val pathAsFile = File("uploads/$pathFile")
if (!Files.exists(Paths.get(pathFile))) {
pathAsFile.mkdir()
......@@ -104,62 +77,58 @@ fun Application.module(testing: Boolean = false) {
if (!file.exists()) {
file.createNewFile()
file.writeBytes(fileBytes)
//call.respond("file not found but created")
status ="created"
}else {
file.writeBytes(fileBytes)
status="updated"
status = "created"
} else {
call.respond(HttpStatusCode.InternalServerError, "file alredy exist")
}
}
}
}
call.respond(FileStatus(path="'uploads/$pathFile/$fileName'",status="$status"))
call.respond(FileStatus(path = "'uploads/$pathFile/$fileName'", status = "$status"))
}
put("/file/{chatbotId}/{id}/{filename}") {
val name = call.parameters["filename"].toString()
// Definition endpoint zur Änderung eine bereits existierende docx
put("/{chatbotId}/file/{id}/{filename}") {
var fileDescription = ""
var fileName = ""
var newName = ""
val name = call.parameters["filename"].toString()
val pathFile = call.parameters["chatbotId"].toString()
var status ="error"
val pathAsFile = File("uploads/$pathFile")
if (!Files.exists(Paths.get(pathFile))) {
pathAsFile.mkdir()
}
var status = "error"
val file = File("uploads/$pathFile/$name ")
val multipartData = call.receiveMultipart()
var fileContent =""
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"))
{
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")
if (!fileId.equals(call.parameters["id"].toString()))
call.respond(HttpStatusCode.NotFound)
}
}
is PartData.FileItem -> {
if (file.exists()) {
if (file.readText().equals(part.streamProvider().toString())) {
call.respondText("file exist but no change detected")
} else {
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()
fileName = part.originalFileName as String
val time = System.currentTimeMillis()
val date = Date(time)
val format = SimpleDateFormat("yyMMddHHmm")
val current = format.format(date)
newName = file.nameWithoutExtension +"_"+ current + "." + file.extension
var fileBytes = part.streamProvider().readBytes()
file.writeBytes(fileBytes)
status ="created"
File("uploads/$pathFile/$newName").writeBytes(fileBytes)
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) {
call.respond(FileStatus(path="'uploads/$pathFile/$fileName'",status="$status"))
call.respond(FileStatus(path = "'uploads/$pathFile/$newName'", status = "$status"))
}
}
registerUploadRoutes()
}
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