package helper import ( "image" "net/http" ) // Checks if the subreact exists, responds with a bad status when false. func IsValidSubreact(subreact string, writer http.ResponseWriter) bool { switch subreact { case "": fallthrough case "t": fallthrough case "g": fallthrough case "k": fallthrough case "p": fallthrough case "a": fallthrough case "pr": fallthrough case "m": break default: WriteErrorJson("invalid subreact", writer, http.StatusBadRequest) return false } return true } // Range checks the length of the argument, responds with a bad status when false. func IsValidRange(str string, name string, min int, max int, writer http.ResponseWriter) bool { if len(str) < min { WriteErrorJson(name+" too short", writer, http.StatusBadRequest) return false } if len(str) > max { WriteErrorJson(name+" too long", writer, http.StatusBadRequest) return false } return true } // Checks that the image has supported properties, responds with a bad status when false. func IsImageSupported(image image.Image, format string, writer http.ResponseWriter) bool { if bounds := image.Bounds(); bounds.Dx() > 4096 || bounds.Dy() > 4096 { WriteErrorJson("image dimensions too large", writer, http.StatusBadRequest) return false } else if bounds.Dx() < 256 || bounds.Dy() < 256 { WriteErrorJson("image dimensions too small", writer, http.StatusBadRequest) return false } switch format { case "png": fallthrough case "jpeg": fallthrough case "gif": break default: WriteErrorJson("image format no recognised", writer, http.StatusBadRequest) return false } return true }