Skip to content

Commit 1d7e6e6

Browse files
authored
Merge pull request #7 from imagekit-developer/IK-1458
fix signed url
2 parents 557eb49 + 2a2da58 commit 1d7e6e6

2 files changed

Lines changed: 84 additions & 51 deletions

File tree

imagekit_test.go

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
neturl "net/url"
55
"os"
66
"reflect"
7-
"regexp"
8-
"sort"
9-
"strings"
107
"testing"
118

129
"github.com/google/go-cmp/cmp"
@@ -63,7 +60,7 @@ func TestUrl(t *testing.T) {
6360
},
6461
},
6562
},
66-
url: "https://imagekit.io/343534/tr:w-100,rt-90/default-image.jpg",
63+
url: "https://imagekit.io/343534/tr:rt-90,w-100/default-image.jpg",
6764
}, {
6865
name: "signed-url",
6966
params: ikurl.UrlParam{
@@ -76,6 +73,57 @@ func TestUrl(t *testing.T) {
7673
},
7774
url: "https://ik.imagekit.io/test/default-image.jpg?ik-t=1653775928&ik-s=48842eca663c6895331331db6c90f262c601f4e8",
7875
}, {
76+
name: "signed-url-with-transformation",
77+
params: ikurl.UrlParam{
78+
Path: "default-image.jpg",
79+
Signed: true,
80+
ExpireSeconds: 100,
81+
UnixTime: func() int64 {
82+
return 1653775828
83+
},
84+
Transformations: []map[string]any{
85+
{
86+
"height": 300,
87+
"width": 300,
88+
},
89+
},
90+
},
91+
url: "https://ik.imagekit.io/test/tr:h-300,w-300/default-image.jpg?ik-t=1653775928&ik-s=1a74eab9fca6fa0bb2298aa07f4e3892a925a508",
92+
},
93+
{
94+
name: "signed-url-with-transformation-in-query",
95+
params: ikurl.UrlParam{
96+
Path: "default-image.jpg",
97+
Signed: true,
98+
ExpireSeconds: 100,
99+
UnixTime: func() int64 {
100+
return 1653775828
101+
},
102+
TransformationPosition: "query",
103+
Transformations: []map[string]any{
104+
{
105+
"height": 300,
106+
"width": 300,
107+
},
108+
},
109+
},
110+
url: "https://ik.imagekit.io/test/default-image.jpg?tr=h-300%2Cw-300&ik-t=1653775928&ik-s=55f319d3a7db76e652545599a57af3dd94e32e24",
111+
},
112+
{
113+
name: "signed-url-without-ExpireSeconds",
114+
params: ikurl.UrlParam{
115+
Path: "default-image.jpg",
116+
Signed: true,
117+
Transformations: []map[string]any{
118+
{
119+
"height": 300,
120+
"width": 300,
121+
},
122+
},
123+
},
124+
url: "https://ik.imagekit.io/test/tr:h-300,w-300/default-image.jpg?ik-s=355f6c8a91031847828169116fd1d1db6e2aa8c7",
125+
},
126+
{
79127
name: "src-with-transformation",
80128
params: ikurl.UrlParam{
81129
Src: "https://imagekit.io/343534/default-image.jpg",
@@ -86,7 +134,7 @@ func TestUrl(t *testing.T) {
86134
},
87135
},
88136
},
89-
url: "https://imagekit.io/343534/default-image.jpg?tr=w-100,rt-90",
137+
url: "https://imagekit.io/343534/default-image.jpg?tr=rt-90%2Cw-100",
90138
}, {
91139
name: "src-without-transformation",
92140
params: ikurl.UrlParam{
@@ -106,7 +154,7 @@ func TestUrl(t *testing.T) {
106154
},
107155
TransformationPosition: ikurl.QUERY,
108156
},
109-
url: "https://imagekit.io/343534/default-image.jpg?tr=w-100%2crt-90",
157+
url: "https://imagekit.io/343534/default-image.jpg?tr=rt-90%2Cw-100",
110158
}, {
111159
name: "transformations",
112160
params: ikurl.UrlParam{
@@ -141,7 +189,7 @@ func TestUrl(t *testing.T) {
141189
},
142190
},
143191
},
144-
url: "https://ik.imagekit.io/dk1m7xkgi/tr:w-200,h-400,cm-extract,fo-center,x-100,y-110,q-85,f-auto,bl-5,dpr-auto,e-grayscale,di-test2_hBIIEweBy.gif,pr-true,lo-true,t-true,b-5_005500,cp-true,md-true,rt-auto,r-40,bg-344222,e-sharpen,e-contrast,x-1/default-image.jpg",
192+
url: "https://ik.imagekit.io/dk1m7xkgi/tr:b-5_005500,bg-344222,bl-5,cm-extract,cp-true,di-test2_hBIIEweBy.gif,dpr-auto,e-contrast,e-grayscale,e-sharpen,f-auto,fo-center,h-400,lo-true,md-true,pr-true,q-85,r-40,rt-auto,t-true,w-200,x-1,x-100,y-110/default-image.jpg",
145193
}, {
146194
name: "aspect-ratio-xc-yc",
147195
params: ikurl.UrlParam{
@@ -163,7 +211,7 @@ func TestUrl(t *testing.T) {
163211
},
164212
},
165213
},
166-
url: "https://ik.imagekit.io/test/tr:w-200,ar-16-9,cm-extract,fo-center,xc-100,yc-110,q-85,f-auto,bl-50,dpr-2,rt-90,e-sharpen-40/default-image.jpg",
214+
url: "https://ik.imagekit.io/test/tr:ar-16-9,bl-50,cm-extract,dpr-2,e-sharpen-40,f-auto,fo-center,q-85,rt-90,w-200,xc-100,yc-110/default-image.jpg",
167215
}, {
168216
name: "unsharp-mask",
169217
params: ikurl.UrlParam{
@@ -189,7 +237,7 @@ func TestUrl(t *testing.T) {
189237
},
190238
},
191239
},
192-
url: "https://ik.imagekit.io/343534/tr:w-100,h-200:rt-90/default-image.jpg",
240+
url: "https://ik.imagekit.io/343534/tr:h-200,w-100:rt-90/default-image.jpg",
193241
}, {
194242

195243
name: "common-overlay-options",
@@ -206,7 +254,7 @@ func TestUrl(t *testing.T) {
206254
},
207255
},
208256
},
209-
url: "https://ik.imagekit.io/test/tr:ox-100,oy-110,oh-100,ow-90,obg-443322,ofo-bottom/default-image.jpg",
257+
url: "https://ik.imagekit.io/test/tr:obg-443322,ofo-bottom,oh-100,ow-90,ox-100,oy-110/default-image.jpg",
210258
}, {
211259
name: "text-overlay-options",
212260
params: ikurl.UrlParam{
@@ -229,7 +277,7 @@ func TestUrl(t *testing.T) {
229277
},
230278
},
231279
},
232-
url: "https://ik.imagekit.io/test/tr:ot-this%20is%20a%20sample%20overlay,ox-100,oy-110,oh-500,ow-900,otbg-ffffff,otp-20_40,otia-right,otc-blue,otf-Arvo,ots-40,ott-ib,or-20/default-image.jpg",
280+
url: "https://ik.imagekit.io/test/tr:oh-500,or-20,ot-this%20is%20a%20sample%20overlay,otbg-ffffff,otc-blue,otf-Arvo,otia-right,otp-20_40,ots-40,ott-ib,ow-900,ox-100,oy-110/default-image.jpg",
233281
}, {
234282
name: "image-overlay-options",
235283
params: ikurl.UrlParam{
@@ -251,7 +299,7 @@ func TestUrl(t *testing.T) {
251299
},
252300
},
253301
},
254-
url: "https://ik.imagekit.io/test/tr:oi-test2_hBIIEweBy.gif,ox-100,oy-110,oh-200,ow-200,oib-4_blue,oidpr-0.2,oiq-80,oic-at_max,oix-100,oiy-20,oit-false/default-image.jpg",
302+
url: "https://ik.imagekit.io/test/tr:oh-200,oi-test2_hBIIEweBy.gif,oib-4_blue,oic-at_max,oidpr-0.2,oiq-80,oit-false,oix-100,oiy-20,ow-200,ox-100,oy-110/default-image.jpg",
255303
},
256304
}
257305

