Skip to content

Commit f636130

Browse files
committed
refactored and fix url generation bug
1 parent 552ee9a commit f636130

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

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

0 commit comments

Comments
 (0)