Class: S3UpNow::Generator
- Inherits:
-
Object
- Object
- S3UpNow::Generator
- Defined in:
- lib/s3-upnow/generator.rb
Instance Method Summary collapse
- #fields ⇒ Object
-
#initialize(options) ⇒ Generator
constructor
A new instance of Generator.
- #key ⇒ Object
- #policy ⇒ Object
- #policy_data ⇒ Object
- #signature ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(options) ⇒ Generator
Returns a new instance of Generator.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/s3-upnow/generator.rb', line 5 def initialize() @key_starts_with = [:key_starts_with] || "uploads/" @options = .reverse_merge( aws_access_key_id: S3UpNow.config.access_key_id, aws_secret_access_key: S3UpNow.config.secret_access_key, bucket: [:bucket] || S3UpNow.config.bucket, region: S3UpNow.config.region || "s3", url: S3UpNow.config.url, ssl: true, acl: "public-read", expiration: 10.hours.from_now.utc.iso8601, max_file_size: 500.megabytes, callback_method: "POST", callback_param: "file", key_starts_with: @key_starts_with, key: key ) end |
Instance Method Details
#fields ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/s3-upnow/generator.rb', line 24 def fields { :utf8 => '✓', :key => @options[:key] || key, :acl => @options[:acl], "AWSAccessKeyId" => @options[:aws_access_key_id], :policy => policy, :signature => signature, :success_action_status => "201", 'X-Requested-With' => 'xhr' } end |
#key ⇒ Object
37 38 39 |
# File 'lib/s3-upnow/generator.rb', line 37 def key @key ||= "#{@key_starts_with}{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}" end |
#policy ⇒ Object
45 46 47 |
# File 'lib/s3-upnow/generator.rb', line 45 def policy Base64.encode64(policy_data.to_json).gsub("\n", "") end |
#policy_data ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/s3-upnow/generator.rb', line 49 def policy_data { expiration: @options[:expiration], conditions: [ ["starts-with", "$utf8", ""], ["starts-with", "$key", @options[:key_starts_with]], ["starts-with", "$x-requested-with", ""], ["content-length-range", 0, @options[:max_file_size]], ["starts-with","$content-type", @options[:content_type_starts_with] ||""], {bucket: @options[:bucket]}, {acl: @options[:acl]}, {success_action_status: "201"} ] + (@options[:conditions] || []) } end |
#signature ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/s3-upnow/generator.rb', line 65 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha1'), @options[:aws_secret_access_key], policy ) ).gsub("\n", "") end |
#url ⇒ Object
41 42 43 |
# File 'lib/s3-upnow/generator.rb', line 41 def url @options[:url] || "http#{@options[:ssl] ? 's' : ''}://#{@options[:region]}.amazonaws.com/#{@options[:bucket]}/" end |