Module: S3DirectRails::UploaderHelper
- Defined in:
- lib/s3_direct_rails/uploader_helper.rb
Instance Method Summary collapse
- #s3_bucket_url ⇒ Object
- #s3_key(path) ⇒ Object
- #s3_policy(options = {}) ⇒ Object
- #s3_signature(options = {}) ⇒ Object
- #s3_uploader(options = {}) ⇒ Object
Instance Method Details
#s3_bucket_url ⇒ Object
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( = {}) [:content_type] ||= '' [:acl] ||= 'public-read' [:max_file_size] ||= 5.gigabytes [: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': '#{[:acl]}'}, {'success_action_status': '201'}, ['content-length-range', 0, #{[: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 = {} Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), S3DirectRails.s3_secret_access_key, s3_policy())).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( = {}) [:uploader_path] ||= File.join(S3DirectRails.s3_uploader_folder,'uploader.html') [:uploaded_files_path] ||= "uploads/#{controller_name}/:uuid" [:create_resource_url] ||= url_for(only_path: false) [:resource_name] ||= controller_name.singularize upload_params = { key: s3_key([:uploaded_files_path]), AWSAccessKeyId: S3DirectRails.s3_access_key_id, bucket: s3_bucket_url, _policy: s3_policy(path: [:uploaded_files_path]), _signature: s3_signature(path: [:uploaded_files_path]), uploader_id: [:uploader_id] }.to_query content_tag :iframe, '', src: "#{s3_bucket_url}/#{[:uploader_path]}?#{upload_params}", frameborder: 0, height: [:iframe_height] || 40, width: [:iframe_width] || 350, data: { create_resource_url: [:create_resource_url] } end |