diff --git a/src/main/java/de/mbremer/room/RoomFileForm.java b/src/main/java/de/mbremer/room/RoomFileForm.java new file mode 100644 index 0000000..9f65f09 --- /dev/null +++ b/src/main/java/de/mbremer/room/RoomFileForm.java @@ -0,0 +1,13 @@ +package de.mbremer.room; + +import org.jboss.resteasy.annotations.providers.multipart.PartType; + +import javax.ws.rs.FormParam; +import javax.ws.rs.core.MediaType; +import java.io.InputStream; + +public class RoomFileForm { + @FormParam("file") + @PartType(MediaType.APPLICATION_OCTET_STREAM) + public InputStream file; +} diff --git a/src/main/java/de/mbremer/room/RoomResource.java b/src/main/java/de/mbremer/room/RoomResource.java new file mode 100644 index 0000000..278fd7d --- /dev/null +++ b/src/main/java/de/mbremer/room/RoomResource.java @@ -0,0 +1,67 @@ +package de.mbremer.room; + +import io.quarkus.qute.Template; +import io.quarkus.qute.TemplateInstance; +import io.quarkus.security.identity.SecurityIdentity; +import org.jboss.logging.Logger; +import org.jboss.resteasy.annotations.providers.multipart.MultipartForm; + +import javax.annotation.security.RolesAllowed; +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.IOException; + +@Path("/room") +@ApplicationScoped +public class RoomResource { + + public static final int MAX_PLANSIZE_IN_BYTES = 1000 * 1024; + @Inject + Logger log; + @Inject + SecurityIdentity identity; + @Inject + Template room; + + private byte[] roomPlan = new byte[0]; + + @GET + @Produces(MediaType.TEXT_HTML) + @RolesAllowed({"USER", "ADMIN"}) + public TemplateInstance getRoom() { + return room + .data("is_admin", identity.hasRole("ADMIN")); + } + + @POST + @Path("/plan") + @Consumes(MediaType.MULTIPART_FORM_DATA) + @Produces(MediaType.TEXT_HTML) + @RolesAllowed({"ADMIN"}) + public TemplateInstance uploadRoomplan(@MultipartForm RoomFileForm roomFileForm) throws IOException { + byte[] newRoomPlan = roomFileForm.file.readAllBytes(); + int newPlanSize = newRoomPlan.length; + + TemplateInstance room = getRoom(); + // validate + log.info("Uploaded " + newPlanSize + " Bytes Roomplan" ); + if (newPlanSize > MAX_PLANSIZE_IN_BYTES) { + room.data("error", "Raumplan ist zu groß: " + newPlanSize + " Bytes. Maximal " + MAX_PLANSIZE_IN_BYTES + " Bytes erlaubt."); + } else { + roomPlan = newRoomPlan; + } + return room; + } + + @GET + @Path("/plan") + @Produces("image/*") + @RolesAllowed({"USER", "ADMIN"}) + public Response getImage(@PathParam("image") String image) { + + return roomPlan.length < 1 ? Response.noContent().build() : Response.ok(roomPlan).build(); + } +} \ No newline at end of file diff --git a/src/main/resources/templates/base.html b/src/main/resources/templates/base.html index e359813..bc52ffb 100644 --- a/src/main/resources/templates/base.html +++ b/src/main/resources/templates/base.html @@ -19,6 +19,7 @@
diff --git a/src/main/resources/templates/room.html b/src/main/resources/templates/room.html new file mode 100644 index 0000000..55cbaa2 --- /dev/null +++ b/src/main/resources/templates/room.html @@ -0,0 +1,19 @@ +{#include base.html} +{#room_active}active{/} +{#contents} + +{#if is_admin} +