mirror of
https://github.com/kaaninchen/Laterna.git
synced 2026-09-17 19:12:48 +00:00
validate provided HEX-Code
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/siestaw/laterna/server/cmd/internal/logger"
|
||||
"github.com/siestaw/laterna/server/cmd/internal/models"
|
||||
"github.com/siestaw/laterna/server/cmd/utils"
|
||||
)
|
||||
|
||||
func ViewColor(id int) (*models.LampState, error) {
|
||||
@@ -19,6 +22,9 @@ func ViewColor(id int) (*models.LampState, error) {
|
||||
}
|
||||
|
||||
func SetColor(id int, color string) error {
|
||||
if !utils.IsValidHexColor(color) {
|
||||
return errors.New("invalid HEX Code: %s")
|
||||
}
|
||||
currentColor, err := ViewColor(id)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -20,6 +21,11 @@ func IDtoInt(id string) (int, error) {
|
||||
}
|
||||
return idInt, nil
|
||||
}
|
||||
func IsValidHexColor(color string) bool {
|
||||
pattern := `^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$`
|
||||
match, _ := regexp.MatchString(pattern, color)
|
||||
return match
|
||||
}
|
||||
|
||||
func HTTPErrorHandling(w http.ResponseWriter, r *http.Request, status int, message string) {
|
||||
timestamp := time.Now().Format("2006-01-02_15-04-05")
|
||||
|
||||
Reference in New Issue
Block a user