-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
156 lines (134 loc) · 3.07 KB
/
url.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package pxl
import (
"errors"
"image/color"
"net/url"
"path"
"strconv"
"strings"
)
func DecodeURL(url *url.URL) (Pxl, *EncodingOptions, error) {
pxl, err := decodeURLPath(url.Path)
if err != nil {
return nil, nil, err
}
opts, err := decodeURLQuery(url.Query())
if err != nil {
return nil, nil, err
}
return pxl, opts, err
}
func decodeURLPath(pth string) (Pxl, error) {
pth = pth[:len(pth)-len(path.Ext(pth))]
if pth == "" || pth[0] != '/' {
return nil, errors.New("invalid path")
}
prts := strings.Split(pth[1:], "/")
pxl := New(len(prts))
for y, prt := range prts {
row, err := strconv.ParseUint(prt, 10, 64)
if err != nil {
return nil, err
}
pxl[y] = row
}
return pxl, nil
}
func decodeURLQuery(params url.Values) (*EncodingOptions, error) {
opts := &EncodingOptions{
Fg: defaultEncodingOptions.Fg,
Bg: defaultEncodingOptions.Bg,
Fc: defaultEncodingOptions.Fc,
Bc: defaultEncodingOptions.Bc,
Fps: defaultEncodingOptions.Fps,
Scale: defaultEncodingOptions.Scale,
}
if str := params.Get("fg"); str != "" {
clr, err := decodeURLColor(str)
if err != nil {
return opts, err
}
opts.Fg = clr
}
if str := params.Get("bg"); str != "" {
clr, err := decodeURLColor(str)
if err != nil {
return opts, err
}
opts.Bg = clr
}
if str := params.Get("fc"); str != "" {
chars := []rune(str)
if len(chars) != 1 {
return opts, errors.New("fc too long")
}
opts.Fc = chars[0]
}
if str := params.Get("bc"); str != "" {
chars := []rune(str)
if len(chars) != 1 {
return opts, errors.New("bc too long")
}
opts.Bc = chars[0]
}
if str := params.Get("fps"); str != "" {
fps, err := strconv.ParseUint(str, 10, 8)
if err != nil {
return opts, err
}
if fps > 100 {
return opts, errors.New("fps out of range")
}
opts.Fps = int(fps)
}
if str := params.Get("scale"); str != "" {
scale, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return opts, err
}
opts.Scale = int(scale)
}
return opts, nil
}
func decodeURLColor(s string) (color.NRGBA, error) {
val, err := strconv.ParseUint(s, 10, 32)
clr := color.NRGBA{
R: uint8((val >> 24) & 0xff),
G: uint8((val >> 16) & 0xff),
B: uint8((val >> 8) & 0xff),
A: uint8((val >> 0) & 0xff),
}
return clr, err
}
func EncodeURL(pxl Pxl, opts *EncodingOptions) *url.URL {
return &url.URL{
Path: encodeURLPath(pxl),
RawQuery: encodeURLQuery(opts).Encode(),
}
}
func encodeURLPath(pxl Pxl) string {
b := &strings.Builder{}
for _, row := range pxl {
b.WriteByte('/')
b.WriteString(strconv.FormatUint(row, 10))
}
return b.String()
}
func encodeURLQuery(opts *EncodingOptions) url.Values {
return url.Values{
"bg": {encodeURLColor(opts.Bg)},
"fg": {encodeURLColor(opts.Fg)},
"bc": {string(opts.Bc)},
"fc": {string(opts.Fc)},
"fps": {strconv.Itoa(opts.Fps)},
"scale": {strconv.Itoa(opts.Scale)},
}
}
func encodeURLColor(clr color.NRGBA) string {
var num uint64
num |= uint64(clr.R) << 24
num |= uint64(clr.G) << 16
num |= uint64(clr.B) << 8
num |= uint64(clr.A) << 0
return strconv.FormatUint(num, 10)
}