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
36
37
38
# File 'lib/s3_cors_fileupload/rails/policy_helper.rb', line 20

def policy_document
  @policy_document ||=
    Base64.encode64(
      MultiJson.dump(
        {
          expiration: 10.hours.from_now.utc.iso8601(3),
          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", ""]
          ]
        }
      )
    ).gsub(/\n/, '')
end

#upload_signatureObject

sign our request by Base64 encoding the policy document.



41
42
43
44
45
46
47
48
49
50
# File 'lib/s3_cors_fileupload/rails/policy_helper.rb', line 41

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