Class: DigitalOpera::Builders::S3FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/digital_opera/builders/s3_form_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ S3FormBuilder

Returns a new instance of S3FormBuilder.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 7

def initialize(options)


  @options = {
    id: "file_upload",
    aws_access_key_id: ENV["S3_ACCESS_KEY"],
    aws_secret_access_key: ENV["S3_SECRET_ACCESS_KEY"],
    bucket: S3Uploader.bucket,
    acl: "authenticated-read",
    expiration: 10.hours.from_now.to_time.iso8601,
    max_file_size: nil,
    as: "file",
    post_method: 'post',
    class: '',
    success_action_redirect: nil,
    data: {}
  }.merge(options)
end

Class Method Details

.bucketObject



113
114
115
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 113

def self.bucket
  ENV["S3_BUCKET_NAME"]
end

.urlObject



109
110
111
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 109

def self.url
  "https://#{S3Uploader.bucket}.s3.amazonaws.com/"
end

Instance Method Details

#fieldsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 49

def fields
  hash = {
    :key => key,
    :acl => @options[:acl],
    :policy => policy,
    :signature => signature,
    "AWSAccessKeyId" => @options[:aws_access_key_id]
    #:success_action_redirect => @options[:post]
  }
  if @options[:success_action_redirect].present?
    hash[:success_action_redirect] = @options[:success_action_redirect]
  end

  hash
end

#form_optionsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 26

def form_options
  hash = {
    id: @options[:id],
    method: "post",
    authenticity_token: false,
    multipart: true,
    class: @options[:class],
    data: {
      post: @options[:post],
      as: @options[:as],
      post_method: @options[:post_method],
      post_authenticity_token: @options[:post_authenticity_token]
    }
  }

  hash[:data][:max_file_size] = @options[:max_file_size] if @options[:max_file_size].present?
  @options[:data].each do |k,v|
    hash[:data][k] = v
  end

  hash
end

#keyObject



65
66
67
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 65

def key
  @key ||= "accounts/#{@account.id.to_s}/#{@options[:type]}/#{SecureRandom.hex}/${filename}"
end

#policyObject



73
74
75
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 73

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

#policy_dataObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 77

def policy_data
  hash = {
    expiration: @options[:expiration],
    conditions: [
      ["starts-with", "$utf8", ""],
      ["starts-with", "$key", ""],
      # ["content-length-range", 0, @options[:max_file_size]],
      {bucket: @options[:bucket]},
      {acl: @options[:acl]}
    ]
  }

  if @options[:max_file_size].present?
    hash[:conditions].push( ["content-length-range", 0, @options[:max_file_size]] )
  end

  if @options[:success_action_redirect].present?
    hash[:conditions].push( { success_action_redirect: @options[:success_action_redirect] } )
  end

  hash
end

#signatureObject



100
101
102
103
104
105
106
107
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 100

def signature
  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest::Digest.new('sha1'),
      @options[:aws_secret_access_key], policy
    )
  ).gsub("\n", "")
end

#urlObject



69
70
71
# File 'lib/digital_opera/builders/s3_form_builder.rb', line 69

def url
  S3Uploader.url
end