2024-04-14 20:21:45 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-04-24 21:57:13 +07:00
|
|
|
"regexp"
|
2024-05-07 22:50:23 +07:00
|
|
|
"strconv"
|
2024-04-14 20:21:45 +07:00
|
|
|
|
2024-04-24 21:57:13 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/models"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
2024-04-14 20:21:45 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/telemetry"
|
2024-05-07 22:50:23 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/views/components"
|
2024-04-14 20:21:45 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func (routes *Routes) APIDeviceCreate(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
ctx, span := tracer.Start(r.Context(), "*Routes.APIDeviceCreate")
|
|
|
|
defer func() { telemetry.EndWithStatus(span, err) }()
|
|
|
|
|
2024-04-29 21:45:18 +07:00
|
|
|
var body *models.Device
|
2024-04-14 20:21:45 +07:00
|
|
|
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to decode json body")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
_ = json.NewEncoder(rw).Encode(map[string]string{"error": fmt.Sprintf("cannot decode json body: %s", err)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-24 21:57:13 +07:00
|
|
|
if err = validateCreateDevice(body); err != nil {
|
2024-04-14 20:21:45 +07:00
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
_ = json.NewEncoder(rw).Encode(map[string]string{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-29 21:45:18 +07:00
|
|
|
device, err := routes.API.DevicesCreate(ctx, body)
|
2024-04-14 20:21:45 +07:00
|
|
|
if err != nil {
|
2024-04-25 20:22:05 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to create device", "body", body)
|
2024-04-24 22:26:04 +07:00
|
|
|
code, message := errs.HTTPMessage(err)
|
|
|
|
rw.WriteHeader(code)
|
|
|
|
_ = json.NewEncoder(rw).Encode(map[string]string{"error": message})
|
2024-04-14 20:21:45 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-24 21:57:13 +07:00
|
|
|
rw.WriteHeader(http.StatusCreated)
|
2024-04-14 20:21:45 +07:00
|
|
|
if err := json.NewEncoder(rw).Encode(device); err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to marshal json api devices")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 21:57:13 +07:00
|
|
|
var slugRegex = regexp.MustCompile(`^[a-z0-9-]+$`)
|
|
|
|
|
2024-04-29 21:45:18 +07:00
|
|
|
func validateCreateDevice(params *models.Device) error {
|
2024-04-14 20:21:45 +07:00
|
|
|
if params.Name == "" {
|
|
|
|
return errors.New("name is required")
|
|
|
|
}
|
|
|
|
if params.Slug == "" {
|
|
|
|
return errors.New("slug is required")
|
|
|
|
}
|
2024-04-24 21:57:13 +07:00
|
|
|
if !slugRegex.MatchString(params.Slug) {
|
|
|
|
return errors.New("slug must be lowercase alphanumeric with dash. eg: my-awesome-laptop")
|
|
|
|
}
|
2024-04-14 20:21:45 +07:00
|
|
|
if params.ResolutionX < 1 {
|
|
|
|
return errors.New("device width resolution is required")
|
|
|
|
}
|
|
|
|
if params.ResolutionY < 1 {
|
|
|
|
return errors.New("device height resolution is required")
|
|
|
|
}
|
2024-04-24 21:57:13 +07:00
|
|
|
if params.AspectRatioTolerance < 0 {
|
|
|
|
return errors.New("aspect ratio tolerance cannot be negative value")
|
|
|
|
}
|
2024-04-14 20:21:45 +07:00
|
|
|
if params.MaxX < 0 {
|
|
|
|
params.MaxX = 0
|
|
|
|
}
|
|
|
|
if params.MaxY < 0 {
|
|
|
|
params.MaxY = 0
|
|
|
|
}
|
|
|
|
if params.MinX < 0 {
|
|
|
|
params.MinX = 0
|
|
|
|
}
|
|
|
|
if params.MinY < 0 {
|
|
|
|
params.MinY = 0
|
|
|
|
}
|
2024-04-24 21:57:13 +07:00
|
|
|
if params.NSFW < 0 {
|
|
|
|
params.NSFW = 0
|
|
|
|
}
|
|
|
|
if params.NSFW > 1 {
|
|
|
|
params.NSFW = 1
|
|
|
|
}
|
|
|
|
if params.WindowsWallpaperMode < 0 {
|
|
|
|
params.WindowsWallpaperMode = 0
|
|
|
|
}
|
|
|
|
if params.WindowsWallpaperMode > 1 {
|
|
|
|
params.WindowsWallpaperMode = 1
|
|
|
|
}
|
2024-04-14 20:21:45 +07:00
|
|
|
return nil
|
|
|
|
}
|
2024-05-07 22:50:23 +07:00
|
|
|
|
|
|
|
func (routes *Routes) DevicesCreateHTMX(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
var err error
|
|
|
|
ctx, span := tracer.Start(req.Context(), "*Routes.DevicesCreateHTMX")
|
|
|
|
defer func() { telemetry.EndWithStatus(span, err) }()
|
|
|
|
|
|
|
|
device, err := createDeviceFromParams(req)
|
|
|
|
if err != nil {
|
|
|
|
rw.WriteHeader(400)
|
|
|
|
if err := components.ErrorToast(err.Error()).Render(ctx, rw); err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to render error notification")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = routes.API.DevicesCreate(ctx, device)
|
|
|
|
if err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to create device", "device", device)
|
|
|
|
code, message := errs.HTTPMessage(err)
|
|
|
|
rw.WriteHeader(code)
|
|
|
|
if err := components.ErrorToast(message).Render(ctx, rw); err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to render error notification")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.Header().Set("HX-Redirect", "/devices")
|
|
|
|
rw.WriteHeader(http.StatusCreated)
|
|
|
|
|
|
|
|
if err := components.SuccessToast("device created").Render(ctx, rw); err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to render success notification")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDeviceFromParams(req *http.Request) (*models.Device, error) {
|
|
|
|
device := new(models.Device)
|
|
|
|
|
|
|
|
device.Enable = 1
|
|
|
|
device.Name = req.FormValue("name")
|
|
|
|
device.Slug = req.FormValue("slug")
|
|
|
|
device.ResolutionX, _ = strconv.ParseFloat(req.FormValue("resolution_x"), 32)
|
|
|
|
device.ResolutionY, _ = strconv.ParseFloat(req.FormValue("resolution_y"), 32)
|
|
|
|
device.AspectRatioTolerance, _ = strconv.ParseFloat(req.FormValue("aspect_ratio_tolerance"), 32)
|
|
|
|
|
|
|
|
maxX, _ := strconv.ParseInt(req.FormValue("max_x"), 10, 32)
|
|
|
|
device.MaxX = int32(maxX)
|
|
|
|
|
|
|
|
maxY, _ := strconv.ParseInt(req.FormValue("max_y"), 10, 32)
|
|
|
|
device.MaxY = int32(maxY)
|
|
|
|
|
|
|
|
minX, _ := strconv.ParseInt(req.FormValue("min_x"), 10, 32)
|
|
|
|
device.MinX = int32(minX)
|
|
|
|
|
|
|
|
minY, _ := strconv.ParseInt(req.FormValue("min_y"), 10, 32)
|
|
|
|
device.MinY = int32(minY)
|
|
|
|
|
|
|
|
nsfw, _ := strconv.ParseInt(req.FormValue("nsfw"), 10, 32)
|
|
|
|
device.NSFW = int32(nsfw)
|
|
|
|
|
|
|
|
windowsWallpaperMode, _ := strconv.ParseInt(req.FormValue("windows_wallpaper_mode"), 10, 32)
|
|
|
|
device.WindowsWallpaperMode = int32(windowsWallpaperMode)
|
|
|
|
|
|
|
|
return device, validateCreateDevice(device)
|
|
|
|
}
|