lib/cloudinary/uploader.rb in cloudinary-1.9.1 vs lib/cloudinary/uploader.rb in cloudinary-1.10.0
- old
+ new
@@ -1,8 +1,9 @@
# Copyright Cloudinary
require 'rest_client'
require 'json'
+require 'cloudinary/cache'
class Cloudinary::Uploader
REMOTE_URL_REGEX = %r(^ftp:|^https?:|^s3:|^data:[^;]*;base64,([a-zA-Z0-9\/+\n=]+)$)
@@ -98,19 +99,19 @@
filename = file
file = File.open(file, "rb")
else
filename = "cloudinaryfile"
end
+ unique_upload_id = Cloudinary::Utils.random_public_id
upload = nil
index = 0
chunk_size = options[:chunk_size] || 20_000_000
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), options.merge(:public_id => public_id, :content_range => range))
- public_id = upload["public_id"]
+ upload = upload_large_part(Cloudinary::Blob.new(buffer, :original_filename => filename), options.merge(:public_id => public_id, :unique_upload_id => unique_upload_id, :content_range => range))
index += 1
end
upload
end
@@ -290,21 +291,22 @@
def self.call_context_api(context, command, public_ids = [], options = {})
return call_api("context", options) do
{
:timestamp => (options[:timestamp] || Time.now.to_i),
- :context => Cloudinary::Utils.encode_hash(context),
+ :context => Cloudinary::Utils.encode_context(context),
:public_ids => Cloudinary::Utils.build_array(public_ids),
:command => command,
:type => options[:type]
}
end
end
def self.call_api(action, options)
options = options.clone
return_error = options.delete(:return_error)
+ use_cache = options[:use_cache] || Cloudinary.config.use_cache
params, non_signable = yield
non_signable ||= []
unless options[:unsigned]
@@ -318,10 +320,11 @@
result = nil
api_url = Cloudinary::Utils.cloudinary_api_url(action, options)
headers = { "User-Agent" => Cloudinary::USER_AGENT }
headers['Content-Range'] = options[:content_range] if options[:content_range]
+ headers['X-Unique-Upload-Id'] = options[:unique_upload_id] if options[:unique_upload_id]
headers.merge!(options[:extra_headers]) if options[:extra_headers]
RestClient::Request.execute(:method => :post, :url => api_url, :payload => params.reject { |k, v| v.nil? || v=="" }, :timeout => timeout, :headers => headers) do
|response, request, tmpresult|
raise CloudinaryException, "Server returned unexpected status code - #{response.code} - #{response.body}" unless [200, 400, 401, 403, 404, 500].include?(response.code)
begin
@@ -336,13 +339,28 @@
else
raise CloudinaryException, result["error"]["message"]
end
end
end
-
+ if use_cache && !result.nil?
+ cache_results(result)
+ end
result
end
def self.build_custom_headers(headers)
Array(headers).map { |*a| a.join(": ") }.join("\n")
end
+
+ 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
end