-
Notifications
You must be signed in to change notification settings - Fork 11
/
url.go
105 lines (88 loc) · 2.15 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
package gothumbor
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"net/url"
"strings"
)
type ThumborOptions struct {
Trim bool
Width int
Height int
VAlign string
Smart bool
FitIn bool
Filters []string
Flip bool
Flop bool
Meta bool
Left int
Top int
Right int
Bottom int
}
func GetCryptedThumborPath(key, imageURL string, options ThumborOptions) (url string, err error) {
var partial string
if partial, err = GetThumborPath(imageURL, options); err != nil {
return
}
hash := hmac.New(sha1.New, []byte(key))
hash.Write([]byte(partial))
message := hash.Sum(nil)
url = base64.URLEncoding.EncodeToString(message)
url = strings.Join([]string{url, partial}, "/")
return
}
func GetThumborPath(imageURL string, options ThumborOptions) (path string, err error) {
return getURLParts(imageURL, options)
}
func getURLParts(imageURL string, options ThumborOptions) (urlPartial string, err error) {
var parts []string
partialObject, err := url.Parse(imageURL)
if err != nil {
return "", err
}
newImageURL := partialObject.String()
if options.Meta {
parts = append(parts, "meta")
}
if options.Trim {
parts = append(parts, "trim")
}
if options.FitIn {
parts = append(parts, "fit-in")
}
if options.Left != 0 || options.Top != 0 || options.Right != 0 || options.Bottom != 0 {
parts = append(parts, fmt.Sprintf("%dx%d:%dx%d", options.Left, options.Top, options.Right, options.Bottom))
}
if options.Height != 0 || options.Width != 0 || options.Flip || options.Flop {
flip := ""
flop := ""
if options.Flip {
flip = "-"
}
if options.Flop {
flop = "-"
}
parts = append(parts, fmt.Sprintf("%s%dx%s%d", flip, options.Width, flop, options.Height))
}
if options.VAlign != "" {
parts = append(parts, options.VAlign)
}
if options.Smart {
parts = append(parts, "smart")
}
filters := []string{}
for _, value := range options.Filters {
filters = append(filters, value)
}
if len(options.Filters) > 0 {
filtersValue := strings.Join(filters, ":")
parts = append(parts, "filters:"+filtersValue)
}
parts = append(parts, newImageURL)
urlPartial = strings.Join(parts, "/")
return
}