-
Notifications
You must be signed in to change notification settings - Fork 1
/
size.go
65 lines (51 loc) · 1.28 KB
/
size.go
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
package main
import (
"net/http"
"strconv"
"strings"
"github.com/labstack/echo/v4"
)
type Size struct {
Height uint64
Width uint64
}
var invalidSizeErr = echo.NewHTTPError(http.StatusBadRequest, "invalid size format, read document for more details or file an issue")
// ParseSize return a error when size in invalid format
func ParseSize(s string) (Size, error) {
userSize := strings.ToLower(s)
var size Size
var err error
width, height, found := strings.Cut(userSize, "x")
size.Width, err = strconv.ParseUint(width, 10, 64)
if err != nil {
return Size{}, invalidSizeErr
}
if found {
size.Height, err = strconv.ParseUint(height, 10, 64)
if err != nil {
return Size{}, invalidSizeErr
}
}
return size, nil
}
func validWidth(width uint64) bool {
if width == 100 || width == 200 || width == 400 || width == 600 || width == 800 || width == 1200 {
return true
}
return false
}
func validHeight(height uint64) bool {
if height == 100 || height == 200 || height == 400 || height == 600 || height == 800 || height == 1200 {
return true
}
return false
}
func validSize(size Size) bool {
if size.Height == 0 {
return validWidth(size.Width)
}
if size.Width == 0 {
return validHeight(size.Height)
}
return validWidth(size.Width) && validHeight(size.Height)
}