diff --git a/Sources/src/main/kotlin/allin/routing/basicRouter.kt b/Sources/src/main/kotlin/allin/routing/basicRouter.kt index f4a16e4..a9e61ef 100644 --- a/Sources/src/main/kotlin/allin/routing/basicRouter.kt +++ b/Sources/src/main/kotlin/allin/routing/basicRouter.kt @@ -4,9 +4,15 @@ import allin.model.ApiMessage import io.github.smiley4.ktorswaggerui.dsl.get import io.ktor.http.* import io.ktor.server.application.* +import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* +import kotlinx.serialization.Serializable +import java.io.File +import java.util.* +@Serializable +data class ImageResponse(val url: String) fun Application.basicRouter() { routing { @@ -23,5 +29,28 @@ fun Application.basicRouter() { }) { call.respond(ApiMessage.WELCOME) } + post("/image") { + val base64Image = call.receiveText() + val imageBytes = Base64.getDecoder().decode(base64Image) + val fileName = "${UUID.randomUUID()}.png" + val file = File("uploads/$fileName") + + file.parentFile.mkdirs() + file.writeBytes(imageBytes) + + val imageUrl = "https://codefirst.iut.uca.fr/containers/AllDev-api/images/$fileName" + call.respond(HttpStatusCode.OK, ImageResponse(imageUrl)) + } + + get("/images/{fileName}") { + val fileName = call.parameters["fileName"] + val file = File("uploads/$fileName") + + if (file.exists()) { + call.respondFile(file) + } else { + call.respond(HttpStatusCode.NotFound, "File not found") + } + } } } \ No newline at end of file