imagekitio 1.0.8 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +292 -82
  3. data/Rakefile +1 -1
  4. data/lib/active_storage/active_storage.rb +2 -0
  5. data/lib/active_storage/service/ik_file.rb +111 -0
  6. data/lib/active_storage/service/image_kit_io_service.rb +186 -0
  7. data/lib/carrierwave/carrierwave.rb +79 -0
  8. data/lib/carrierwave/storage/ik_file.rb +8 -7
  9. data/lib/carrierwave/storage/imagekit_store.rb +54 -54
  10. data/lib/carrierwave/support/uri_filename.rb +9 -7
  11. data/lib/imagekitio/api_service/bulk.rb +58 -0
  12. data/lib/imagekitio/api_service/custom_metadata_field.rb +52 -0
  13. data/lib/imagekitio/api_service/file.rb +162 -0
  14. data/lib/imagekitio/api_service/folder.rb +49 -0
  15. data/lib/imagekitio/base.rb +12 -0
  16. data/lib/imagekitio/client.rb +186 -0
  17. data/lib/imagekitio/configurable.rb +43 -0
  18. data/lib/imagekitio/constant.rb +36 -0
  19. data/lib/imagekitio/constants/default.rb +22 -0
  20. data/lib/imagekitio/constants/error.rb +69 -0
  21. data/lib/imagekitio/constants/file.rb +11 -0
  22. data/lib/imagekitio/constants/supported_transformation.rb +64 -0
  23. data/lib/imagekitio/constants/url.rb +14 -0
  24. data/lib/imagekitio/errors.rb +4 -0
  25. data/lib/imagekitio/railtie.rb +1 -1
  26. data/lib/imagekitio/request.rb +79 -0
  27. data/lib/imagekitio/sdk/version.rb +5 -0
  28. data/lib/imagekitio/url.rb +241 -0
  29. data/lib/imagekitio/utils/calculation.rb +44 -0
  30. data/lib/imagekitio/utils/formatter.rb +48 -0
  31. data/lib/imagekitio/utils/option_validator.rb +36 -0
  32. data/lib/imagekitio.rb +10 -83
  33. metadata +55 -15
  34. data/lib/imagekit/constants/defaults.rb +0 -20
  35. data/lib/imagekit/constants/errors.rb +0 -77
  36. data/lib/imagekit/constants/file.rb +0 -5
  37. data/lib/imagekit/constants/supported_transformation.rb +0 -57
  38. data/lib/imagekit/constants/url.rb +0 -9
  39. data/lib/imagekit/file.rb +0 -133
  40. data/lib/imagekit/imagekit.rb +0 -117
  41. data/lib/imagekit/resource.rb +0 -56
  42. data/lib/imagekit/sdk/version.rb +0 -5
  43. data/lib/imagekit/url.rb +0 -236
  44. data/lib/imagekit/utils/calculation.rb +0 -36
  45. data/lib/imagekit/utils/formatter.rb +0 -29
