Skip to content

Commit bc79f2d

Browse files
authored
Merge pull request #46 from bishosilwal/fix-url-generation
Fix url generation with external absolute path
2 parents 552ee9a + 2618513 commit bc79f2d

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

lib/imagekitio/sdk/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ImageKitIo
22
module Sdk
3-
VERSION = '2.2.0'
3+
VERSION = '2.2.1'
44
end
55
end

lib/imagekitio/url.rb

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,52 +46,42 @@ def build_url(options)
4646
end
4747

4848
result_url_hash = {'host': "", 'path': "", 'query': ""}
49-
existing_query=nil
49+
parsed_host = Addressable::URI.parse(url_endpoint)
50+
existing_query = nil
5051
if path != ""
5152
parsed_url = Addressable::URI.parse(path)
52-
existing_query=parsed_url.query
53-
parsed_host = Addressable::URI.parse(url_endpoint)
54-
result_url_hash[:scheme] = parsed_host.scheme
55-
5653
# making sure single '/' at end
5754
result_url_hash[:host] = parsed_host.host.to_s.chomp("/") + parsed_host.path.chomp("/") + "/"
58-
result_url_hash[:path] = trim_slash(parsed_url.path)
55+
path_without_query = Addressable::URI.parse(path)
56+
path_without_query.fragment = path_without_query.query = nil
57+
result_url_hash[:path] = path_without_query.hostname.nil? ? trim_slash(path_without_query.to_s) : CGI.escape(trim_slash(path_without_query.to_s))
5958
else
6059
parsed_url = Addressable::URI.parse(src)
61-
existing_query=parsed_url.query
62-
host = parsed_url.host
6360
result_url_hash[:userinfo] = parsed_url.userinfo if parsed_url.userinfo
64-
result_url_hash[:host] = host
65-
result_url_hash[:scheme] = parsed_url.scheme
61+
result_url_hash[:host] = parsed_url.host
6662
result_url_hash[:path] = parsed_url.path
6763
src_param_used_for_url = true
6864
end
65+
66+
existing_query = parsed_url.query
67+
result_url_hash[:scheme] = parsed_host.scheme
6968
query_params = {}
70-
if existing_query!=nil
71-
existing_query.split("&").each do |part|
72-
parts=part.split("=")
73-
if parts.length==2
74-
query_params[parts[0]]=parts[1]
75-
end
76-
end
77-
end
69+
query_params = CGI.parse(existing_query).reject {|k, v| v.empty? }.transform_values(&:first) unless existing_query.nil?
7870
options.fetch(:query_parameters, {}).each do |key, value|
79-
query_params[key]=value
71+
query_params[key] = value
8072
end
8173
transformation_str = transformation_to_str(options[:transformation]).chomp("/")
82-
8374
unless transformation_str.nil? || transformation_str.strip.empty?
8475
if (transformation_position == constants.QUERY_TRANSFORMATION_POSITION) || src_param_used_for_url == true
8576
result_url_hash[:query] = "#{constants.TRANSFORMATION_PARAMETER}=#{transformation_str}"
8677
query_params[:tr]=transformation_str
8778
else
8879
result_url_hash[:path] = "#{constants.TRANSFORMATION_PARAMETER}:#{transformation_str}/#{result_url_hash[:path]}"
8980
end
90-
9181
end
9282

9383
result_url_hash[:host] = result_url_hash[:host].to_s.reverse.chomp("/").reverse
94-
result_url_hash[:path] = result_url_hash[:path].chomp("/")
84+
result_url_hash[:path] = result_url_hash[:path].chomp("/") unless result_url_hash[:path].nil?
9585
result_url_hash[:scheme] ||= "https"
9686

