Class: UniversalS3Uploader::ViewHelper::UploaderHelper
- Inherits:
-
Object
- Object
- UniversalS3Uploader::ViewHelper::UploaderHelper
- Defined in:
- lib/universal_s3_uploader/view_helper.rb
Instance Method Summary collapse
- #bucket ⇒ Object
- #div_tag(name, value) ⇒ Object
-
#initialize(policy_name) ⇒ UploaderHelper
constructor
A new instance of UploaderHelper.
- #policy_encoded ⇒ Object
-
#set_bucket ⇒ Object
extract bucket name.
-
#set_policy(policy_name) ⇒ Object
set expiration time.
- #signature ⇒ Object
- #tags(key, id) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(policy_name) ⇒ UploaderHelper
Returns a new instance of UploaderHelper.
10 11 12 13 14 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 10 def initialize(policy_name) @config = YAML.load_file("#{Rails.root.to_s}/config/amazon.yml") set_policy(policy_name) set_bucket end |
Instance Method Details
#bucket ⇒ Object
70 71 72 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 70 def bucket @bucket end |
#div_tag(name, value) ⇒ Object
40 41 42 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 40 def div_tag(name, value) "<div class='#{name}' data-value='#{value}'></div>" end |
#policy_encoded ⇒ Object
78 79 80 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 78 def policy_encoded Base64.encode64(@policy.to_json).gsub("\n","") end |
#set_bucket ⇒ Object
extract bucket name
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 26 def set_bucket @policy['conditions'].each do |condition| if condition.class == Hash && condition.keys.first == 'bucket' @bucket = condition.values.first return end end raise 'No bucket name in policy Exception' end |
#set_policy(policy_name) ⇒ Object
set expiration time
17 18 19 20 21 22 23 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 17 def set_policy(policy_name) @policy = @config[policy_name] @policy['conditions'] << ["starts-with", "$Filename", ""] # for Flash upload if @policy['expiration'] == '' || @policy['expiration'].nil? @policy['expiration'] = 1.hour.from_now.gmtime.iso8601 end end |
#signature ⇒ Object
82 83 84 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 82 def signature Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), @config['secret_access_key'], policy_encoded)).gsub("\n","") end |
#tags(key, id) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 37 def (key, id) av = ActionView::Base.new def div_tag(name, value) "<div class='#{name}' data-value='#{value}'></div>" end tag = "<div class='universal_s3_uploader' id='#{id}' action='#{url}'>" ([{key: key}] + @policy['conditions']).each do |condition| if condition.class == Hash tag += div_tag condition.keys.first, condition.values.first elsif condition.class == Array if condition[0] == 'eq' || condition[0] == 'starts-with' tag += div_tag condition[1][1..-1], condition[2] unless condition[1] == '$key' end else raise 'Something in policy unexpected' end end tag += div_tag 'AWSAccessKeyId', @config['access_key_id'] tag += div_tag 'Policy', policy_encoded tag += div_tag 'Signature', signature tag += av.file_field_tag :file, multiple: true, accept: 'image/*' object_id = 'flash_' + id tag += "<object id=#{'flash_' + id} class='flash_uploader' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=11,1,0,0'>" tag += '<param name="movie" value="/assets/UniversalS3Uploader.swf">' tag += '<param name="wmode" value="transparent">' tag += '</object>' tag += '</div>' end |
#url ⇒ Object
74 75 76 |
# File 'lib/universal_s3_uploader/view_helper.rb', line 74 def url "http://#{@bucket}.s3.amazonaws.com" end |