Skip to content

Commit

Permalink
fix signed url
Browse files Browse the repository at this point in the history
  • Loading branch information
ANKUR DWIVEDI authored and ANKUR DWIVEDI committed May 17, 2024
1 parent 557eb49 commit 3e95f53
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
ikurl "github.com/imagekit-developer/imagekit-go/url"
)

const DEFAULT_TIMESTAMP = 9999999999

// Url generates url from UrlParam
func (ik *ImageKit) Url(params ikurl.UrlParam) (string, error) {
var resultUrl string
Expand Down Expand Up @@ -81,18 +83,29 @@ func (ik *ImageKit) Url(params ikurl.UrlParam) (string, error) {
now = params.UnixTime()
}

var expires = strconv.FormatInt(now+int64(params.ExpireSeconds), 10)
var expires string
if params.ExpireSeconds > 0 {
expires = strconv.FormatInt(now+int64(params.ExpireSeconds), 10)
} else {
expires = strconv.Itoa(DEFAULT_TIMESTAMP)
}
var path = strings.Replace(resultUrl, endpoint, "", 1)

path = path + expires
mac := hmac.New(sha1.New, []byte(ik.Config.Cloud.PrivateKey))
mac.Write([]byte(path))
signature := hex.EncodeToString(mac.Sum(nil))

if strings.Index(resultUrl, "?") > -1 {
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
if params.ExpireSeconds > 0 && params.ExpireSeconds != DEFAULT_TIMESTAMP {
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
} else {
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-s=%s", signature)
}
} else {
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
if params.ExpireSeconds > 0 && params.ExpireSeconds != DEFAULT_TIMESTAMP {
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
} else {
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-s=%s", signature)
}
}
}

Expand Down

0 comments on commit 3e95f53

Please sign in to comment.