Module: UploadsHelper

Defined in:
app/helpers/uploads_helper.rb

Instance Method Summary collapse

Instance Method Details

#s3_keyObject



31
32
33
# File 'app/helpers/uploads_helper.rb', line 31

def s3_key
  @s3_key ||= SecureRandom.hex(8).scan(/..../).join('/')
end

#s3_post_params(options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/uploads_helper.rb', line 2

def s3_post_params(options = {})
  acl = options[:acl] || 'public-read'
  expiration = options[:expiration] || 6.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')
  max_filesize = options[:max_filesize] || 2.gigabyte

  policy = Base64.encode64(
    { 'expiration' => expiration,
      'conditions' => [
        {'bucket' => UploadJuicer::Config.s3['bucket']},
        ['starts-with', '$key', s3_key],
        {'acl' => acl},
        {'success_action_status' => '201'},
        ['starts-with', '$Filename', ''],
        ['content-length-range', 0, max_filesize]
      ]
    }.to_json)

  signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), UploadJuicer::Config.s3['secret_access_key'], policy)).gsub("\n", "")

  {
    "key" => "#{s3_key}/${filename}",
    "AWSAccessKeyId" => "#{UploadJuicer::Config.s3['access_key_id']}",
    "acl" => "#{acl}",
    "policy" => "#{policy}",
    "signature" => "#{signature}",
    "success_action_status" => "201"
  }
end

#s3_upload_urlObject



35
36
37
# File 'app/helpers/uploads_helper.rb', line 35

def s3_upload_url
  @s3_upload_url ||= "http://#{UploadJuicer::Config.s3['bucket']}.s3.amazonaws.com/"
end

#swfupload_paramsObject



39
40
41
# File 'app/helpers/uploads_helper.rb', line 39

def swfupload_params
  { :upload_url => s3_upload_url, :post_params => s3_post_params }.to_json.html_safe
end