Class: EffectiveAssetsS3Helper::S3Uploader

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/effective_assets_s3_helper.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ S3Uploader

Returns a new instance of S3Uploader.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/effective_assets_s3_helper.rb', line 23

def initialize(options)
  @options = options.reverse_merge(
    aws_access_key_id: EffectiveAssets.aws_access_key_id,
    aws_secret_access_key: EffectiveAssets.aws_secret_access_key,
    bucket: EffectiveAssets.aws_bucket,
    aws_acl: EffectiveAssets.aws_acl,
    expiration: 10.hours.from_now.utc.iso8601,
    max_file_size: 1000.megabytes,
    key_starts_with: "#{EffectiveAssets.aws_path.chomp('/')}/",
    key: '${filename}' # We use an AJAX request to update this key to a useful value
  )
end

Instance Method Details

#fieldsObject



36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/effective_assets_s3_helper.rb', line 36

def fields
  {
    :key => @options[:key],
    :acl => @options[:aws_acl],
    'AWSAccessKeyId' => @options[:aws_access_key_id],
    :policy => policy,
    :signature => signature,
    :success_action_status => '201',
    'X-Requested-With' => 'xhr'
  }
end

#policyObject



48
49
50
# File 'app/helpers/effective_assets_s3_helper.rb', line 48

def policy
  Base64.encode64(policy_data.to_json).gsub("\n", "")
end

#policy_dataObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/effective_assets_s3_helper.rb', line 52

def policy_data
  {
    expiration: @options[:expiration],
    conditions: [
      ['starts-with', '$key', @options[:key_starts_with]],
      ['starts-with', '$x-requested-with', ''],
      ['content-length-range', 0, @options[:max_file_size]],
      ['starts-with','$content-type', @options[:content_type_starts_with] || ''],
      {:bucket => @options[:bucket]},
      {:acl => @options[:aws_acl]},
      {:success_action_status => '201'}
    ] + (@options[:conditions] || [])
  }
end

#signatureObject



67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/effective_assets_s3_helper.rb', line 67

def signature
  raise 'effective_assets config.aws_secret_access_key is blank. Please provide a value in config/initializers/effective_assets.rb' if @options[:aws_secret_access_key].blank?

  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::SHA1.new(),
      @options[:aws_secret_access_key].to_s, policy
    )
  ).gsub("\n", "")
end