@@ -262,45 +310,14 @@ func TestUrl(t *testing.T) {
262310
if err != nil {
263311
t.Errorf(err.Error())
264312
}
265-
266-
if strings.Index(tc.url, "tr:") > -1 {
267-
url, tr := extractTransformation(t, url)
268-
expectedUrl, expectedTr := extractTransformation(t, tc.url)
269-
270-
if !urlsEquals(url, expectedUrl) {
271-
t.Errorf("expected url: %s\ngot: %s", expectedUrl, url)
272-
}
273-
274-
if !cmp.Equal(tr, expectedTr) {
275-
t.Errorf("url: %s, expected tr: %s\ngot tr: %s", tc.url, expectedTr, tr)
276-
}
313+
if !urlsEquals(url, tc.url) {
314+
t.Errorf("expected url: %s\ngot: %s", tc.url, url)
277315
}
278316
})
279317
}
280318

281319
}
282320

283-
func extractTransformation(t *testing.T, url string) (urlResult string, trResult []string) {
284-
re := regexp.MustCompile("tr:(.+)/")
285-
m := re.FindStringSubmatch(url)
286-
287-
if m == nil {
288-
t.Error("transformation not found")
289-
return
290-
}
291-
292-
urlResult = strings.Replace(url, "tr:"+m[1], "", 1)
293-
trs := strings.Split(m[1], ":")
294-
295-
for _, tr := range trs {
296-
parts := strings.Split(tr, ",")
297-
sort.Strings(parts)
298-
trResult = append(trResult, parts...)
299-
}
300-
301-
return urlResult, trResult
302-
}
303-
304321
func urlsEquals(url1 string, url2 string) bool {
305322
u1, _ := neturl.Parse(url1)
306323
u2, _ := neturl.Parse(url2)

url.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import (
66
"encoding/hex"
77
"fmt"
88
neturl "net/url"
9+
"sort"
910
"strconv"
1011
"strings"
1112
"time"
1213

1314
ikurl "github.com/imagekit-developer/imagekit-go/url"
1415
)
1516

17+
const DEFAULT_TIMESTAMP = 9999999999
18+
1619
// Url generates url from UrlParam
1720
func (ik *ImageKit) Url(params ikurl.UrlParam) (string, error) {
1821
var resultUrl string
@@ -42,7 +45,7 @@ func (ik *ImageKit) Url(params ikurl.UrlParam) (string, error) {
4245
} else {
4346
if params.TransformationPosition == ikurl.QUERY {
4447
params.QueryParameters["tr"] = joinTransformations(params.Transformations...)
45-
url, err = neturl.Parse(endpoint + params.Path)
48+
url, err = neturl.Parse(strings.TrimRight(endpoint, "/") + "/" + strings.TrimLeft(params.Path, "/"))
4649

4750
} else {
4851
url, err = neturl.Parse(url.String() +
@@ -81,18 +84,30 @@ func (ik *ImageKit) Url(params ikurl.UrlParam) (string, error) {
8184
now = params.UnixTime()
8285
}
8386

84-
var expires = strconv.FormatInt(now+int64(params.ExpireSeconds), 10)
87+
var expires string
88+
if params.ExpireSeconds > 0 {
89+
expires = strconv.FormatInt(now+int64(params.ExpireSeconds), 10)
90+
} else {
91+
expires = strconv.Itoa(DEFAULT_TIMESTAMP)
92+
}
8593
var path = strings.Replace(resultUrl, endpoint, "", 1)
86-
94+
path = strings.TrimPrefix(path, "/")
8795
path = path + expires
8896
mac := hmac.New(sha1.New, []byte(ik.Config.Cloud.PrivateKey))
8997
mac.Write([]byte(path))
9098
signature := hex.EncodeToString(mac.Sum(nil))
91-
9299
if strings.Index(resultUrl, "?") > -1 {
93-
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
100+
if params.ExpireSeconds > 0 && params.ExpireSeconds != DEFAULT_TIMESTAMP {
101+
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
102+
} else {
103+
resultUrl = resultUrl + "&" + fmt.Sprintf("ik-s=%s", signature)
104+
}
94105
} else {
95-
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
106+
if params.ExpireSeconds > 0 && params.ExpireSeconds != DEFAULT_TIMESTAMP {
107+
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-t=%s&ik-s=%s", expires, signature)
108+
} else {
109+
resultUrl = resultUrl + "?" + fmt.Sprintf("ik-s=%s", signature)
110+
}
96111
}
97112
}
98113

@@ -134,6 +149,7 @@ func transform(tr map[string]any) string {
134149
parts = append(parts, prefix+"-"+value)
135150
}
136151
}
152+
sort.Strings(parts)
137153

138154
return strings.Join(parts, ",")
139155
}

0 commit comments

Comments
 (0)