Skip to content

Commit 3e95f53

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
fix signed url
1 parent 557eb49 commit 3e95f53

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

url.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
ikurl "github.com/imagekit-developer/imagekit-go/url"
1414
)
1515

16+
const DEFAULT_TIMESTAMP = 9999999999
17+
1618
// Url generates url from UrlParam
1719
func (ik *ImageKit) Url(params ikurl.UrlParam) (string, error) {
1820
var resultUrl string
@@ -81,18 +83,29 @@ func (ik *ImageKit) Url(params ikurl.UrlParam) (string, error) {
8183
now = params.UnixTime()
8284
}
8385

84-
var expires = strconv.FormatInt(now+int64(params.ExpireSeconds), 10)
86+
var expires string
87+
if params.ExpireSeconds > 0 {
88+
expires = strconv.FormatInt(now+int64(params.ExpireSeconds), 10)
89+
} else {
90+
expires = strconv.Itoa(DEFAULT_TIMESTAMP)
91+
}
8592
var path = strings.Replace(resultUrl, endpoint, "", 1)
86-
8793
path = path + expires
8894
mac := hmac.New(sha1.New, []byte(ik.Config.Cloud.PrivateKey))
8995
mac.Write([]byte(path))
9096
signature := hex.EncodeToString(mac.Sum(nil))
91-
9297
if strings.Index(resultUrl, "?") > -1 {
93-
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
98+
if params.ExpireSeconds > 0 && params.ExpireSeconds != DEFAULT_TIMESTAMP {
99+
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
100+
} else {
101+
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-s=%s", signature)
102+
}
94103
} else {
95-
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
104+
if params.ExpireSeconds > 0 && params.ExpireSeconds != DEFAULT_TIMESTAMP {
105+
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
106+
} else {
107+
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-s=%s", signature)
108+
}
96109
}
97110
}
98111

0 commit comments

Comments
 (0)