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 25 26 27 28 29 30 |
# File 'lib/s3_direct_upload/form_helper.rb', line 13 def initialize() @key_starts_with = [:key_starts_with] || "uploads/" = .reverse_merge( aws_access_key_id: S3DirectUpload.config.access_key_id, aws_secret_access_key: S3DirectUpload.config.secret_access_key, bucket: S3DirectUpload.config.bucket, region: S3DirectUpload.config.region || "s3", url: S3DirectUpload.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
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/s3_direct_upload/form_helper.rb', line 47 def fields { :key => [:key] || key, :acl => [:acl], "AWSAccessKeyId" => [:aws_access_key_id], :policy => policy, :signature => signature, :success_action_status => "201", 'X-Requested-With' => 'xhr' } end |
#form_options ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/s3_direct_upload/form_helper.rb', line 32 def { id: [:id], class: [:class], method: "post", authenticity_token: false, multipart: true, data: { callback_url: [:callback_url], callback_method: [:callback_method], callback_param: [:callback_param] }.reverse_merge([:data] || {}) } end |
#key ⇒ Object
59 60 61 |
# File 'lib/s3_direct_upload/form_helper.rb', line 59 def key @key ||= "#{@key_starts_with}{timestamp}-{unique_id}-#{SecureRandom.hex}/${filename}" end |
#policy ⇒ Object
67 68 69 |
# File 'lib/s3_direct_upload/form_helper.rb', line 67 def policy Base64.encode64(policy_data.to_json).gsub("\n", "") end |
#policy_data ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/s3_direct_upload/form_helper.rb', line 71 def policy_data { expiration: [:expiration], conditions: [ ["starts-with", "$utf8", ""], ["starts-with", "$key", [:key_starts_with]], ["starts-with", "$x-requested-with", ""], ["content-length-range", 0, [:max_file_size]], ["starts-with","$content-type",""], {bucket: [:bucket]}, {acl: [:acl]}, {success_action_status: "201"} ] + ([:conditions] || []) } end |
#signature ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/s3_direct_upload/form_helper.rb', line 87 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), [:aws_secret_access_key], policy ) ).gsub("\n", "") end |
#url ⇒ Object
63 64 65 |
# File 'lib/s3_direct_upload/form_helper.rb', line 63 def url [:url] || "http#{@options[:ssl] ? 's' : ''}://#{@options[:region]}.amazonaws.com/#{@options[:bucket]}/" end |