-
Notifications
You must be signed in to change notification settings - Fork 16
/
validators.go
69 lines (60 loc) · 1.38 KB
/
validators.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
66
67
68
69
package main
import (
"fmt"
"log"
"mime/multipart"
"net/http"
)
func validateImage(header *multipart.FileHeader) bool {
file, err := header.Open()
if err != nil {
log.Println(err)
return false
}
defer file.Close()
buff := make([]byte, 512)
if _, err = file.Read(buff); err != nil {
log.Println(err)
return false
}
ct := http.DetectContentType(buff)
switch ct {
case "image/jpeg", "image/jpg", "image/png", "image/webp":
return true
default:
return false
}
}
func validateImageParams(imageParams *ImageParams, config *Config) error {
if config.Debug {
return nil
}
validSize := false
imageSize := fmt.Sprintf("%dx%d", imageParams.Width, imageParams.Height)
for _, size := range config.ValidImageSizes {
if size == imageSize {
validSize = true
break
}
}
if !validSize {
return fmt.Errorf(
"size=%dx%d is not supported by server. Contact server admin.",
imageParams.Width, imageParams.Height)
}
validQuality := imageParams.Quality == 0 || imageParams.Quality == config.DefaultImageQuality
if !validQuality && imageParams.Quality <= 100 && imageParams.Quality >= 10 {
for _, val := range config.ValidImageQualities {
if val == imageParams.Quality {
validQuality = true
break
}
}
}
if !validQuality {
return fmt.Errorf(
"quality=%d is not supported by server. Contact server admin.",
imageParams.Quality)
}
return nil
}