Class: S3FormPresenter::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_form_presenter.rb

Constant Summary collapse

HIDDEN_FIELD_NAMES =
:key, :access_key, :acl, :redirect_url, :policy, :signature
ACCESSOR_FIELDS =
HIDDEN_FIELD_NAMES - [:policy, :signature]
RENAMED_FIELDS =
{:redirect_url => "success_action_redirect", :access_key => "AWSAccessKeyId"}
REQUIRED_ATTRIBUTES =
[:bucket, :secret_key] + HIDDEN_FIELD_NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, redirect_url, options = {}, &block) ⇒ Form

Returns a new instance of Form.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/s3_form_presenter.rb', line 13

def initialize(key, redirect_url, options={}, &block)
  @key = key
  @access_key = options[:access_key] || ENV["AWS_ACCESS_KEY_ID"]
  @secret_key = options[:secret_key] || ENV["AWS_SECRET_ACCESS_KEY"]
  @bucket = options[:bucket] || ENV["AWS_S3_BUCKET"]
  @acl = options[:acl] || :private
  @extra_form_attributes = options[:extra_form_attributes]
  @redirect_url = redirect_url
  if block_given?
    @inner_content = block.call
  else
    @inner_content = %Q(<input name="file" type="file"><input type="submit" value="Upload File" class="btn btn-primary">)
  end
  generate_hidden_field_accessors
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



11
12
13
# File 'lib/s3_form_presenter.rb', line 11

def bucket
  @bucket
end

#extra_form_attributesObject

Returns the value of attribute extra_form_attributes.



11
12
13
# File 'lib/s3_form_presenter.rb', line 11

def extra_form_attributes
  @extra_form_attributes
end

#inner_contentObject

Returns the value of attribute inner_content.



11
12
13
# File 'lib/s3_form_presenter.rb', line 11

def inner_content
  @inner_content
end

#secret_keyObject

Returns the value of attribute secret_key.



11
12
13
# File 'lib/s3_form_presenter.rb', line 11

def secret_key
  @secret_key
end

#starts_withObject

Returns the value of attribute starts_with.



11
12
13
# File 'lib/s3_form_presenter.rb', line 11

def starts_with
  @starts_with
end

Instance Method Details



33
34
35
# File 'lib/s3_form_presenter.rb', line 33

def footer
  %Q(</form>)
end

#headerObject



29
30
31
# File 'lib/s3_form_presenter.rb', line 29

def header
  %Q(<form action="https://#{bucket}.s3.amazonaws.com/" method="post" enctype="multipart/form-data"#{extra_form_attributes}>)
end

#hidden_field(name, value) ⇒ Object



43
44
45
46
# File 'lib/s3_form_presenter.rb', line 43

def hidden_field(name, value)
  name = RENAMED_FIELDS[name] || name
  %Q(<input type="hidden" name="#{name}" value="#{value}">)
end

#hidden_fieldsObject



37
38
39
40
41
# File 'lib/s3_form_presenter.rb', line 37

def hidden_fields
  HIDDEN_FIELD_NAMES.map do |field|
    hidden_field(field, send(field))
  end
end

#policyObject



57
58
59
# File 'lib/s3_form_presenter.rb', line 57

def policy
  Base64.encode64(policy_object.to_json).gsub(/\n|\r/, '')
end

#policy_objectObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/s3_form_presenter.rb', line 65

def policy_object
  {
    "expiration" => policy_expiration,
    "conditions" => [
                     {"bucket" => bucket},
                     ["starts-with", "$key", "#{starts_with}"],
                     {"acl" => acl},
                     {"success_action_redirect" => redirect_url}
                    ]
  }
end

#signatureObject



61
62
63
# File 'lib/s3_form_presenter.rb', line 61

def signature
  Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_key, policy)).gsub("\n","")
end

#to_htmlObject



48
49
50
51
52
53
54
55
# File 'lib/s3_form_presenter.rb', line 48

def to_html
  validate_required
  content = ""
  content += header
  content += hidden_fields.join
  content += inner_content
  content += footer
end