Sha256: a53a0821d32edde2a67f4b6e35357d90b0bc21aec5a3485aceb5bbe73ee33ae1

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require 'base64'
require 'openssl'
require 'digest/sha1'
require 'json'

module S3CorsFileupload
  class PolicyHelper
    attr_reader :options

    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

    # generate the policy document that amazon is expecting.
    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

    # sign our request by Base64 encoding the policy document.
    def upload_signature
      Base64.encode64(
        OpenSSL::HMAC.digest(
          OpenSSL::Digest::Digest.new('sha1'),
          options[:secret_access_key],
          self.policy_document
        )
      ).gsub(/\n/, '')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
s3_cors_fileupload-0.1.5 lib/s3_cors_fileupload/rails/policy_helper.rb
s3_cors_fileupload-0.1.4 lib/s3_cors_fileupload/rails/policy_helper.rb