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]
Class Method Summary collapse
-
.add_tag(tag, public_ids = [], options = {}) ⇒ Object
options may include ‘exclusive’ (boolean) which causes clearing this tag from all other resources.
- .build_eager(eager) ⇒ Object
- .build_upload_params(options) ⇒ Object
- .destroy(public_id, options = {}) ⇒ Object
- .exists?(public_id, options = {}) ⇒ Boolean
- .explicit(public_id, options = {}) ⇒ Object
- .explode(public_id, options = {}) ⇒ Object
- .generate_sprite(tag, options = {}) ⇒ Object
- .multi(tag, 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
- .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_tag(tag, public_ids = [], options = {}) ⇒ Object
options may include ‘exclusive’ (boolean) which causes clearing this tag from all other resources
235 236 237 238 239 |
# File 'lib/cloudinary/uploader.rb', line 235 def self.add_tag(tag, public_ids = [], = {}) exclusive = .delete(:exclusive) command = exclusive ? "set_exclusive" : "add" return self.(tag, command, public_ids, ) end |
.build_eager(eager) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/cloudinary/uploader.rb', line 7 def self.build_eager(eager) return nil if eager.nil? Cloudinary::Utils.build_array(eager).map do |transformation, format| transformation = transformation.clone format = transformation.delete(:format) || format [Cloudinary::Utils.generate_transformation_string(transformation), format].compact.join("/") end.join("|") end |
.build_upload_params(options) ⇒ Object
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 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cloudinary/uploader.rb', line 17 def self.build_upload_params() #symbolize keys = .clone .keys.each{|key| [key.to_sym] = .delete(key) if key.is_a?(String)} params = {:timestamp=>([:timestamp] || Time.now.to_i), :transformation => Cloudinary::Utils.generate_transformation_string(.clone), :public_id=>[:public_id], :callback=>[:callback], :format=>[:format], :type=>[:type], :backup=>Cloudinary::Utils.as_safe_bool([:backup]), :faces=>Cloudinary::Utils.as_safe_bool([:faces]), :exif=>Cloudinary::Utils.as_safe_bool([:exif]), :colors=>Cloudinary::Utils.as_safe_bool([:colors]), :image_metadata=>Cloudinary::Utils.as_safe_bool([:image_metadata]), :invalidate=>Cloudinary::Utils.as_safe_bool([:invalidate]), :eager=>build_eager([:eager]), :headers=>build_custom_headers([:headers]), :use_filename=>Cloudinary::Utils.as_safe_bool([:use_filename]), :unique_filename=>Cloudinary::Utils.as_safe_bool([:unique_filename]), :overwrite=>Cloudinary::Utils.as_safe_bool([:overwrite]), :discard_original_filename=>Cloudinary::Utils.as_safe_bool([:discard_original_filename]), :notification_url=>[:notification_url], :eager_notification_url=>[:eager_notification_url], :eager_async=>Cloudinary::Utils.as_safe_bool([:eager_async]), :proxy=>[:proxy], :folder=>[:folder], :allowed_formats => Cloudinary::Utils.build_array([:allowed_formats]).join(","), :tags=>[:tags] && Cloudinary::Utils.build_array([:tags]).join(","), :context => Cloudinary::Utils.encode_hash([:context]), :face_coordinates => Cloudinary::Utils.encode_double_array([:face_coordinates]), :custom_coordinates => Cloudinary::Utils.encode_double_array([:custom_coordinates]), :moderation => [:moderation], :raw_convert => [:raw_convert], :ocr => [:ocr], :categorization => [:categorization], :detection => [:detection], :similarity_search => [:similarity_search], :background_removal => [:background_removal], :auto_tagging => [:auto_tagging] && [:auto_tagging].to_f, :upload_preset => [:upload_preset], :phash => Cloudinary::Utils.as_safe_bool([:phash]), :return_delete_token => Cloudinary::Utils.as_safe_bool([:return_delete_token]), } params end |
.destroy(public_id, options = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cloudinary/uploader.rb', line 127 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 RestClient::Request.execute(:method => :head, :url => cloudinary_url, :timeout=>5).code.to_s =~ /2\d{2}/ rescue RestClient::ResourceNotFound => e return false end end |
.explicit(public_id, options = {}) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/cloudinary/uploader.rb', line 160 def self.explicit(public_id, ={}) call_api("explicit", ) do { :timestamp=>([:timestamp] || Time.now.to_i), :type=>[:type], :public_id=> public_id, :callback=> [:callback], :eager=>build_eager([:eager]), :eager_notification_url=>[:eager_notification_url], :eager_async=>Cloudinary::Utils.as_safe_bool([:eager_async]), :headers=>build_custom_headers([:headers]), :tags=>[:tags] && Cloudinary::Utils.build_array([:tags]).join(","), :face_coordinates => [:face_coordinates] && Cloudinary::Utils.encode_double_array([:face_coordinates]) } end end |
.explode(public_id, options = {}) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/cloudinary/uploader.rb', line 221 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 = {}) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/cloudinary/uploader.rb', line 186 def self.generate_sprite(tag, ={}) version_store = .delete(:version_store) result = call_api("sprite", ) do { :timestamp=>([:timestamp] || Time.now.to_i), :tag=>tag, :async=>[:async], :notification_url=>[:notification_url], :transformation => Cloudinary::Utils.generate_transformation_string(.merge(:fetch_format=>[:format])) } 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 = {}) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/cloudinary/uploader.rb', line 208 def self.multi(tag, ={}) call_api("multi", ) do { :timestamp=>([:timestamp] || Time.now.to_i), :tag=>tag, :format=>[:format], :async=>[:async], :notification_url=>[:notification_url], :transformation => Cloudinary::Utils.generate_transformation_string(.clone) } end end |
.remove_tag(tag, public_ids = [], options = {}) ⇒ Object
241 242 243 |
# File 'lib/cloudinary/uploader.rb', line 241 def self.remove_tag(tag, public_ids = [], = {}) return self.(tag, "remove", public_ids, ) end |
.rename(from_public_id, to_public_id, options = {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cloudinary/uploader.rb', line 138 def self.rename(from_public_id, to_public_id, ={}) call_api("rename", ) do { :timestamp=>([:timestamp] || Time.now.to_i), :type=>[:type], :overwrite=>[:overwrite], :from_public_id=>from_public_id, :to_public_id=>to_public_id, } end end |
.replace_tag(tag, public_ids = [], options = {}) ⇒ Object
245 246 247 |
# File 'lib/cloudinary/uploader.rb', line 245 def self.replace_tag(tag, public_ids = [], = {}) return self.(tag, "replace", public_ids, ) end |
.text(text, options = {}) ⇒ Object
178 179 180 181 182 183 184 |
# File 'lib/cloudinary/uploader.rb', line 178 def self.text(text, ={}) call_api("text", ) do params = {:timestamp => Time.now.to_i, :text=>text} TEXT_PARAMS.each{|k| params[k] = [k] if ![k].nil?} params end end |
.unsigned_upload(file, upload_preset, options = {}) ⇒ Object
65 66 67 |
# File 'lib/cloudinary/uploader.rb', line 65 def self.unsigned_upload(file, upload_preset, ={}) upload(file, .merge(:unsigned => true, :upload_preset => upload_preset)) end |
.upload(file, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cloudinary/uploader.rb', line 69 def self.upload(file, ={}) call_api("upload", ) do params = build_upload_params() if file.is_a?(Pathname) params[:file] = File.open(file, "rb") elsif file.respond_to?(:read) || file =~ /^ftp:|^https?:|^s3:|^data:[^;]*;base64,([a-zA-Z0-9\/+\n=]+)$/ params[:file] = file else params[:file] = File.open(file, "rb") end [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.
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 84 def self.upload_large(file, ={}, ={}) if .is_a?(Hash) = public_id = [:public_id] else public_id = = end if file.is_a?(Pathname) || !file.respond_to?(:read) filename = file file = File.open(file, "rb") else filename = "cloudinaryfile" end upload = upload_id = nil index = 0 chunk_size = [:chunk_size] || 20_000_000 while !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(:public_id=>public_id, :content_range=>range)) public_id = upload["public_id"] 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 122 123 124 125 |
# File 'lib/cloudinary/uploader.rb', line 114 def self.upload_large_part(file, ={}) [:resource_type] ||= :raw call_api("upload_chunked", ) do params = build_upload_params() if file.is_a?(Pathname) || !file.respond_to?(:read) params[:file] = File.open(file, "rb") else params[:file] = file end [params, [:file]] end end |