imagekitio 1.0.10 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +185 -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 -51
  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 +41 -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 -237
  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,237 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Url holds url generation method
4
-
5
- require "cgi"
6
- require "addressable/uri"
7
- require "openssl"
8
- require_relative "./utils/formatter"
9
- require_relative "./constants/defaults"
10
- require_relative "./constants/supported_transformation"
11
- require_relative "./sdk/version.rb"
12
-
13
- class Url
14
- def initialize(request_obj)
15
- @req_obj = request_obj
16
- end
17
-
18
- def generate_url(options)
19
- if options.key? :src
20
- options[:transformation_position] = Default::TRANSFORMATION_POSITION
21
- end
22
- extended_options = extend_url_options(options)
23
- build_url(extended_options)
24
- end
25
-
26
- def build_url(options)
27
- # build url from all options
28
-
29
- path = options.fetch(:path, "")
30
- src = options.fetch(:src, "")
31
- url_endpoint = options.fetch(:url_endpoint, "")
32
- transformation_position = options[:transformation_position]
33
-
34
- unless Default::VALID_TRANSFORMATION_POSITION.include? transformation_position
35
- raise ArgumentError, INVALID_TRANSFORMATION_POS
36
- end
37
-
38
- src_param_used_for_url = false
39
- if (src != "") || (transformation_position == Default::QUERY_TRANSFORMATION_POSITION)
40
- src_param_used_for_url = true
41
- end
42
-
43
- if path == "" && src == ""
44
- return ""
45
- end
46
-
47
- result_url_hash = {'host': "", 'path': "", 'query': ""}
48
- existing_query=nil
49
- if path != ""
50
- parsed_url = Addressable::URI.parse(path)
51
- existing_query=parsed_url.query
52
- parsed_host = Addressable::URI.parse(url_endpoint)
53
- result_url_hash[:scheme] = parsed_host.scheme
54
-
55
- # making sure single '/' at end
56
- result_url_hash[:host] = parsed_host.host.to_s.chomp("/") + parsed_host.path.chomp("/") + "/"
57
- result_url_hash[:path] = trim_slash(parsed_url.path)
58
- else
59
- parsed_url = Addressable::URI.parse(src)
60
- existing_query=parsed_url.query
61
- host = parsed_url.host
62
- result_url_hash[:userinfo] = parsed_url.userinfo if parsed_url.userinfo
63
- result_url_hash[:host] = host
64
- result_url_hash[:scheme] = parsed_url.scheme
65
- result_url_hash[:path] = parsed_url.path
66
- src_param_used_for_url = true
67
- end
68
- query_params = {}
69
- if existing_query!=nil
70
- existing_query.split("&").each do |part|
71
- parts=part.split("=")
72
- if parts.length==2
73
- query_params[parts[0]]=parts[1]
74
- end
75
- end
76
- end
77
- options.fetch(:query_parameters, {}).each do |key, value|
78
- query_params[key]=value
79
- end
80
- transformation_str = transformation_to_str(options[:transformation]).chomp("/")
81
-
82
- unless transformation_str.nil? || transformation_str.strip.empty?
83
- if (transformation_position == Default::QUERY_TRANSFORMATION_POSITION) || src_param_used_for_url == true
84
- result_url_hash[:query] = "#{Default::TRANSFORMATION_PARAMETER}=#{transformation_str}"
85
- query_params[:tr]=transformation_str
86
- else
87
- result_url_hash[:path] = "#{Default::TRANSFORMATION_PARAMETER}:#{transformation_str}/#{result_url_hash[:path]}"
88
- end
89
-
90
- end
91
-
92
- result_url_hash[:host] = result_url_hash[:host].to_s.reverse.chomp("/").reverse
93
- result_url_hash[:path] = result_url_hash[:path].chomp("/")
94
- result_url_hash[:scheme] ||= "https"
95
-
96
- query_param_arr = []
97
- query_param_arr.push("ik-sdk-version=ruby-"+Imagekit::Sdk::VERSION)
98
- query_params.each do |key, value|
99
- if value.to_s == ""
100
- query_param_arr.push(key.to_s)
101
- else
102
- query_param_arr.push(key.to_s + "=" + value.to_s)
103
- end
104
- end
105
-
106
- query_param_str = query_param_arr.join("&")
107
- result_url_hash[:query] = query_param_str
108
-
109
- # Signature String and Timestamp
110
- # We can do this only for URLs that are created using urlEndpoint and path parameter
111
- # because we need to know the endpoint to be able to remove it from the URL to create a signature
112
- # for the remaining. With the src parameter, we would not know the "pattern" in the URL
113
- if options[:signed] && !(options[:src])
114
- intermediate_url = result_url_hash.fetch(:scheme, "") + "://" + result_url_hash.fetch(:host, "") + result_url_hash.fetch(:path, "")
115
- if result_url_hash[:query]!=nil && result_url_hash[:query]!=""
116
- intermediate_url += result_url_hash.fetch(:query, "")
117
- end
118
- end
119
-
120
- url=hash_to_url(result_url_hash)
121
- if options[:signed]
122
- private_key = options[:private_key]
123
- expire_seconds = options[:expire_seconds]
124
- expire_timestamp = get_signature_timestamp(expire_seconds)
125
- url_signature = get_signature(private_key, url, url_endpoint, expire_timestamp)
126
- query_param_arr.push(Default::SIGNATURE_PARAMETER + "=" + url_signature)
127
-
128
- if expire_timestamp && (expire_timestamp != Default::TIMESTAMP)
129
- query_param_arr.push(Default::TIMESTAMP_PARAMETER + "=" + expire_timestamp.to_s)
130
- end
131
-
132
- query_param_str = query_param_arr.join("&")
133
- result_url_hash[:query] = query_param_str
134
-
135
- url=hash_to_url(result_url_hash)
136
- end
137
- url
138
- end
139
-
140
- def transformation_to_str(transformation)
141
- # creates transformation_position string for url
142
- # from transformation dictionary
143
-
144
- unless transformation.is_a?(Array)
145
- return ""
146
- end
147
-
148
- parsed_transforms = []
149
- (0..(transformation.length - 1)).each do |i|
150
- parsed_transform_step = []
151
-
152
- transformation[i].keys.each do |key|
153
- transform_key = SUPPORTED_TRANS.fetch(key, nil)
154
- transform_key ||= key
155
-
156
- if transform_key == "oi" || transform_key == "di"
157
- transformation[i][key][0] = "" if transformation[i][key][0] == "/"
158
- transformation[i][key] = transformation[i][key].gsub("/", "@@")
159
- end
160
-
161
- if transformation[i][key] == "-"
162
- parsed_transform_step.push(transform_key)
163
- else
164
- parsed_transform_step.push("#{transform_key}#{Default::TRANSFORM_KEY_VALUE_DELIMITER}#{transformation[i][key]}")
165
- end
166
- end
167
- parsed_transforms.push(parsed_transform_step.join(Default::TRANSFORM_DELIMITER))
168
- end
169
- parsed_transforms.join(Default::CHAIN_TRANSFORM_DELIMITER)
170
- end
171
-
172
- def get_signature_timestamp(seconds)
173
- # this function returns either default time stamp
174
- # or current unix time and expiry seconds to get
175
- # signature time stamp
176
-
177
- if seconds.to_i == 0
178
- Default::DEFAULT_TIMESTAMP
179
- else
180
- DateTime.now.strftime("%s").to_i + seconds.to_i
181
- end
182
- end
183
-
184
- def get_signature(private_key, url, url_endpoint, expiry_timestamp)
185
- # creates signature(hashed hex key) and returns from
186
- # private_key, url, url_endpoint and expiry_timestamp
187
- if expiry_timestamp==0
188
- expiry_timestamp=Default::DEFAULT_TIMESTAMP
189
- end
190
- if url_endpoint[url_endpoint.length-1]!="/"
191
- url_endpoint+="/"
192
- end
193
- replaced_url=url.gsub(url_endpoint, "")
194
- replaced_url = replaced_url + expiry_timestamp.to_s
195
- OpenSSL::HMAC.hexdigest("SHA1", private_key, replaced_url)
196
- end
197
-
198
- def extend_url_options(options)
199
- attr_dict = {"public_key": @req_obj.public_key,
200
- "private_key": @req_obj.private_key,
201
- "url_endpoint": @req_obj.url_endpoint,
202
- "transformation_position": @req_obj.transformation_position, }
203
- # extending url options
204
- attr_dict.merge(options)
205
- end
206
-
207
- def hash_to_url(url_hash)
208
- generated_url = url_hash.fetch(:scheme, "") + "://" + url_hash.fetch(:host, "") + url_hash.fetch(:path, "")
209
- if url_hash[:query] != ""
210
- generated_url = generated_url + "?" + url_hash.fetch(:query, "")
211
- return generated_url
212
- end
213
- generated_url
214
- end
215
-
216
- def trim_slash(str, both = true)
217
- if str == ""
218
- return ""
219
- end
220
- # remove slash from a string
221
- # if both is not provide trims both slash
222
- # example - '/abc/' returns 'abc'
223
- # if both=false it will only trim end slash
224
- # example - '/abc/' returns '/abc'
225
- # NOTE: IT'S RECOMMENDED TO USE inbuilt .chomp('string you want to remove')
226
- # FOR REMOVING ONLY TRAILING SLASh
227
- if both
228
- str[0].chomp("/") + str[1..-2] + str[-1].chomp("/")
229
- else
230
- str.chomp("/")
231
- end
232
- end
233
-
234
- # class Imagekit
235
-
236
- # end
237
- 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