-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudinary.go
49 lines (39 loc) · 1.16 KB
/
cloudinary.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
package cloudinary
import (
"crypto/sha1"
"encoding/base64"
"strings"
)
const (
baseUrl = "https://res.cloudinary.com"
defaultResourceType = "image"
)
type Client struct {
cloudName string
secure bool
apiKey string
apiSecret string
}
func NewClient(cloudName string, apiKey string, apiSecret string) Client {
return Client{
cloudName: cloudName,
secure: true,
apiKey: apiKey,
apiSecret: apiSecret,
}
}
func (c *Client) SignUrlByRawTransformation(productId string, accessType string, rawTransformation string) string {
seed := rawTransformation + "/" + productId + c.apiSecret
hash := sha1.New()
hash.Write([]byte(seed))
b := hash.Sum(nil)
signature := base64.StdEncoding.EncodeToString(b)
signature = strings.Replace(signature, "+", "-", -1)
signature = strings.Replace(signature, "/", "_", -1)
signature = string([]rune(signature)[:8])
signature = "s--" + signature + "--"
transformation := strings.Replace(rawTransformation, "\n", "%0A", -1)
signUrl := baseUrl + "/" + c.cloudName + "/" + defaultResourceType + "/" + accessType
signUrl = signUrl + "/" + signature + "/" + transformation + "/" + productId
return signUrl
}