Class: Cloudinary::Utils
Constant Summary collapse
- SHARED_CDN =
Deprecated.
Use Cloudinary::SHARED_CDN
Cloudinary::SHARED_CDN
- DEFAULT_RESPONSIVE_WIDTH_TRANSFORMATION =
{:width => :auto, :crop => :limit}
- IMAGE_FORMATS =
%w(bmp png tif tiff jpg jpeg gif pdf ico eps jpc jp2 psd)- @@json_decode =
false
Class Method Summary collapse
- .api_sign_request(params_to_sign, api_secret) ⇒ Object
- .api_string_to_sign(params_to_sign) ⇒ Object
- .as_bool(value) ⇒ Object
- .as_safe_bool(value) ⇒ Object
- .asset_file_name(path) ⇒ Object
- .build_array(array) ⇒ Object
- .cloudinary_api_url(action = 'upload', options = {}) ⇒ Object
- .cloudinary_url(public_id, options = {}) ⇒ Object
- .config_option_consume(options, option_name, default_value = nil) ⇒ Object
- .encode_double_array(array) ⇒ Object
- .encode_hash(hash) ⇒ Object
- .finalize_resource_type(resource_type, type, url_suffix, use_root_path, shorten) ⇒ Object
- .finalize_source(source, format, url_suffix) ⇒ Object
-
.generate_transformation_string(options = {}) ⇒ Object
Warning: options are being destructively updated!.
- .json_decode(str) ⇒ Object
- .private_download_url(public_id, format, options = {}) ⇒ Object
- .random_public_id ⇒ Object
- .resource_type_for_format(format) ⇒ Object
- .safe_blank?(value) ⇒ Boolean
- .sign_request(params, options = {}) ⇒ Object
- .signed_download_url(public_id, options = {}) ⇒ Object
- .signed_preloaded_image(result) ⇒ Object
-
.smart_escape(string) ⇒ Object
Based on CGI::unescape.
- .supported_image_format?(format) ⇒ Boolean
-
.unsigned_download_url(source, options = {}) ⇒ Object
Warning: options are being destructively updated!.
-
.unsigned_download_url_prefix(source, cloud_name, private_cdn, cdn_subdomain, secure_cdn_subdomain, cname, secure, secure_distribution) ⇒ Object
cdn_subdomain and secure_cdn_subdomain 1) Customers in shared distribution (e.g.
- .zip_download_url(tag, options = {}) ⇒ Object
Class Method Details
.api_sign_request(params_to_sign, api_secret) ⇒ Object
144 145 146 147 |
# File 'lib/cloudinary/utils.rb', line 144 def self.api_sign_request(params_to_sign, api_secret) to_sign = api_string_to_sign(params_to_sign) Digest::SHA1.hexdigest("#{to_sign}#{api_secret}") end |
.api_string_to_sign(params_to_sign) ⇒ Object
140 141 142 |
# File 'lib/cloudinary/utils.rb', line 140 def self.api_string_to_sign(params_to_sign) params_to_sign.map{|k,v| [k.to_s, v.is_a?(Array) ? v.join(",") : v]}.reject{|k,v| v.nil? || v == ""}.sort_by(&:first).map{|k,v| "#{k}=#{v}"}.join("&") end |
.as_bool(value) ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/cloudinary/utils.rb', line 443 def self.as_bool(value) case value when nil then nil when String then value.downcase == "true" || value == "1" when TrueClass then true when FalseClass then false when Fixnum then value != 0 when Symbol then value == :true else raise "Invalid boolean value #{value} of type #{value.class}" end end |
.as_safe_bool(value) ⇒ Object
456 457 458 459 460 461 462 |
# File 'lib/cloudinary/utils.rb', line 456 def self.as_safe_bool(value) case as_bool(value) when nil then nil when TrueClass then 1 when FalseClass then 0 end end |
.asset_file_name(path) ⇒ Object
360 361 362 363 364 365 366 |
# File 'lib/cloudinary/utils.rb', line 360 def self.asset_file_name(path) data = Cloudinary.app_root.join(path).read(:mode=>"rb") ext = path.extname md5 = Digest::MD5.hexdigest(data) public_id = "#{path.basename(ext)}-#{md5}" "#{public_id}#{ext}" end |
.build_array(array) ⇒ Object
401 402 403 404 405 406 407 |
# File 'lib/cloudinary/utils.rb', line 401 def self.build_array(array) case array when Array then array when nil then [] else [array] end end |
.cloudinary_api_url(action = 'upload', options = {}) ⇒ Object
306 307 308 309 310 311 |
# File 'lib/cloudinary/utils.rb', line 306 def self.cloudinary_api_url(action = 'upload', = {}) cloudinary = [:upload_prefix] || Cloudinary.config.upload_prefix || "https://api.cloudinary.com" cloud_name = [:cloud_name] || Cloudinary.config.cloud_name || raise(CloudinaryException, "Must supply cloud_name") resource_type = [:resource_type] || "image" return [cloudinary, "v1_1", cloud_name, resource_type, action].join("/") end |
.cloudinary_url(public_id, options = {}) ⇒ Object
351 352 353 354 355 356 357 358 |
# File 'lib/cloudinary/utils.rb', line 351 def self.cloudinary_url(public_id, = {}) if [:type].to_s == 'authenticated' && ![:sign_url] result = signed_download_url(public_id, ) else result = unsigned_download_url(public_id, ) end return result end |
.config_option_consume(options, option_name, default_value = nil) ⇒ Object
438 439 440 441 |
# File 'lib/cloudinary/utils.rb', line 438 def self.config_option_consume(, option_name, default_value = nil) return .delete(option_name) if .include?(option_name) return Cloudinary.config.send(option_name) || default_value end |
.encode_double_array(array) ⇒ Object
417 418 419 420 421 422 423 424 |
# File 'lib/cloudinary/utils.rb', line 417 def self.encode_double_array(array) array = build_array(array) if array.length > 0 && array[0].is_a?(Array) return array.map{|a| build_array(a).join(",")}.join("|") else return array.join(",") end end |
.encode_hash(hash) ⇒ Object
409 410 411 412 413 414 415 |
# File 'lib/cloudinary/utils.rb', line 409 def self.encode_hash(hash) case hash when Hash then hash.map{|k,v| "#{k}=#{v}"}.join("|") when nil then "" else hash end end |
.finalize_resource_type(resource_type, type, url_suffix, use_root_path, shorten) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/cloudinary/utils.rb', line 241 def self.finalize_resource_type(resource_type, type, url_suffix, use_root_path, shorten) type ||= :upload if !url_suffix.blank? if resource_type.to_s == "image" && type.to_s == "upload" resource_type = "images" type = nil elsif resource_type.to_s == "raw" && type.to_s == "upload" resource_type = "files" type = nil else raise(CloudinaryException, "URL Suffix only supported for image/upload and raw/upload") end end if use_root_path if (resource_type.to_s == "image" && type.to_s == "upload") || (resource_type.to_s == "images" && type.blank?) resource_type = nil type = nil else raise(CloudinaryException, "Root path only supported for image/upload") end end if shorten && resource_type.to_s == "image" && type.to_s == "upload" resource_type = "iu" type = nil end [resource_type, type] end |
.finalize_source(source, format, url_suffix) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/cloudinary/utils.rb', line 221 def self.finalize_source(source, format, url_suffix) source = source.gsub(%r(([^:])//), '\1/') if source.match(%r(^https?:/)i) source = smart_escape(source) source_to_sign = source else source = smart_escape(URI.decode(source)) source_to_sign = source unless url_suffix.blank? raise(CloudinaryException, "url_suffix should not include . or /") if url_suffix.match(%r([\./])) source = "#{source}/#{url_suffix}" end if !format.blank? source = "#{source}.#{format}" source_to_sign = "#{source_to_sign}.#{format}" end end [source, source_to_sign] end |
.generate_transformation_string(options = {}) ⇒ Object
Warning: options are being destructively updated!
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cloudinary/utils.rb', line 13 def self.generate_transformation_string(={}) if .is_a?(Array) return .map{|base_transformation| generate_transformation_string(base_transformation.clone)}.join("/") end # Symbolize keys .keys.each do |key| [(key.to_sym rescue key)] = .delete(key) end responsive_width = config_option_consume(, :responsive_width) size = .delete(:size) [:width], [:height] = size.split("x") if size width = [:width] width = width.to_s if width.is_a?(Symbol) height = [:height] has_layer = [:overlay].present? || [:underlay].present? crop = .delete(:crop) angle = build_array(.delete(:angle)).join(".") no_html_sizes = has_layer || angle.present? || crop.to_s == "fit" || crop.to_s == "limit" || crop.to_s == "lfill" .delete(:width) if width && (width.to_f < 1 || no_html_sizes || width == "auto" || responsive_width) .delete(:height) if height && (height.to_f < 1 || no_html_sizes || responsive_width) width=height=nil if crop.nil? && !has_layer && width != "auto" background = .delete(:background) background = background.sub(/^#/, 'rgb:') if background color = .delete(:color) color = color.sub(/^#/, 'rgb:') if color base_transformations = build_array(.delete(:transformation)) if base_transformations.any?{|base_transformation| base_transformation.is_a?(Hash)} base_transformations = base_transformations.map do |base_transformation| base_transformation.is_a?(Hash) ? generate_transformation_string(base_transformation.clone) : generate_transformation_string(:transformation=>base_transformation) end else named_transformation = base_transformations.join(".") base_transformations = [] end effect = .delete(:effect) effect = Array(effect).flatten.join(":") if effect.is_a?(Array) || effect.is_a?(Hash) border = .delete(:border) if border.is_a?(Hash) border = "#{border[:width] || 2}px_solid_#{(border[:color] || "black").sub(/^#/, 'rgb:')}" elsif border.to_s =~ /^\d+$/ # fallback to html border attribute [:border] = border border = nil end flags = build_array(.delete(:flags)).join(".") dpr = config_option_consume(, :dpr) if .include? :offset [:start_offset], [:end_offset] = split_range .delete(:offset) end params = { :a => angle, :b => background, :bo => border, :c => crop, :co => color, :dpr => dpr, :e => effect, :fl => flags, :h => height, :t => named_transformation, :w => width } { :ac => :audio_codec, :br => :bit_rate, :cs => :color_space, :d => :default_image, :dl => :delay, :dn => :density, :du => :duration, :eo => :end_offset, :f => :fetch_format, :g => :gravity, :l => :overlay, :o => :opacity, :p => :prefix, :pg => :page, :q => :quality, :r => :radius, :af => :audio_frequency, :so => :start_offset, :u => :underlay, :vc => :video_codec, :vs => :video_sampling, :x => :x, :y => :y, :z => :zoom }.each do |param, option| params[param] = .delete(option) end params[:vc] = process_video_params params[:vc] if params[:vc].present? [:so, :eo, :du].each do |range_value| params[range_value] = norm_range_value params[range_value] if params[range_value].present? end transformation = params.reject{|_k,v| v.blank?}.map{|k,v| "#{k}_#{v}"}.sort.join(",") raw_transformation = .delete(:raw_transformation) transformation = [transformation, raw_transformation].reject(&:blank?).join(",") transformations = base_transformations << transformation if responsive_width responsive_width_transformation = Cloudinary.config.responsive_width_transformation || DEFAULT_RESPONSIVE_WIDTH_TRANSFORMATION transformations << generate_transformation_string(responsive_width_transformation.clone) end if width.to_s == "auto" || responsive_width [:responsive] = true end if dpr.to_s == "auto" [:hidpi] = true end transformations.reject(&:blank?).join("/") end |
.json_decode(str) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/cloudinary/utils.rb', line 385 def self.json_decode(str) if !@@json_decode @@json_decode = true begin require 'json' rescue LoadError begin require 'active_support/json' rescue LoadError raise LoadError, "Please add the json gem or active_support to your Gemfile" end end end defined?(JSON) ? JSON.parse(str) : ActiveSupport::JSON.decode(str) end |
.private_download_url(public_id, format, options = {}) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/cloudinary/utils.rb', line 322 def self.private_download_url(public_id, format, = {}) cloudinary_params = sign_request({ :timestamp=>Time.now.to_i, :public_id=>public_id, :format=>format, :type=>[:type], :attachment=>[:attachment], :expires_at=>[:expires_at] && [:expires_at].to_i }, ) return Cloudinary::Utils.cloudinary_api_url("download", ) + "?" + cloudinary_params.to_query end |
.random_public_id ⇒ Object
375 376 377 378 |
# File 'lib/cloudinary/utils.rb', line 375 def self.random_public_id sr = defined?(ActiveSupport::SecureRandom) ? ActiveSupport::SecureRandom : SecureRandom sr.base64(20).downcase.gsub(/[^a-z0-9]/, "").sub(/^[0-9]+/, '')[0,20] end |
.resource_type_for_format(format) ⇒ Object
434 435 436 |
# File 'lib/cloudinary/utils.rb', line 434 def self.resource_type_for_format(format) self.supported_image_format?(format) ? 'image' : 'raw' end |
.safe_blank?(value) ⇒ Boolean
464 465 466 |
# File 'lib/cloudinary/utils.rb', line 464 def self.safe_blank?(value) value.nil? || value == "" || value == [] end |
.sign_request(params, options = {}) ⇒ Object
313 314 315 316 317 318 319 320 |
# File 'lib/cloudinary/utils.rb', line 313 def self.sign_request(params, ={}) 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") params = params.reject{|k, v| self.safe_blank?(v)} params[:signature] = Cloudinary::Utils.api_sign_request(params, api_secret) params[:api_key] = api_key params end |
.signed_download_url(public_id, options = {}) ⇒ Object
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/cloudinary/utils.rb', line 340 def self.signed_download_url(public_id, = {}) aws_private_key_path = [:aws_private_key_path] || Cloudinary.config.aws_private_key_path || raise(CloudinaryException, "Must supply aws_private_key_path") aws_key_pair_id = [:aws_key_pair_id] || Cloudinary.config.aws_key_pair_id || raise(CloudinaryException, "Must supply aws_key_pair_id") authenticated_distribution = [:authenticated_distribution] || Cloudinary.config.authenticated_distribution || raise(CloudinaryException, "Must supply authenticated_distribution") @signers ||= Hash.new{|h,k| path, id = k; h[k] = AwsCfSigner.new(path, id)} signer = @signers[[aws_private_key_path, aws_key_pair_id]] url = Cloudinary::Utils.unsigned_download_url(public_id, {:type=>:authenticated}.merge().merge(:secure=>true, :secure_distribution=>authenticated_distribution, :private_cdn=>true)) expires_at = [:expires_at] || (Time.now+3600) signer.sign(url, :ending => expires_at) end |
.signed_preloaded_image(result) ⇒ Object
380 381 382 |
# File 'lib/cloudinary/utils.rb', line 380 def self.signed_preloaded_image(result) "#{result["resource_type"]}/#{result["type"] || "upload"}/v#{result["version"]}/#{[result["public_id"], result["format"]].reject(&:blank?).join(".")}##{result["signature"]}" end |
.smart_escape(string) ⇒ Object
Based on CGI::unescape. In addition does not escape / :
369 370 371 372 373 |
# File 'lib/cloudinary/utils.rb', line 369 def self.smart_escape(string) string.gsub(/([^a-zA-Z0-9_.\-\/:]+)/) do '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase end end |
.supported_image_format?(format) ⇒ Boolean
428 429 430 431 432 |
# File 'lib/cloudinary/utils.rb', line 428 def self.supported_image_format?(format) format = format.to_s.downcase extension = format =~ /\./ ? format.split('.').last : format IMAGE_FORMATS.include?(extension) end |
.unsigned_download_url(source, options = {}) ⇒ Object
Warning: options are being destructively updated!
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/cloudinary/utils.rb', line 150 def self.unsigned_download_url(source, = {}) type = .delete(:type) [:fetch_format] ||= .delete(:format) if type == :fetch transformation = self.generate_transformation_string() resource_type = .delete(:resource_type) || "image" version = .delete(:version) format = .delete(:format) cloud_name = config_option_consume(, :cloud_name) || raise(CloudinaryException, "Must supply cloud_name in tag or in configuration") secure = .delete(:secure) ssl_detected = .delete(:ssl_detected) secure = ssl_detected || Cloudinary.config.secure if secure.nil? private_cdn = config_option_consume(, :private_cdn) secure_distribution = config_option_consume(, :secure_distribution) cname = config_option_consume(, :cname) shorten = config_option_consume(, :shorten) force_remote = .delete(:force_remote) cdn_subdomain = config_option_consume(, :cdn_subdomain) secure_cdn_subdomain = config_option_consume(, :secure_cdn_subdomain) sign_url = config_option_consume(, :sign_url) secret = config_option_consume(, :api_secret) sign_version = config_option_consume(, :sign_version) # Deprecated behavior url_suffix = .delete(:url_suffix) use_root_path = config_option_consume(, :use_root_path) raise(CloudinaryException, "URL Suffix only supported in private CDN") if url_suffix.present? and not private_cdn original_source = source return original_source if source.blank? if defined?(CarrierWave::Uploader::Base) && source.is_a?(CarrierWave::Uploader::Base) source = format.blank? ? source.filename : source.full_public_id end source = source.to_s if !force_remote return original_source if (type.nil? || type == :asset) && source.match(%r(^https?:/)i) if source.start_with?("/") if source.start_with?("/images/") source = source.sub(%r(/images/), '') else return original_source end end ||= defined?(Cloudinary::Static) ? Cloudinary::Static. : {} if type == :asset && ["images/#{source}"] return original_source if !Cloudinary.config.static_image_support source = ["images/#{source}"]["public_id"] source += File.extname(original_source) if !format elsif type == :asset return original_source # requested asset, but no metadata - probably local file. return. end end resource_type, type = finalize_resource_type(resource_type, type, url_suffix, use_root_path, shorten) source, source_to_sign = finalize_source(source, format, url_suffix) version ||= 1 if source_to_sign.include?("/") and !source_to_sign.match(/^v[0-9]+/) and !source_to_sign.match(/^https?:\//) version &&= "v#{version}" transformation = transformation.gsub(%r(([^:])//), '\1/') if sign_url to_sign = [transformation, sign_version && version, source_to_sign].reject(&:blank?).join("/") signature = 's--' + Base64.urlsafe_encode64(Digest::SHA1.digest(to_sign + secret))[0,8] + '--' end prefix = unsigned_download_url_prefix(source, cloud_name, private_cdn, cdn_subdomain, secure_cdn_subdomain, cname, secure, secure_distribution) source = [prefix, resource_type, type, signature, transformation, version, source].reject(&:blank?).join("/") end |
.unsigned_download_url_prefix(source, cloud_name, private_cdn, cdn_subdomain, secure_cdn_subdomain, cname, secure, secure_distribution) ⇒ Object
cdn_subdomain and secure_cdn_subdomain 1) Customers in shared distribution (e.g. res.cloudinary.com)
if cdn_domain is true uses res-[1-5].cloudinary.com for both http and https. Setting secure_cdn_subdomain to false disables this for https.
2) Customers with private cdn
if cdn_domain is true uses cloudname-res-[1-5].cloudinary.com for http
if secure_cdn_domain is true uses cloudname-res-[1-5].cloudinary.com for https (please contact support if you require this)
3) Customers with cname
if cdn_domain is true uses a[1-5].cname for http. For https, uses the same naming scheme as 1 for shared distribution and as 2 for private distribution.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/cloudinary/utils.rb', line 277 def self.unsigned_download_url_prefix(source, cloud_name, private_cdn, cdn_subdomain, secure_cdn_subdomain, cname, secure, secure_distribution) return "/res#{cloud_name}" if cloud_name.start_with?("/") # For development shared_domain = !private_cdn if secure if secure_distribution.nil? || secure_distribution == Cloudinary::OLD_AKAMAI_SHARED_CDN secure_distribution = private_cdn ? "#{cloud_name}-res.cloudinary.com" : Cloudinary::SHARED_CDN end shared_domain ||= secure_distribution == Cloudinary::SHARED_CDN secure_cdn_subdomain = cdn_subdomain if secure_cdn_subdomain.nil? && shared_domain if secure_cdn_subdomain secure_distribution = secure_distribution.gsub('res.cloudinary.com', "res-#{(Zlib::crc32(source) % 5) + 1}.cloudinary.com") end prefix = "https://#{secure_distribution}" elsif cname subdomain = cdn_subdomain ? "a#{(Zlib::crc32(source) % 5) + 1}." : "" prefix = "http://#{subdomain}#{cname}" else host = [private_cdn ? "#{cloud_name}-" : "", "res", cdn_subdomain ? "-#{(Zlib::crc32(source) % 5) + 1}" : "", ".cloudinary.com"].join prefix = "http://#{host}" end prefix += "/#{cloud_name}" if shared_domain prefix end |
.zip_download_url(tag, options = {}) ⇒ Object
335 336 337 338 |
# File 'lib/cloudinary/utils.rb', line 335 def self.zip_download_url(tag, = {}) cloudinary_params = sign_request({:timestamp=>Time.now.to_i, :tag=>tag, :transformation=>generate_transformation_string()}, ) return Cloudinary::Utils.cloudinary_api_url("download_tag.zip", ) + "?" + cloudinary_params.to_query end |