Class: DigitalOpera::Builders::S3FormBuilder
- Defined in:
- lib/digital_opera/builders/s3_form_builder.rb
Class Method Summary collapse
Instance Method Summary collapse
- #fields ⇒ Object
- #form_options ⇒ Object
-
#initialize(options) ⇒ S3FormBuilder
constructor
A new instance of S3FormBuilder.
- #key ⇒ Object
- #policy ⇒ Object
- #policy_data ⇒ Object
- #signature ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(options) ⇒ S3FormBuilder
Returns a new instance of S3FormBuilder.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 7 def initialize() = { id: "file_upload", aws_access_key_id: ENV["S3_ACCESS_KEY"], aws_secret_access_key: ENV["S3_SECRET_ACCESS_KEY"], bucket: S3Uploader.bucket, acl: "authenticated-read", expiration: 10.hours.from_now.to_time.iso8601, max_file_size: nil, as: "file", post_method: 'post', class: '', success_action_redirect: nil, data: {} }.merge() end |
Class Method Details
.bucket ⇒ Object
113 114 115 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 113 def self.bucket ENV["S3_BUCKET_NAME"] end |
.url ⇒ Object
109 110 111 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 109 def self.url "https://#{S3Uploader.bucket}.s3.amazonaws.com/" end |
Instance Method Details
#fields ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 49 def fields hash = { :key => key, :acl => [:acl], :policy => policy, :signature => signature, "AWSAccessKeyId" => [:aws_access_key_id] #:success_action_redirect => @options[:post] } if [:success_action_redirect].present? hash[:success_action_redirect] = [:success_action_redirect] end hash end |
#form_options ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 26 def hash = { id: [:id], method: "post", authenticity_token: false, multipart: true, class: [:class], data: { post: [:post], as: [:as], post_method: [:post_method], post_authenticity_token: [:post_authenticity_token] } } hash[:data][:max_file_size] = [:max_file_size] if [:max_file_size].present? [:data].each do |k,v| hash[:data][k] = v end hash end |
#key ⇒ Object
65 66 67 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 65 def key @key ||= "accounts/#{@account.id.to_s}/#{@options[:type]}/#{SecureRandom.hex}/${filename}" end |
#policy ⇒ Object
73 74 75 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 73 def policy Base64.encode64(policy_data.to_json).gsub("\n", "") end |
#policy_data ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 77 def policy_data hash = { expiration: [:expiration], conditions: [ ["starts-with", "$utf8", ""], ["starts-with", "$key", ""], # ["content-length-range", 0, @options[:max_file_size]], {bucket: [:bucket]}, {acl: [:acl]} ] } if [:max_file_size].present? hash[:conditions].push( ["content-length-range", 0, [:max_file_size]] ) end if [:success_action_redirect].present? hash[:conditions].push( { success_action_redirect: [:success_action_redirect] } ) end hash end |
#signature ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 100 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), [:aws_secret_access_key], policy ) ).gsub("\n", "") end |
#url ⇒ Object
69 70 71 |
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 69 def url S3Uploader.url end |