Module: S3lurp::ViewHelpers
- Defined in:
- lib/s3lurp/view_helpers.rb
Constant Summary collapse
- HIDDEN_FIELD_MAP =
{ :key => 'key', :aws_access_key => 'AWSAccessKeyId', :acl => 'acl', :cache_control => 'Cache-Control', :content_type => 'Content-Type', :content_disposition => 'Content-Disposition', :content_encoding => 'Content-Encoding', :expires => 'Expires', :success_action_redirect => 'success_action_redirect', :success_action_status => 'success_action_status' }
- NON_FIELD_OPTIONS =
%w( s3_bucket aws_secret_key max_file_size min_file_size amz_meta_tags minutes_valid form_html_options file_field_tag_accept multiple_files form_fields_only no_file_input submit_tag submit_tag_value submit_tag_options).map(&:to_sym)
Instance Method Summary collapse
- #s3_direct_form_fields(opt = {}) ⇒ Object
- #s3_direct_form_tag(opt = {}) ⇒ Object
- #s3_generate_amz_meta_tags(meta = {}) ⇒ Object
- #s3_generate_file_field_tag(opt = {}) ⇒ Object
- #s3_generate_policy(fields = {}, options = {}) ⇒ Object
- #s3_generate_submit_tag(opt = {}) ⇒ Object
Instance Method Details
#s3_direct_form_fields(opt = {}) ⇒ Object
25 26 27 |
# File 'lib/s3lurp/view_helpers.rb', line 25 def s3_direct_form_fields(opt ={}) s3_direct_form_tag(opt.merge!({:form_fields_only => true})) end |
#s3_direct_form_tag(opt = {}) ⇒ Object
30 31 32 33 34 35 36 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 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/s3lurp/view_helpers.rb', line 30 def s3_direct_form_tag(opt = {}) = (NON_FIELD_OPTIONS + HIDDEN_FIELD_MAP.keys).each_with_object({}) do |i, h| h[i] = opt[i] || S3lurp.config.send(i) end # configurable fields the field map is formed as {:configuration_name => "input_field_name"} hidden_fields = HIDDEN_FIELD_MAP.each_with_object({}) do |(k,v), h| h[k] = [k] unless [k].nil? || [k].blank? end = [:amz_meta_tags].is_a?(Hash) ? ([:amz_meta_tags]) : {} # generate an expiration date for the policy minutes = Integer([:minutes_valid]) rescue 360 expiration_date = minutes.minutes.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z') security_fields = {} if hidden_fields[:aws_access_key] # only generate security fields when necessary security_fields[:policy] = Base64.encode64( s3_generate_policy( hidden_fields.clone, { :meta_tags => , :s3_bucket => [:s3_bucket], :expiration => expiration_date, :min_file_size => [:min_file_size], :max_file_size => [:max_file_size] } )).gsub(/\n/,'') security_fields[:signature] = Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), [:aws_secret_key], security_fields[:policy]) ).gsub(/\n/,'') end .merge! security_fields submit = s3_generate_submit_tag() file = s3_generate_file_field_tag() form_content = ( hidden_fields.map{|k,v| hidden_field_tag(HIDDEN_FIELD_MAP[k],v, {:id => nil})}.join.html_safe + .map{|k,v| hidden_field_tag(k,v,{:id => nil})}.join.html_safe ) form_content << field_set_tag(nil,:class=>"s3lurp_file") {file} unless [:no_file_input] if [:form_fields_only] form_content else form_tag("http://s3.amazonaws.com/#{options[:s3_bucket]}", {:authenticity_token => false, :method => 'POST', :multipart => true}.merge([:form_html_options])) do ( form_content + field_set_tag(nil,:class=>"s3lurp_submit") {submit.html_safe} ) end end end |
#s3_generate_amz_meta_tags(meta = {}) ⇒ Object
111 112 113 114 115 |
# File 'lib/s3lurp/view_helpers.rb', line 111 def ( = {}) .each_with_object({}) do |(k,v), hash| hash[%(x-amz-meta-#{k})] = v end end |
#s3_generate_file_field_tag(opt = {}) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/s3lurp/view_helpers.rb', line 128 def s3_generate_file_field_tag(opt ={}) opts = {:class => 's3lurp_file_tag'} opts.merge!({:accept => opt[:file_field_tag_accept]}) if opt[:file_field_tag_accept] opts.merge!({:multiple => true}) if opt[:multiple_files] file_field_tag('file', opts) end |
#s3_generate_policy(fields = {}, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/s3lurp/view_helpers.rb', line 83 def s3_generate_policy(fields = {}, = {}) fields.delete(:aws_access_key) conditions = [ { :bucket => [:s3_bucket]}, ['content-length-range', [:min_file_size], [:max_file_size]], ['starts-with', '$utf8', ""] ] HIDDEN_FIELD_MAP.each do |field, field_name| next unless fields[field] case field when :key key = fields[field].gsub(/\/?\$\{filename\}\/?/,'') conditions.push ["starts-with", "$key", key] else conditions.push ({ field_name => fields[field] }) end end if [:meta_tags] [:meta_tags].each do |k,v| conditions.push ["starts-with", "$#{k}", ''] end end policy = { "expiration" => [:expiration], "conditions" => conditions }.to_json end |
#s3_generate_submit_tag(opt = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/s3lurp/view_helpers.rb', line 117 def s3_generate_submit_tag(opt = {}) opt[:submit_tag_options].update(:name => nil) if opt[:submit_tag] opt[:submit_tag] elsif opt[:submit_tag_value] submit_tag(opt[:submit_tag_value], opt[:submit_tag_options]) else submit_tag("Upload", opt[:submit_tag_options]) end end |