9787
query_param_arr = []
@@ -103,22 +93,9 @@ def build_url(options)
10393
query_param_arr.push(key.to_s + "=" + value.to_s)
10494
end
10595
end
106-
10796
query_param_str = query_param_arr.join("&")
10897
result_url_hash[:query] = query_param_str
109-
110-
# Signature String and Timestamp
111-
# We can do this only for URLs that are created using urlEndpoint and path parameter
112-
# because we need to know the endpoint to be able to remove it from the URL to create a signature
113-
# for the remaining. With the src parameter, we would not know the "pattern" in the URL
114-
if options[:signed] && !(options[:src])
115-
intermediate_url = result_url_hash.fetch(:scheme, "") + "://" + result_url_hash.fetch(:host, "") + result_url_hash.fetch(:path, "")
116-
if result_url_hash[:query]!=nil && result_url_hash[:query]!=""
117-
intermediate_url += result_url_hash.fetch(:query, "")
118-
end
119-
end
120-
121-
url=hash_to_url(result_url_hash)
98+
url = hash_to_url(result_url_hash)
12299
if options[:signed]
123100
private_key = options[:private_key]
124101
expire_seconds = options[:expire_seconds]
@@ -129,7 +106,6 @@ def build_url(options)
129106
if expire_timestamp && (expire_timestamp != constants.TIMESTAMP)
130107
query_param_arr.push(constants.TIMESTAMP_PARAMETER + "=" + expire_timestamp.to_s)
131108
end
132-
133109
query_param_str = query_param_arr.join("&")
134110
result_url_hash[:query] = query_param_str
135111

test/imagekit/url_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@
259259
expect(url).to eq("https://imagekit.io/your-imgekit-id/tr:h-300,w-400/default-image.jpg?ik-sdk-version=ruby-#{ImageKitIo::Sdk::VERSION}&γειασας=γειασας")
260260
end
261261

262+
it 'test_generate_url_with_absolute_path' do
263+
options = { path: "https://example.com/external-image.jpg",
264+
query_parameters: { "γειασας": "γειασας" },
265+
transformation: [{ height: 300, width: 400 }]
266+
}
267+
url = url_obj.generate_url(options)
268+
269+
expect(url).to eq("https://imagekit.io/your-imgekit-id/tr:h-300,w-400/https%3A%2F%2Fexample.com%2Fexternal-image.jpg?ik-sdk-version=ruby-#{ImageKitIo::Sdk::VERSION}&γειασας=γειασας")
270+
end
271+
262272
it "test_generate_url_with_src_checking_query_param_added" do
263273

264274
options = { src: "https://ik.imagekit.io/ldt7znpgpjs/test_YhNhoRxWt.jpg?another=abc&xyz=888",
@@ -360,6 +370,19 @@
360370
expect(url).to include("https://ik.imagekit.io/your_imagekit_id/endpoint/tr:h-300,w-400/Fjällräven_KnSJwp87u6q.png?ik-sdk-version=ruby-#{ImageKitIo::Sdk::VERSION}&ik-s=")
361371
end
362372

373+
it 'test_generate_url_with_absolute_path_and_signed' do
374+
allow(url_obj).to receive(:get_signature_timestamp).with(5) { 100 }
375+
options = { path: "https://example.com/external-image.jpg",
376+
query_parameters: { "γειασας": "γειασας" },
377+
transformation: [{ height: 300, width: 400 }],
378+
signed: true,
379+
expire_seconds: 5
380+
}
381+
url = url_obj.generate_url(options)
382+
383+
expect(url).to eq("https://imagekit.io/your-imgekit-id/tr:h-300,w-400/https%3A%2F%2Fexample.com%2Fexternal-image.jpg?ik-sdk-version=ruby-#{ImageKitIo::Sdk::VERSION}&γειασας=γειασας&ik-s=e2fff55e07b923b1f0f0d484dfd966367d6d65bc&ik-t=100")
384+
end
385+
363386
it "test_url_with_invalid_args_returns_as_it_is" do
364387

365388
options = { path: "/default-image.jpg",

0 commit comments

Comments
 (0)