require 'image_fu/version' require 'image_fu/client' require 'base64' require 'openssl' require 'uuidtools' require 'rack/utils' module ImageFu CONFIG = { :access_key_id => ENV['IMAGE_FU_AWS_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY'], :secret_access_key => ENV['IMAGE_FU_AWS_SECRET'] || ENV['AWS_SECRET'], :upload_bucket => ENV['IMAGE_FU_UPLOAD_BUCKET'] || ENV['UPLOAD_BUCKET'], :server => 'http://image-fu.heroku-apps.com' } def self.config(options) CONFIG.merge!(options) end def self.signed_upload_params(options = {}) options.reverse_merge!( :acl => 'private', :max_size => 10.megabytes, :expiry => 1.hour, :bucket => CONFIG[:upload_bucket] ) key = UUIDTools::UUID.random_create.to_s policy = { 'expiration' => Time.now.in(options[:expiry]).utc, 'conditions' => [ {'bucket' => options[:bucket]}, {'acl' => options[:acl]}, {'success_action_status' => '201'}, # {'key' => key}, ['content-length-range', 0, options[:max_size] || DEFAULT_PARAMS[:max_size]], ['starts-with', '$key', ''], ['starts-with', '$Filename', ''] ] } policy = Base64.encode64(policy.to_json).gsub("\n","") policy_hash = OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), CONFIG[:secret_access_key], policy) signature = Base64.encode64(policy_hash).gsub("\n","") { :server => CONFIG[:server], :bucket => options[:bucket], :parameters => { :key => key, :AWSAccessKeyId => CONFIG[:access_key_id], :acl => options[:acl], :success_action_status => "201", :policy => policy, :signature => Rack::Utils.escape_path(signature) } } end def self.get_image(id) client.get_image(id) end def self.client ImageFu::Client.new end end require 'image_fu/engine' if defined?(::Rails::Engine)