data/lib/imagekit/url.rb DELETED
@@ -1,236 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Url holds url generation method
4
-
5
- require "cgi"
6
- require "openssl"
7
- require_relative "./utils/formatter"
8
- require_relative "./constants/defaults"
9
- require_relative "./constants/supported_transformation"
10
- require_relative "./sdk/version.rb"
11
-
12
- class Url
13
- def initialize(request_obj)
14
- @req_obj = request_obj
15
- end
16
-
17
- def generate_url(options)
18
- if options.key? :src
19
- options[:transformation_position] = Default::TRANSFORMATION_POSITION
20
- end
21
- extended_options = extend_url_options(options)
22
- build_url(extended_options)
23
- end
24
-
25
- def build_url(options)
26
- # build url from all options
27
-
28
- path = options.fetch(:path, "")
29
- src = options.fetch(:src, "")
30
- url_endpoint = options.fetch(:url_endpoint, "")
31
- transformation_position = options[:transformation_position]
32
-
33
- unless Default::VALID_TRANSFORMATION_POSITION.include? transformation_position
34
- raise ArgumentError, INVALID_TRANSFORMATION_POS
35
- end
36
-
37
- src_param_used_for_url = false
38
- if (src != "") || (transformation_position == Default::QUERY_TRANSFORMATION_POSITION)
39
- src_param_used_for_url = true
40
- end
41
-
42
- if path == "" && src == ""
43
- return ""
44
- end
45
-
46
- result_url_hash = {'host': "", 'path': "", 'query': ""}
47
- existing_query=nil
48
- if path != ""
49
- parsed_url = URI.parse(path)
50
- existing_query=parsed_url.query
51
- parsed_host = URI(url_endpoint)
52
- result_url_hash[:scheme] = parsed_host.scheme
53
-
54
- # making sure single '/' at end
55
- result_url_hash[:host] = parsed_host.host.to_s.chomp("/") + parsed_host.path.chomp("/") + "/"
56
- result_url_hash[:path] = trim_slash(parsed_url.path)
57
- else
58
- parsed_url = URI.parse(src)
59
- existing_query=parsed_url.query
60
- host = parsed_url.host
61
- result_url_hash[:userinfo] = parsed_url.userinfo if parsed_url.userinfo
62
- result_url_hash[:host] = host
63
- result_url_hash[:scheme] = parsed_url.scheme
64
- result_url_hash[:path] = parsed_url.path
65
- src_param_used_for_url = true
66
- end
67
- query_params = {}
68
- if existing_query!=nil
69
- existing_query.split("&").each do |part|
70
- parts=part.split("=")
71
- if parts.length==2
72
- query_params[parts[0]]=parts[1]
73
- end
74
- end
75
- end
76
- options.fetch(:query_parameters, {}).each do |key, value|
77
- query_params[key]=value
78
- end
79
- transformation_str = transformation_to_str(options[:transformation]).chomp("/")
80
-
81
- unless transformation_str.nil? || transformation_str.strip.empty?
82
- if (transformation_position == Default::QUERY_TRANSFORMATION_POSITION) || src_param_used_for_url == true
83
- result_url_hash[:query] = "#{Default::TRANSFORMATION_PARAMETER}=#{transformation_str}"
84
- query_params[:tr]=transformation_str
85
- else
86
- result_url_hash[:path] = "#{Default::TRANSFORMATION_PARAMETER}:#{transformation_str}/#{result_url_hash[:path]}"
87
- end
88
-
89
- end
90
-
91
- result_url_hash[:host] = result_url_hash[:host].to_s.reverse.chomp("/").reverse
92
- result_url_hash[:path] = result_url_hash[:path].chomp("/")
93
- result_url_hash[:scheme] ||= "https"
94
-
95
- query_param_arr = []
96
- query_param_arr.push("ik-sdk-version=ruby-"+Imagekit::Sdk::VERSION)
97
- query_params.each do |key, value|
98
- if value.to_s == ""
99
- query_param_arr.push(key.to_s)
100
- else
101
- query_param_arr.push(key.to_s + "=" + value.to_s)
102
- end
103
- end
104
-
105
- query_param_str = query_param_arr.join("&")
106
- result_url_hash[:query] = query_param_str
107
-
108
- # Signature String and Timestamp
109
- # We can do this only for URLs that are created using urlEndpoint and path parameter
110
- # because we need to know the endpoint to be able to remove it from the URL to create a signature
111
- # for the remaining. With the src parameter, we would not know the "pattern" in the URL
112
- if options[:signed] && !(options[:src])
113
- intermediate_url = result_url_hash.fetch(:scheme, "") + "://" + result_url_hash.fetch(:host, "") + result_url_hash.fetch(:path, "")
114
- if result_url_hash[:query]!=nil && result_url_hash[:query]!=""
115
- intermediate_url += result_url_hash.fetch(:query, "")
116
- end
117
- end
118
-
119
- url=hash_to_url(result_url_hash)
120
- if options[:signed]
121
- private_key = options[:private_key]
122
- expire_seconds = options[:expire_seconds]
123
- expire_timestamp = get_signature_timestamp(expire_seconds)
124
- url_signature = get_signature(private_key, url, url_endpoint, expire_timestamp)
125
- query_param_arr.push(Default::SIGNATURE_PARAMETER + "=" + url_signature)
126
-
127
- if expire_timestamp && (expire_timestamp != Default::TIMESTAMP)
128
- query_param_arr.push(Default::TIMESTAMP_PARAMETER + "=" + expire_timestamp.to_s)
129
- end
130
-
131
- query_param_str = query_param_arr.join("&")
132
- result_url_hash[:query] = query_param_str
133
-
134
- url=hash_to_url(result_url_hash)
135
- end
136
- url
137
- end
138
-
139
- def transformation_to_str(transformation)
140
- # creates transformation_position string for url
141
- # from transformation dictionary
142
-
143
- unless transformation.is_a?(Array)
144
- return ""
145
- end
146
-
147
- parsed_transforms = []
148
- (0..(transformation.length - 1)).each do |i|
149
- parsed_transform_step = []
150
-
151
- transformation[i].keys.each do |key|
152
- transform_key = SUPPORTED_TRANS.fetch(key, nil)
153
- transform_key ||= key
154
-
155
- if transform_key == "oi" || transform_key == "di"
156
- transformation[i][key][0] = "" if transformation[i][key][0] == "/"
157
- transformation[i][key] = transformation[i][key].gsub("/", "@@")
158
- end
159
-
160
- if transformation[i][key] == "-"
161
- parsed_transform_step.push(transform_key)
162
- else
163
- parsed_transform_step.push("#{transform_key}#{Default::TRANSFORM_KEY_VALUE_DELIMITER}#{transformation[i][key]}")
164
- end
165
- end
166
- parsed_transforms.push(parsed_transform_step.join(Default::TRANSFORM_DELIMITER))
167
- end
168
- parsed_transforms.join(Default::CHAIN_TRANSFORM_DELIMITER)
169
- end
170
-
171
- def get_signature_timestamp(seconds)
172
- # this function returns either default time stamp
173
- # or current unix time and expiry seconds to get
174
- # signature time stamp
175
-
176
- if seconds.to_i == 0
177
- Default::DEFAULT_TIMESTAMP
178
- else
179
- DateTime.now.strftime("%s").to_i + seconds.to_i
180
- end
181
- end
182
-
183
- def get_signature(private_key, url, url_endpoint, expiry_timestamp)
184
- # creates signature(hashed hex key) and returns from
185
- # private_key, url, url_endpoint and expiry_timestamp
186
- if expiry_timestamp==0
187
- expiry_timestamp=Default::DEFAULT_TIMESTAMP
188
- end
189
- if url_endpoint[url_endpoint.length-1]!="/"
190
- url_endpoint+="/"
191
- end
192
- replaced_url=url.gsub(url_endpoint, "")
193
- replaced_url = replaced_url + expiry_timestamp.to_s
194
- OpenSSL::HMAC.hexdigest("SHA1", private_key, replaced_url)
195
- end
196
-
197
- def extend_url_options(options)
198
- attr_dict = {"public_key": @req_obj.public_key,
199
- "private_key": @req_obj.private_key,
200
- "url_endpoint": @req_obj.url_endpoint,
201
- "transformation_position": @req_obj.transformation_position, }
202
- # extending url options
203
- attr_dict.merge(options)
204
- end
205
-
206
- def hash_to_url(url_hash)
207
- generated_url = url_hash.fetch(:scheme, "") + "://" + url_hash.fetch(:host, "") + url_hash.fetch(:path, "")
208
- if url_hash[:query] != ""
209
- generated_url = generated_url + "?" + url_hash.fetch(:query, "")
210
- return generated_url
211
- end
212
- generated_url
213
- end
214
-
215
- def trim_slash(str, both = true)
216
- if str == ""
217
- return ""
218
- end
219
- # remove slash from a string
220
- # if both is not provide trims both slash
221
- # example - '/abc/' returns 'abc'
222
- # if both=false it will only trim end slash
223
- # example - '/abc/' returns '/abc'
224
- # NOTE: IT'S RECOMMENDED TO USE inbuilt .chomp('string you want to remove')
225
- # FOR REMOVING ONLY TRAILING SLASh
226
- if both
227
- str[0].chomp("/") + str[1..-2] + str[-1].chomp("/")
228
- else
229
- str.chomp("/")
230
- end
231
- end
232
-
233
- # class Imagekit
234
-
235
- # end
236
- end
@@ -1,36 +0,0 @@
1
- require "date"
2
- require "securerandom"
3
-
4
- DEFAULT_TIME_DIFF = 60 * 30
5
-
6
- def is_valid_hex(hex_string)
7
- # checks if hexadecimal value is valid or not
8
- /^[[:xdigit:]]+$/ === hex_string
9
- end
10
-
11
- def hamming_distance(first, second)
12
- # Calculate Hamming distance between to hex string
13
- unless is_valid_hex(first) && is_valid_hex(second)
14
- raise ArgumentError, "Both argument should be hexadecimal"
15
- end
16
- a = first.to_i(16)
17
- b = second.to_i(16)
18
- (a ^ b).to_s(2).count("1")
19
- end
20
-
21
- def get_authenticated_params(token, expire, private_key)
22
- # return authenticated param
23
- default_expire = DateTime.now.strftime("%s").to_i + DEFAULT_TIME_DIFF
24
- token ||= SecureRandom.uuid
25
-
26
- auth_params = {'token': token, 'expire': expire, 'signature': ""}
27
- unless private_key
28
- return nil
29
- end
30
-
31
- signature = OpenSSL::HMAC.hexdigest("SHA1", private_key, token.to_s + expire.to_s)
32
- auth_params[:token] = token
33
- auth_params[:expire] = expire || default_expire
34
- auth_params[:signature] = signature
35
- auth_params
36
- end
@@ -1,29 +0,0 @@
1
- def snake_to_camel(word)
2
- word_list = word.split("_")
3
- result = []
4
- word_list&.each { |i|
5
- if i == word_list[0]
6
- result.push(i)
7
- else
8
- result.push(i.capitalize)
9
- end
10
- }
11
- result.join
12
- end
13
-
14
- def camel_to_snake(camel_word)
15
- # convert camel case to snake case
16
- camel_word.to_s.gsub(/::/, "/")
17
- .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
18
- .gsub(/([a-z\d])([A-Z])/, '\1_\2')
19
- .tr("-", "_")
20
- .downcase
21
- end
22
-
23
- def request_formatter(data)
24
- result = {}
25
- data.each do |key, val|
26
- result[snake_to_camel(key.to_s)] = val
27
- end
28
- result
29
- end