Class: S3CorsFileupload::PolicyHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_cors_fileupload/rails/policy_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_options = {}) ⇒ PolicyHelper

Returns a new instance of PolicyHelper.



10
11
12
13
14
15
16
17
# File 'lib/s3_cors_fileupload/rails/policy_helper.rb', line 10

def initialize(_options = {})
  # default max_file_size to 500 MB if nothing is received
  @options = {
    :acl => 'public-read',
    :max_file_size => Config.max_file_size || 524288000,
    :bucket => Config.bucket
  }.merge(_options).merge(:secret_access_key => Config.secret_access_key)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/s3_cors_fileupload/rails/policy_helper.rb', line 8

def options
  @options
end

Instance Method Details

#policy_documentObject

generate the policy document that amazon is expecting.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/s3_cors_fileupload/rails/policy_helper.rb', line 20

def policy_document
  Base64.encode64(
    {
      expiration: 1.hour.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z'),
      conditions: [
        { bucket: options[:bucket] },
        { acl: options[:acl] },
        { success_action_status: '201' },
        ["content-length-range", 0, options[:max_file_size]],
        ["starts-with", "$utf8", ""],
        ["starts-with", "$key", ""],
        ["starts-with", "$Content-Type", ""]
      ]
    }.to_json
  ).gsub(/\n|\r/, '')
end

#upload_signatureObject

sign our request by Base64 encoding the policy document.



38
39
40
41
42
43
44
45
46
# File 'lib/s3_cors_fileupload/rails/policy_helper.rb', line 38

def upload_signature
  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::Digest.new('sha1'),
      options[:secret_access_key],
      self.policy_document
    )
  ).gsub(/\n/, '')
end