-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquery.go
More file actions
122 lines (102 loc) · 2.58 KB
/
query.go
File metadata and controls
122 lines (102 loc) · 2.58 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"io"
"net/url"
"regexp"
"strconv"
"strings"
)
var SupportOperators map[string]string = map[string]string{
"w": "resize",
"h": "resize",
"c": "crop",
"q": "quality", // only jpeg
"grayscale": "grayscale",
"sepia": "sepia",
"contrast": "contrast",
"brightness": "brightness",
"saturation": "saturation",
"colorize": "colorize",
"colorbalance": "colorbalance",
}
type Query struct {
Raw string
SourceUrl string
IsExternalSource bool
params map[string]string
}
func NewQuery() *Query {
return &Query{}
}
func (query *Query) Clear() {
query.Raw = ""
query.SourceUrl = ""
query.IsExternalSource = false
query.params = make(map[string]string)
}
var regexp_protocol *RegexpUtil = &RegexpUtil{regexp.MustCompile(`^/(?P<protocol>http|https):/`)}
var regexp_params *RegexpUtil = &RegexpUtil{regexp.MustCompile(`(?P<operator>^[a-z]+)(?P<value>[0-9,-]+$)`)}
func (query *Query) Parse(urlString string) bool {
query.Clear()
query.Raw = urlString
u, err := url.Parse(query.Raw)
if err != nil {
return false
}
s := strings.Split(u.Path, ":")
match_protocol := regexp_protocol.FindStringSubmatchMap(u.Path)
if len(match_protocol) > 0 {
query.IsExternalSource = true
query.SourceUrl = match_protocol["protocol"] + ":/" + s[1]
} else {
query.IsExternalSource = false
query.SourceUrl = LoadConfig().Origin.Url + s[0]
}
match_param := map[string]string{}
for _, v := range s {
match_param = regexp_params.FindStringSubmatchMap(v)
if len(match_param) > 0 {
if _, ok := SupportOperators[match_param["operator"]]; ok {
query.params[match_param["operator"]] = match_param["value"]
}
}
}
return true
}
func (query *Query) Stringify() string {
if query.Count() == 0 {
return "default"
}
j, err := json.Marshal(query.params)
if err != nil {
return "default"
}
h := sha1.New()
io.WriteString(h, string(j))
return hex.EncodeToString(h.Sum(nil))
}
func (query *Query) Count() int {
return len(query.params)
}
func (query *Query) Has(operator string) bool {
_, exists := query.params[operator]
return exists
}
func (query *Query) GetInt(operator string) (value int) {
if v, exists := query.params[operator]; exists {
value, _ = strconv.Atoi(v)
}
return value
}
func (query *Query) GetIntArray(operator string) (values []int) {
if v, exists := query.params[operator]; exists {
for _, s := range strings.Split(v, ",") {
i, _ := strconv.Atoi(s)
values = append(values, i)
}
}
return values
}