Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for signed authenticated URLs

  • Loading branch information...
commit af13e11d6b612ec15106e19ad5ed67b0b1e3e8af 1 parent baab164
@TalLevAmi TalLevAmi authored
View
1  cloudinary.gemspec
@@ -19,5 +19,6 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_dependency "rest-client"
+ s.add_dependency "aws_cf_signer"
s.add_development_dependency "rspec"
end
View
5 lib/cloudinary.rb
@@ -42,6 +42,11 @@ def self.config(new_config=nil)
"private_cdn" => !uri.path.blank?,
"secure_distribution" => uri.path[1..-1]
)
+ uri.query.to_s.split("&").each do
+ |param|
+ key, value = param.split("=")
+ set_config(key=>URI.decode(value))
+ end
end
set_config(new_config) if new_config
View
4 lib/cloudinary/helper.rb
@@ -154,6 +154,10 @@ def cl_private_download_url(public_id, format, options = {})
def cl_zip_download_url(tag, options = {})
Cloudinary::Utils.zip_download_url(tag, options)
end
+
+ def cl_signed_download_url(public_id, options = {})
+ Cloudinary::Utils.signed_download_url(public_id, options)
+ end
def self.included(base)
ActionView::Helpers::FormBuilder.send(:include, Cloudinary::FormBuilder)
View
15 lib/cloudinary/utils.rb
@@ -1,6 +1,7 @@
# Copyright Cloudinary
require 'digest/sha1'
require 'zlib'
+require 'aws_cf_signer'
class Cloudinary::Utils
SHARED_CDN = "d3jpl91pxevbkh.cloudfront.net"
@@ -154,7 +155,7 @@ def self.cloudinary_api_url(action = 'upload', options = {})
def self.private_download_url(public_id, format, options = {})
api_key = options[:api_key] || Cloudinary.config.api_key || raise("Must supply api_key")
api_secret = options[:api_secret] || Cloudinary.config.api_secret || raise("Must supply api_secret")
- cloudinary_params = {:timestamp=>Time.now.to_i, :public_id=>public_id, :format=>format}.reject{|k, v| v.blank?}
+ cloudinary_params = {:timestamp=>Time.now.to_i, :public_id=>public_id, :format=>format, :type=>options[:type]}.reject{|k, v| v.blank?}
cloudinary_params[:signature] = Cloudinary::Utils.api_sign_request(cloudinary_params, api_secret)
cloudinary_params[:api_key] = api_key
return Cloudinary::Utils.cloudinary_api_url("download", options) + "?" + cloudinary_params.to_query
@@ -169,7 +170,17 @@ def self.zip_download_url(tag, options = {})
return Cloudinary::Utils.cloudinary_api_url("download_tag.zip", options) + "?" + cloudinary_params.to_query
end
-
+ def self.signed_download_url(public_id, options = {})
+ aws_private_key_path = options[:aws_private_key_path] || Cloudinary.config.aws_private_key_path || raise("Must supply aws_private_key_path")
+ aws_key_pair_id = options[:aws_key_pair_id] || Cloudinary.config.aws_key_pair_id || raise("Must supply aws_key_pair_id")
+ authenticated_distribution = options[:authenticated_distribution] || Cloudinary.config.authenticated_distribution || raise("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.cloudinary_url(public_id, options.merge(:secure=>true, :secure_distribution=>authenticated_distribution, :private_cdn=>true))
+ expiration = options[:expiration] || (Time.now+3600)
+ signer.sign(url, :ending => expiration)
+ end
+
def self.asset_file_name(path)
data = Rails.root.join(path).read(:mode=>"rb")
ext = path.extname
Please sign in to comment.
Something went wrong with that request. Please try again.