Class: Cloudinary::Uploader
Constant Summary collapse
- TEXT_PARAMS =
[:public_id, :font_family, :font_size, :font_color, :text_align, :font_weight, :font_style, :background, :opacity, :text_decoration, :line_spacing]
- SLIDESHOW_PARAMS =
[:notification_url, :public_id, :upload_preset]
Class Method Summary collapse
- .add_context(context, public_ids = [], options = {}) ⇒ Object
-
.add_tag(tag, public_ids = [], options = {}) ⇒ Object
options may include ‘exclusive’ (boolean) which causes clearing this tag from all other resources.
- .build_custom_headers(headers) ⇒ Object
- .build_upload_params(options, as_bool = false) ⇒ Object
- .cache_results(result) ⇒ Object
- .call_api(action, options) ⇒ Object
- .call_context_api(context, command, public_ids = [], options = {}) ⇒ Object
- .call_tags_api(tag, command, public_ids = [], options = {}) ⇒ Object
-
.create_archive(options = {}, target_format = nil) ⇒ Object
Creates a new archive in the server and returns information in JSON format.
-
.create_slideshow(options = {}) ⇒ Hash
Creates auto-generated video slideshow.
-
.create_zip(options = {}) ⇒ Object
Creates a new zip archive in the server and returns information in JSON format.
- .destroy(public_id, options = {}) ⇒ Object
- .exists?(public_id, options = {}) ⇒ Boolean
- .explicit(public_id, options = {}) ⇒ Object
- .explode(public_id, options = {}) ⇒ Object
-
.generate_sprite(tag, options = {}) ⇒ Hash
Generates sprites by merging multiple images into a single large image.
-
.multi(tag, options = {}) ⇒ Hash
Creates either a single animated image, video or a PDF.
- .remove_all_context(public_ids = [], options = {}) ⇒ Object
- .remove_all_tags(public_ids = [], options = {}) ⇒ Object
- .remove_tag(tag, public_ids = [], options = {}) ⇒ Object
- .rename(from_public_id, to_public_id, options = {}) ⇒ Object
- .replace_tag(tag, public_ids = [], options = {}) ⇒ Object
- .text(text, options = {}) ⇒ Object
- .unsigned_upload(file, upload_preset, options = {}) ⇒ Object
-
.update_metadata(metadata, public_ids, options = {}) ⇒ Object
Populates metadata fields with the given values.
- .upload(file, options = {}) ⇒ Object
-
.upload_large(file, public_id_or_options = {}, old_options = {}) ⇒ Object
Upload large files.
-
.upload_large_part(file, options = {}) ⇒ Object
Upload large files.
Class Method Details
.add_context(context, public_ids = [], options = {}) ⇒ Object
321 322 323 |
# File 'lib/cloudinary/uploader.rb', line 321 def self.add_context(context, public_ids = [], = {}) return self.call_context_api(context, "add", public_ids, ) end |
.add_tag(tag, public_ids = [], options = {}) ⇒ Object
options may include ‘exclusive’ (boolean) which causes clearing this tag from all other resources
265 266 267 268 269 |
# File 'lib/cloudinary/uploader.rb', line 265 def self.add_tag(tag, public_ids = [], = {}) exclusive = .delete(:exclusive) command = exclusive ? "set_exclusive" : "add" return self.(tag, command, public_ids, ) end |
.build_custom_headers(headers) ⇒ Object
406 407 408 |
# File 'lib/cloudinary/uploader.rb', line 406 def self.build_custom_headers(headers) Array(headers).map { |*a| a.join(": ") }.join("\n") end |
.build_upload_params(options, as_bool = false) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cloudinary/uploader.rb', line 8 def self.build_upload_params(, as_bool = false) #symbolize keys = .clone .keys.each { |key| [key.to_sym] = .delete(key) if key.is_a?(String) } params = { :access_control => Cloudinary::Utils.json_array_param([:access_control]), :allowed_formats => Cloudinary::Utils.build_array([:allowed_formats]).join(","), :auto_tagging => [:auto_tagging] && [:auto_tagging].to_f, :context => Cloudinary::Utils.encode_context([:context]), :custom_coordinates => Cloudinary::Utils.encode_double_array([:custom_coordinates]), :eager => Cloudinary::Utils.build_eager([:eager]), :face_coordinates => Cloudinary::Utils.encode_double_array([:face_coordinates]), :headers => build_custom_headers([:headers]), :responsive_breakpoints => Cloudinary::Utils.generate_responsive_breakpoints_string([:responsive_breakpoints]), :tags => [:tags] && Cloudinary::Utils.build_array([:tags]).join(","), :timestamp => ([:timestamp] || Time.now.to_i), :transformation => Cloudinary::Utils.generate_transformation_string(.clone), :metadata => Cloudinary::Utils.encode_context([:metadata]) } bool_params = [ :async, :backup, :cinemagraph_analysis, :colors, :discard_original_filename, :eager_async, :exif, :faces, :image_metadata, :media_metadata, :invalidate, :overwrite, :phash, :quality_analysis, :return_delete_token, :visual_search, :unique_filename, :use_filename, :use_filename_as_display_name, :use_asset_folder_as_public_id_prefix, :unique_display_name, :accessibility_analysis ] string_params = [ :access_mode, :asset_folder, :background_removal, :callback, :categorization, :detection, :display_name, :eager_notification_url, :eval, :on_success, :folder, :format, :filename_override, :moderation, :notification_url, :ocr, :proxy, :public_id, :public_id_prefix, :quality_override, :raw_convert, :similarity_search, :type, :upload_preset ] bool_params.each do |b| params[b] = as_bool ? Cloudinary::Utils.as_bool([b]): Cloudinary::Utils.as_safe_bool([b]) end string_params.each do |s| params[s] = [s] end params end |
.cache_results(result) ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/cloudinary/uploader.rb', line 410 def self.cache_results(result) if result["responsive_breakpoints"] result["responsive_breakpoints"].each do |bp| Cloudinary::Cache.set( result["public_id"], {type: result["type"], resource_type: result["resource_type"], raw_transformation: bp["transformation"]}, bp["breakpoints"].map{|o| o['width']} ) end end end |
.call_api(action, options) ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'lib/cloudinary/uploader.rb', line 343 def self.call_api(action, ) = .clone return_error = .delete(:return_error) use_cache = [:use_cache] || Cloudinary.config.use_cache params, non_signable = yield non_signable ||= [] headers = { "User-Agent" => Cloudinary::USER_AGENT } headers['Content-Range'] = [:content_range] if [:content_range] headers['X-Unique-Upload-Id'] = [:unique_upload_id] if [:unique_upload_id] headers.merge!([:extra_headers]) if [:extra_headers] oauth_token = [:oauth_token] || Cloudinary.config.oauth_token if oauth_token headers["Authorization"] = "Bearer #{oauth_token}" elsif ![:unsigned] api_key = [:api_key] || Cloudinary.config.api_key || raise(CloudinaryException, "Must supply api_key") api_secret = [:api_secret] || Cloudinary.config.api_secret || raise(CloudinaryException, "Must supply api_secret") signature_algorithm = [:signature_algorithm] signature_version = [:signature_version] params[:signature] = Cloudinary::Utils.api_sign_request(params.reject { |k, v| non_signable.include?(k) }, api_secret, signature_algorithm, signature_version) params[:api_key] = api_key end api_url = Cloudinary::Utils.cloudinary_api_url(action, ) api_proxy = [:api_proxy] || Cloudinary.config.api_proxy timeout = .fetch(:timeout) { Cloudinary.config.to_h.fetch(:timeout, 60) } conn = Faraday.new(url: api_url) do |faraday| faraday.proxy = api_proxy if api_proxy faraday.request :multipart, ** faraday.request :url_encoded faraday.adapter @adapter || Faraday.default_adapter end response = conn.send(:post) do |req| req.headers = headers req.body = params.reject { |_, v| v.nil? || v=="" } req..timeout = timeout if timeout end raise CloudinaryException, "Server returned unexpected status code - #{response.status} - #{response.body}" unless [200, 400, 401, 403, 404, 500].include?(response.status) begin result = Cloudinary::Utils.json_decode(response.body) rescue => e # Error is parsing json raise CloudinaryException, "Error parsing server response (#{response.status}) - #{response.body}. Got - #{e}" end if result["error"] if return_error result["error"]["http_code"] = response.status else raise CloudinaryException, result["error"]["message"] end end if use_cache && !result.nil? cache_results(result) end result end |
.call_context_api(context, command, public_ids = [], options = {}) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/cloudinary/uploader.rb', line 331 def self.call_context_api(context, command, public_ids = [], = {}) return call_api("context", ) do { :timestamp => ([:timestamp] || Time.now.to_i), :context => Cloudinary::Utils.encode_context(context), :public_ids => Cloudinary::Utils.build_array(public_ids), :command => command, :type => [:type] } end end |
.call_tags_api(tag, command, public_ids = [], options = {}) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/cloudinary/uploader.rb', line 309 def self.(tag, command, public_ids = [], = {}) return call_api("tags", ) do { :timestamp => ([:timestamp] || Time.now.to_i), :tag => tag, :public_ids => Cloudinary::Utils.build_array(public_ids), :command => command, :type => [:type] } end end |
.create_archive(options = {}, target_format = nil) ⇒ Object
Creates a new archive in the server and returns information in JSON format
169 170 171 172 173 174 175 |
# File 'lib/cloudinary/uploader.rb', line 169 def self.create_archive(={}, target_format = nil) call_api("generate_archive", ) do params = Cloudinary::Utils.archive_params() params[:target_format] = target_format if target_format params end end |
.create_slideshow(options = {}) ⇒ Hash
Creates auto-generated video slideshow.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/cloudinary/uploader.rb', line 199 def self.( = {}) [:resource_type] ||= :video call_api("create_slideshow", ) do params = { :timestamp => Time.now.to_i, :transformation => Cloudinary::Utils.build_eager([:transformation]), :manifest_transformation => Cloudinary::Utils.build_eager([:manifest_transformation]), :manifest_json => [:manifest_json] && [:manifest_json].to_json, :tags => [:tags] && Cloudinary::Utils.build_array([:tags]).join(","), :overwrite => Cloudinary::Utils.as_safe_bool([:overwrite]) } SLIDESHOW_PARAMS.each { |k| params[k] = [k] unless [k].nil? } params end end |
.create_zip(options = {}) ⇒ Object
Creates a new zip archive in the server and returns information in JSON format
178 179 180 |
# File 'lib/cloudinary/uploader.rb', line 178 def self.create_zip(={}) create_archive(, "zip") end |
.destroy(public_id, options = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/cloudinary/uploader.rb', line 123 def self.destroy(public_id, ={}) call_api("destroy", ) do { :timestamp => ([:timestamp] || Time.now.to_i), :type => [:type], :public_id => public_id, :invalidate => [:invalidate], } end end |
.exists?(public_id, options = {}) ⇒ Boolean
150 151 152 153 154 155 156 157 158 |
# File 'lib/cloudinary/uploader.rb', line 150 def self.exists?(public_id, ={}) cloudinary_url = Cloudinary::Utils.cloudinary_url(public_id, ) begin code = Faraday.new(request: { timeout: 5 }).head(cloudinary_url).status code >= 200 && code < 300 rescue Faraday::ResourceNotFound return false end end |
.explicit(public_id, options = {}) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/cloudinary/uploader.rb', line 160 def self.explicit(public_id, ={}) call_api("explicit", ) do params = build_upload_params() params[:public_id] = public_id params end end |
.explode(public_id, options = {}) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/cloudinary/uploader.rb', line 251 def self.explode(public_id, ={}) call_api("explode", ) do { :timestamp => ([:timestamp] || Time.now.to_i), :public_id => public_id, :type => [:type], :format => [:format], :notification_url => [:notification_url], :transformation => Cloudinary::Utils.generate_transformation_string(.clone) } end end |
.generate_sprite(tag, options = {}) ⇒ Hash
Generates sprites by merging multiple images into a single large image.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/cloudinary/uploader.rb', line 223 def self.generate_sprite(tag, = {}) version_store = .delete(:version_store) result = call_api("sprite", ) do Cloudinary::Utils.build_multi_and_sprite_params(tag, ) end if version_store == :file && result && result["version"] if defined?(Rails) && defined?(Rails.root) FileUtils.mkdir_p("#{Rails.root}/tmp/cloudinary") File.open("#{Rails.root}/tmp/cloudinary/cloudinary_sprite_#{tag}.version", "w") { |file| file.print result["version"].to_s } end end return result end |
.multi(tag, options = {}) ⇒ Hash
Creates either a single animated image, video or a PDF.
245 246 247 248 249 |
# File 'lib/cloudinary/uploader.rb', line 245 def self.multi(tag, = {}) call_api("multi", ) do Cloudinary::Utils.build_multi_and_sprite_params(tag, ) end end |
.remove_all_context(public_ids = [], options = {}) ⇒ Object
325 326 327 |
# File 'lib/cloudinary/uploader.rb', line 325 def self.remove_all_context(public_ids = [], = {}) return self.call_context_api(nil, "remove_all", public_ids, ) end |
.remove_all_tags(public_ids = [], options = {}) ⇒ Object
279 280 281 |
# File 'lib/cloudinary/uploader.rb', line 279 def self.(public_ids = [], = {}) return self.(nil, "remove_all", public_ids, ) end |
.remove_tag(tag, public_ids = [], options = {}) ⇒ Object
271 272 273 |
# File 'lib/cloudinary/uploader.rb', line 271 def self.remove_tag(tag, public_ids = [], = {}) return self.(tag, "remove", public_ids, ) end |
.rename(from_public_id, to_public_id, options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cloudinary/uploader.rb', line 134 def self.rename(from_public_id, to_public_id, ={}) call_api("rename", ) do { :timestamp => ([:timestamp] || Time.now.to_i), :type => [:type], :overwrite => Cloudinary::Utils.as_safe_bool([:overwrite]), :from_public_id => from_public_id, :to_public_id => to_public_id, :to_type => [:to_type], :invalidate => Cloudinary::Utils.as_safe_bool([:invalidate]), :context => [:context], :metadata => [:metadata] } end end |
.replace_tag(tag, public_ids = [], options = {}) ⇒ Object
275 276 277 |
# File 'lib/cloudinary/uploader.rb', line 275 def self.replace_tag(tag, public_ids = [], = {}) return self.(tag, "replace", public_ids, ) end |
.text(text, options = {}) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/cloudinary/uploader.rb', line 184 def self.text(text, ={}) call_api("text", ) do params = { :timestamp => Time.now.to_i, :text => text } TEXT_PARAMS.each { |k| params[k] = [k] unless [k].nil? } params end end |
.unsigned_upload(file, upload_preset, options = {}) ⇒ Object
54 55 56 |
# File 'lib/cloudinary/uploader.rb', line 54 def self.unsigned_upload(file, upload_preset, ={}) upload(file, .merge(:unsigned => true, :upload_preset => upload_preset)) end |
.update_metadata(metadata, public_ids, options = {}) ⇒ Object
Populates metadata fields with the given values. Existing values will be overwritten.
Any metadata-value pairs given are merged with any existing metadata-value pairs (an empty value for an existing metadata field clears the value).
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/cloudinary/uploader.rb', line 295 def self.(, public_ids, = {}) self.call_api("metadata", ) do { timestamp: ([:timestamp] || Time.now.to_i), type: [:type], public_ids: Cloudinary::Utils.build_array(public_ids), metadata: Cloudinary::Utils.encode_context(), clear_invalid: Cloudinary::Utils.as_safe_bool([:clear_invalid]) } end end |
.upload(file, options = {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/cloudinary/uploader.rb', line 58 def self.upload(file, ={}) call_api("upload", ) do params = build_upload_params() params[:file] = Cloudinary::Utils.handle_file_param(file, ) [params, [:file]] end end |
.upload_large(file, public_id_or_options = {}, old_options = {}) ⇒ Object
Upload large files. Note that public_id should include an extension for best results.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/cloudinary/uploader.rb', line 67 def self.upload_large(file, ={}, ={}) if .is_a?(Hash) = public_id = [:public_id] else public_id = = end .merge(:public_id => public_id) if Cloudinary::Utils.is_remote?(file) return upload(file, ) end if file.is_a?(Pathname) || !file.respond_to?(:read) filename = file file = File.open(file, "rb") else filename = "cloudinaryfile" end chunk_size = [:chunk_size] || 20_000_000 if file.size < chunk_size return upload(file, ) end filename = [:filename] if [:filename] unique_upload_id = Cloudinary::Utils.random_public_id upload = nil index = 0 until file.eof? buffer = file.read(chunk_size) current_loc = index*chunk_size range = "bytes #{current_loc}-#{current_loc+buffer.size - 1}/#{file.size}" upload = upload_large_part(Cloudinary::Blob.new(buffer, :original_filename => filename), .merge(:unique_upload_id => unique_upload_id, :content_range => range)) index += 1 end upload end |
.upload_large_part(file, options = {}) ⇒ Object
Upload large files. Note that public_id should include an extension for best results.
114 115 116 117 118 119 120 121 |
# File 'lib/cloudinary/uploader.rb', line 114 def self.upload_large_part(file, ={}) [:resource_type] ||= :raw call_api("upload", ) do params = build_upload_params() params[:file] = Cloudinary::Utils.handle_file_param(file, ) [params, [:file]] end end |