aboutsummaryrefslogtreecommitdiff
path: root/src/server/helper/valid.go
blob: 9363747375c8f962d540567ecd3f3664d5aecb17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
}