Module: S3DirectRails::UploaderHelper

Defined in:
lib/s3_direct_rails/uploader_helper.rb

Instance Method Summary collapse

Instance Method Details

#s3_bucket_urlObject



25
26
27
# File 'lib/s3_direct_rails/uploader_helper.rb', line 25

def s3_bucket_url
  "http://#{S3DirectRails.s3_uploader_bucket}.s3.amazonaws.com"
end

#s3_key(path) ⇒ Object



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

def s3_key(path)
  "#{path}/${filename}"
end

#s3_policy(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/s3_direct_rails/uploader_helper.rb', line 33

def s3_policy(options = {})
  options[:content_type] ||= ''
  options[:acl] ||= 'public-read'
  options[:max_file_size] ||= 5.gigabytes
  options[:path] ||= ''

  Base64.encode64(
    "{'expiration': '#{10.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')}',
      'conditions': [
        {'bucket': '#{S3DirectRails.s3_uploader_bucket}'},
        ['starts-with', '$key', ''],
        {'acl': '#{options[:acl]}'},
        {'success_action_status': '201'},
        ['content-length-range', 0, #{options[:max_file_size]}],
        ['starts-with','$Content-Type','']
      ]
  }").gsub(/\n|\r/, '')
end

#s3_signature(options = {}) ⇒ Object



52
53
54
55
56
57
# File 'lib/s3_direct_rails/uploader_helper.rb', line 52

def s3_signature options = {}
  Base64.encode64(
    OpenSSL::HMAC.digest(
    OpenSSL::Digest::Digest.new('sha1'),
    S3DirectRails.s3_secret_access_key, s3_policy(options))).gsub("\n","")
end

#s3_uploader(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/s3_direct_rails/uploader_helper.rb', line 4

def s3_uploader(options = {})
  options[:uploader_path] ||= File.join(S3DirectRails.s3_uploader_folder,'uploader.html')
  options[:uploaded_files_path] ||= "uploads/#{controller_name}/:uuid"
  options[:create_resource_url] ||= url_for(only_path: false)
  options[:resource_name] ||= controller_name.singularize

  upload_params = { key: s3_key(options[:uploaded_files_path]),
                    AWSAccessKeyId: S3DirectRails.s3_access_key_id,
                    bucket: s3_bucket_url,
                    _policy: s3_policy(path: options[:uploaded_files_path]),
                    _signature: s3_signature(path: options[:uploaded_files_path]),
                    uploader_id: options[:uploader_id] }.to_query

   :iframe, '',
              src: "#{s3_bucket_url}/#{options[:uploader_path]}?#{upload_params}",
              frameborder: 0,
              height: options[:iframe_height] || 40,
              width: options[:iframe_width] || 350,
              data: { create_resource_url: options[:create_resource_url] }
end