Module: D2S3::ViewHelpers

Includes:
Signature
Defined in:
lib/d2s3/view_helpers.rb

Instance Method Summary collapse

Methods included from Signature

#assert, #b64_hmac_sha1, #binb2b64, #binb2hex, #core_hmac_sha1, #core_sha1, #hex_sha1, #rol, #safe_add, #self_test, #sha1_ft, #sha1_kt, #str2binb

Instance Method Details

#s3_http_upload_tag(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/d2s3/view_helpers.rb', line 7

def s3_http_upload_tag(options = {})
bucket          = D2S3::S3Config.bucket
access_key_id   = D2S3::S3Config.access_key_id
key             = options[:key] || ''
content_type    = options[:content_type] || '' # Defaults to binary/octet-stream if blank
redirect        = options[:redirect] || '/'
acl             = options[:acl] || 'public-read'
expiration_date = (options[:expiration_date] || 10.hours).from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
max_filesize    = options[:max_filesize] || 1.megabyte
submit_button   = options[:submit_button] || '<input type="submit" value="Upload">'

options[:form] ||= {}
options[:form][:id] ||= 'upload-form'
options[:form][:class] ||= 'upload-form'

policy = Base64.encode64(
  "{'expiration': '#{expiration_date}',
    'conditions': [
      {'bucket': '#{bucket}'},
      ['starts-with', '$key', '#{key}'],
      {'acl': '#{acl}'},
      {'success_action_redirect': '#{redirect}'},
      ['starts-with', '#{content_type}', ''],
      ['content-length-range', 0, #{max_filesize}]
    ]
  }").gsub(/\n|\r/, '')

  signature = b64_hmac_sha1(D2S3::S3Config.secret_access_key, policy)
  out = ""
  out << %(
    <form action="https://#{bucket}.s3.amazonaws.com/" method="post" enctype="multipart/form-data" id="#{options[:form][:id]}" class="#{options[:form][:class]}" style="#{options[:form][:style]}">
    <input type="hidden" name="key" value="#{key}/${filename}">
    <input type="hidden" name="AWSAccessKeyId" value="#{access_key_id}">
    <input type="hidden" name="acl" value="#{acl}">
    <input type="hidden" name="success_action_redirect" value="#{redirect}">
    <input type="hidden" name="policy" value="#{policy}">
    <input type="hidden" name="signature" value="#{signature}">
    <input name="file" type="file">#{submit_button}
    </form>
  )
end