lib/image_fu.rb in image_fu-0.1.0.beta1 vs lib/image_fu.rb in image_fu-0.1.0.beta2

- old
+ new

@@ -2,10 +2,11 @@ require 'image_fu/client' require 'base64' require 'openssl' require 'uuidtools' require 'rack/utils' +require 'aws-sdk' 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'], @@ -15,48 +16,38 @@ def self.config(options) CONFIG.merge!(options) end + def self.s3 + AWS::S3.new(:access_key_id => CONFIG[:access_key_id], :secret_access_key => CONFIG[:secret_access_key]) + 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', ''] - ] - } + bucket = s3.buckets[options[:bucket]] - policy = Base64.encode64(policy.to_json).gsub("\n","") - policy_hash = OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), CONFIG[:secret_access_key], policy) + form = bucket.presigned_post( + :key => key, + :expires => options[:expiry].to_i, + :acl => options[:acl], + :success_action_status => 201, + :content_length => 0..(options[:max_size]), + :ignore => ['Filename'] + ) - 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) - } + :parameters => form.fields } end def self.get_image(id) client.get_image(id)