Class: Anaconda::FormBuilderHelpers::S3Uploader
- Inherits:
-
Object
- Object
- Anaconda::FormBuilderHelpers::S3Uploader
- Defined in:
- lib/anaconda/form_builder_helpers.rb
Instance Method Summary collapse
- #base_key ⇒ Object
- #fields ⇒ Object
- #form_options ⇒ Object
-
#initialize(options) ⇒ S3Uploader
constructor
A new instance of S3Uploader.
- #key ⇒ Object
- #policy ⇒ Object
- #policy_data ⇒ Object
- #signature ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(options) ⇒ S3Uploader
Returns a new instance of S3Uploader.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/anaconda/form_builder_helpers.rb', line 62 def initialize() @options = .reverse_merge( id: "fileupload", aws_access_key_id: Anaconda.aws[:aws_access_key], aws_secret_access_key: Anaconda.aws[:aws_secret_key], bucket: Anaconda.aws[:aws_bucket], acl: "public-read", expiration: 10.hours.from_now, max_file_size: 500.megabytes, as: "file" ) end |
Instance Method Details
#base_key ⇒ Object
103 104 105 |
# File 'lib/anaconda/form_builder_helpers.rb', line 103 def base_key @options[:base_key] end |
#fields ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/anaconda/form_builder_helpers.rb', line 89 def fields { :key => key, :acl => @options[:acl], :policy => policy, :signature => signature, "AWSAccessKeyId" => @options[:aws_access_key_id], } end |
#form_options ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/anaconda/form_builder_helpers.rb', line 75 def { id: @options[:id], method: "post", authenticity_token: false, multipart: true, data: { post: @options[:post], as: @options[:as], base_key: base_key } } end |
#key ⇒ Object
99 100 101 |
# File 'lib/anaconda/form_builder_helpers.rb', line 99 def key @key ||= "#{base_key}/${filename}" end |
#policy ⇒ Object
111 112 113 |
# File 'lib/anaconda/form_builder_helpers.rb', line 111 def policy Base64.encode64(policy_data.to_json).gsub("\n", "") end |
#policy_data ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/anaconda/form_builder_helpers.rb', line 115 def policy_data { expiration: @options[:expiration], conditions: [ #["starts-with", "$utf8", ""], ["starts-with", "$key", base_key], ["content-length-range", 1, @options[:max_file_size]], {bucket: @options[:bucket]}, {acl: @options[:acl]} ] } end |
#signature ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/anaconda/form_builder_helpers.rb', line 128 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), @options[:aws_secret_access_key], policy ) ).gsub("\n", "") end |
#url ⇒ Object
107 108 109 |
# File 'lib/anaconda/form_builder_helpers.rb', line 107 def url "https://s3.amazonaws.com/#{@options[:bucket]}/" end |