Class: S3DirectUpload::UploadHelper::S3Uploader
- Inherits:
-
Object
- Object
- S3DirectUpload::UploadHelper::S3Uploader
- Defined in:
- lib/s3_direct_upload/form_helper.rb
Instance Method Summary collapse
- #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.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/s3_direct_upload/form_helper.rb', line 13 def initialize() @options = .reverse_merge( aws_access_key_id: S3DirectUpload.config.access_key_id, aws_secret_access_key: S3DirectUpload.config.secret_access_key, bucket: S3DirectUpload.config.bucket, acl: "public-read", expiration: 10.hours.from_now.utc.iso8601, max_file_size: 500.megabytes, as: "file", key: key ) end |
Instance Method Details
#fields ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/s3_direct_upload/form_helper.rb', line 40 def fields { :key => @options[:key] || key, :acl => @options[:acl], :policy => policy, :signature => signature, "AWSAccessKeyId" => @options[:aws_access_key_id], success_action_status: "201" } end |
#form_options ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/s3_direct_upload/form_helper.rb', line 26 def { id: @options[:id], class: @options[:class], method: "post", authenticity_token: false, multipart: true, data: { post: @options[:post], as: @options[:as] }.reverse_merge(@options[:data] || {}) } end |
#key ⇒ Object
51 52 53 |
# File 'lib/s3_direct_upload/form_helper.rb', line 51 def key @key ||= "uploads/#{SecureRandom.hex}/${filename}" end |
#policy ⇒ Object
59 60 61 |
# File 'lib/s3_direct_upload/form_helper.rb', line 59 def policy Base64.encode64(policy_data.to_json).gsub("\n", "") end |
#policy_data ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/s3_direct_upload/form_helper.rb', line 63 def policy_data { expiration: @options[:expiration], conditions: [ ["starts-with", "$utf8", ""], ["starts-with", "$key", ""], ["content-length-range", 0, @options[:max_file_size]], ["starts-with","$Content-Type",""], {bucket: @options[:bucket]}, {acl: @options[:acl]}, {success_action_status: "201"} ] } end |
#signature ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/s3_direct_upload/form_helper.rb', line 78 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), @options[:aws_secret_access_key], policy ) ).gsub("\n", "") end |
#url ⇒ Object
55 56 57 |
# File 'lib/s3_direct_upload/form_helper.rb', line 55 def url "https://s3.amazonaws.com/#{@options[:bucket]}/" end |