upload roomplan

This commit is contained in:
mbremer
2021-07-31 16:18:51 +02:00
parent ccf130ad28
commit 78bb174be6
5 changed files with 102 additions and 2 deletions

View File

@@ -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;
}

View File

@@ -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();
}
}