/
params.go
55 lines (42 loc) · 999 Bytes
/
params.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
package api
import (
"fmt"
"github.com/DMarby/picsum-photos/internal/database"
"github.com/DMarby/picsum-photos/internal/params"
)
// Errors
var (
ErrInvalidBlurAmount = fmt.Errorf("Invalid blur amount")
)
const (
minBlurAmount = 1
maxBlurAmount = 10
maxImageSize = 5000 // The max allowed image width/height that can be requested
)
func validateImageParams(p *params.Params) error {
if p.Width > maxImageSize {
return params.ErrInvalidSize
}
if p.Height > maxImageSize {
return params.ErrInvalidSize
}
if p.Blur && p.BlurAmount < minBlurAmount {
return ErrInvalidBlurAmount
}
if p.Blur && p.BlurAmount > maxBlurAmount {
return ErrInvalidBlurAmount
}
return nil
}
func getImageDimensions(p *params.Params, databaseImage *database.Image) (width, height int) {
// Default to the image width/height if 0 is passed
width = p.Width
height = p.Height
if width == 0 {
width = databaseImage.Width
}
if height == 0 {
height = databaseImage.Height
}
return
}