universal_s3_uploader 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/universal_s3_uploader/engine.rb +4 -0
 - data/lib/universal_s3_uploader/view_helper.rb +82 -0
 - data/lib/universal_s3_uploader.rb +2 -82
 - metadata +3 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 911a298d9727cc3fbe9630b6738260be106fb193
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: bbafefe7772f18d5f7a5aa1adfed149ad17fcb57
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 591aa7c0c011ae3f9d308fafc646607a757b8b58da69149ae99a188f8f7740958f38b0a3e730e0beec9e5e2a634ea4a2d0bdd72479a29501962441ed2e962bb4
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 3a05de1196c21acf28117e9705142eab21755ca151f13f1e494b9d518250acb30b5ebdddf7353c4dd15fa55b1af29218e7c89560a08fae6cce1ec3ded1dda3b3
         
     | 
| 
         @@ -0,0 +1,82 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module UniversalS3Uploader
         
     | 
| 
      
 2 
     | 
    
         
            +
            	module ViewHelper
         
     | 
| 
      
 3 
     | 
    
         
            +
            		def universal_s3_uploader_tag(key, policy_name, options = {})
         
     | 
| 
      
 4 
     | 
    
         
            +
            			uh = UploaderHelper.new(policy_name)
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            			form_tag(uh.url, {method: 'POST', enctype: 'multipart/form-data', authenticity_token: false, class: 'universal_s3_uploader'}.merge(options)) do
         
     | 
| 
      
 7 
     | 
    
         
            +
            				uh.tags(key).html_safe
         
     | 
| 
      
 8 
     | 
    
         
            +
            			end
         
     | 
| 
      
 9 
     | 
    
         
            +
            		end
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            		class UploaderHelper
         
     | 
| 
      
 12 
     | 
    
         
            +
            			def initialize(policy_name)
         
     | 
| 
      
 13 
     | 
    
         
            +
            				@config = YAML.load_file("#{Rails.root.to_s}/config/amazon.yml")
         
     | 
| 
      
 14 
     | 
    
         
            +
            				set_policy(policy_name)
         
     | 
| 
      
 15 
     | 
    
         
            +
            				set_bucket
         
     | 
| 
      
 16 
     | 
    
         
            +
            			end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            			# set expiration time
         
     | 
| 
      
 19 
     | 
    
         
            +
            			def set_policy(policy_name)
         
     | 
| 
      
 20 
     | 
    
         
            +
            				@policy = @config[policy_name]
         
     | 
| 
      
 21 
     | 
    
         
            +
            				@policy['conditions'] << ["starts-with", "$Filename", ""]	# for Flash upload
         
     | 
| 
      
 22 
     | 
    
         
            +
            				if @policy['expiration'] == '' || @policy['expiration'].nil?
         
     | 
| 
      
 23 
     | 
    
         
            +
            					@policy['expiration'] = 1.hour.from_now.iso8601
         
     | 
| 
      
 24 
     | 
    
         
            +
            				end
         
     | 
| 
      
 25 
     | 
    
         
            +
            			end
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
            			# extract bucket name
         
     | 
| 
      
 28 
     | 
    
         
            +
            			def set_bucket
         
     | 
| 
      
 29 
     | 
    
         
            +
            				@policy['conditions'].each do |condition|
         
     | 
| 
      
 30 
     | 
    
         
            +
            					if condition.class == Hash && condition.keys.first == 'bucket'
         
     | 
| 
      
 31 
     | 
    
         
            +
            						@bucket = condition.values.first
         
     | 
| 
      
 32 
     | 
    
         
            +
            						return
         
     | 
| 
      
 33 
     | 
    
         
            +
            					end
         
     | 
| 
      
 34 
     | 
    
         
            +
            				end
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
            				raise 'No bucket name in policy Exception'
         
     | 
| 
      
 37 
     | 
    
         
            +
            			end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
            			def tags(key)
         
     | 
| 
      
 40 
     | 
    
         
            +
            				av = ActionView::Base.new
         
     | 
| 
      
 41 
     | 
    
         
            +
            				tag = ''
         
     | 
| 
      
 42 
     | 
    
         
            +
             
     | 
| 
      
 43 
     | 
    
         
            +
            				([{key: key}] + @policy['conditions']).each do |condition|
         
     | 
| 
      
 44 
     | 
    
         
            +
            					if condition.class == Hash
         
     | 
| 
      
 45 
     | 
    
         
            +
            						tag += av.hidden_field_tag condition.keys.first, condition.values.first, id: nil
         
     | 
| 
      
 46 
     | 
    
         
            +
            					elsif condition.class == Array
         
     | 
| 
      
 47 
     | 
    
         
            +
            						if condition[0] == 'eq' || condition[0] == 'starts-with'
         
     | 
| 
      
 48 
     | 
    
         
            +
            							tag += av.hidden_field_tag condition[1][1..-1], condition[2], id: nil unless condition[1] == '$key'
         
     | 
| 
      
 49 
     | 
    
         
            +
            						end
         
     | 
| 
      
 50 
     | 
    
         
            +
            					else
         
     | 
| 
      
 51 
     | 
    
         
            +
            						raise 'Something in policy unexpected'
         
     | 
| 
      
 52 
     | 
    
         
            +
            					end
         
     | 
| 
      
 53 
     | 
    
         
            +
            				end
         
     | 
| 
      
 54 
     | 
    
         
            +
            				tag += av.hidden_field_tag :AWSAccessKeyId, @config['access_key_id'], id: nil
         
     | 
| 
      
 55 
     | 
    
         
            +
            				tag += av.hidden_field_tag :Policy, policy_encoded, id: nil
         
     | 
| 
      
 56 
     | 
    
         
            +
            				tag += av.hidden_field_tag :Signature, signature, id: nil
         
     | 
| 
      
 57 
     | 
    
         
            +
            				tag += av.file_field_tag :file, multiple: true, accept: 'image/*'
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
            				tag += '<object id="swfUploader" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=11,1,0,0">'
         
     | 
| 
      
 60 
     | 
    
         
            +
            				tag += '<param name="movie" value="/assets/S3Uploader.swf">'
         
     | 
| 
      
 61 
     | 
    
         
            +
            				tag += '<param name="wmode" value="transparent">'
         
     | 
| 
      
 62 
     | 
    
         
            +
            				tag += '</object>'
         
     | 
| 
      
 63 
     | 
    
         
            +
            			end
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
            			def bucket
         
     | 
| 
      
 66 
     | 
    
         
            +
            				@bucket
         
     | 
| 
      
 67 
     | 
    
         
            +
            			end
         
     | 
| 
      
 68 
     | 
    
         
            +
             
     | 
| 
      
 69 
     | 
    
         
            +
            			def url
         
     | 
| 
      
 70 
     | 
    
         
            +
            				"http://#{@bucket}.s3.amazonaws.com"
         
     | 
| 
      
 71 
     | 
    
         
            +
            			end
         
     | 
| 
      
 72 
     | 
    
         
            +
             
     | 
| 
      
 73 
     | 
    
         
            +
            			def policy_encoded
         
     | 
| 
      
 74 
     | 
    
         
            +
            				Base64.encode64(@policy.to_json).gsub("\n","")
         
     | 
| 
      
 75 
     | 
    
         
            +
            			end
         
     | 
| 
      
 76 
     | 
    
         
            +
             
     | 
| 
      
 77 
     | 
    
         
            +
            			def signature
         
     | 
| 
      
 78 
     | 
    
         
            +
            				Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), @config['secret_access_key'], policy_encoded)).gsub("\n","")
         
     | 
| 
      
 79 
     | 
    
         
            +
            			end
         
     | 
| 
      
 80 
     | 
    
         
            +
            		end
         
     | 
| 
      
 81 
     | 
    
         
            +
            	end
         
     | 
| 
      
 82 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -4,88 +4,8 @@ require 'digest/sha1' 
     | 
|
| 
       4 
4 
     | 
    
         
             
            require 'action_view'
         
     | 
| 
       5 
5 
     | 
    
         
             
            require 'rails'
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
       7 
     | 
    
         
            -
             
     | 
| 
       8 
     | 
    
         
            -
             
     | 
| 
       9 
     | 
    
         
            -
            		def universal_s3_uploader_tag(key, policy_name, options = {})
         
     | 
| 
       10 
     | 
    
         
            -
            			uh = UploaderHelper.new(policy_name)
         
     | 
| 
       11 
     | 
    
         
            -
             
     | 
| 
       12 
     | 
    
         
            -
            			form_tag(uh.url, {method: 'POST', enctype: 'multipart/form-data', authenticity_token: false, class: 'universal_s3_uploader'}.merge(options)) do
         
     | 
| 
       13 
     | 
    
         
            -
            				uh.tags(key).html_safe
         
     | 
| 
       14 
     | 
    
         
            -
            			end
         
     | 
| 
       15 
     | 
    
         
            -
            		end
         
     | 
| 
       16 
     | 
    
         
            -
             
     | 
| 
       17 
     | 
    
         
            -
            		class UploaderHelper
         
     | 
| 
       18 
     | 
    
         
            -
            			def initialize(policy_name)
         
     | 
| 
       19 
     | 
    
         
            -
            				@config = YAML.load_file("#{Rails.root.to_s}/config/amazon.yml")
         
     | 
| 
       20 
     | 
    
         
            -
            				set_policy(policy_name)
         
     | 
| 
       21 
     | 
    
         
            -
            				set_bucket
         
     | 
| 
       22 
     | 
    
         
            -
            			end
         
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
            			# set expiration time
         
     | 
| 
       25 
     | 
    
         
            -
            			def set_policy(policy_name)
         
     | 
| 
       26 
     | 
    
         
            -
            				@policy = @config[policy_name]
         
     | 
| 
       27 
     | 
    
         
            -
            				@policy['conditions'] << ["starts-with", "$Filename", ""]	# for Flash upload
         
     | 
| 
       28 
     | 
    
         
            -
            				if @policy['expiration'] == '' || @policy['expiration'].nil?
         
     | 
| 
       29 
     | 
    
         
            -
            					@policy['expiration'] = 1.hour.from_now.iso8601
         
     | 
| 
       30 
     | 
    
         
            -
            				end
         
     | 
| 
       31 
     | 
    
         
            -
            			end
         
     | 
| 
       32 
     | 
    
         
            -
             
     | 
| 
       33 
     | 
    
         
            -
            			# extract bucket name
         
     | 
| 
       34 
     | 
    
         
            -
            			def set_bucket
         
     | 
| 
       35 
     | 
    
         
            -
            				@policy['conditions'].each do |condition|
         
     | 
| 
       36 
     | 
    
         
            -
            					if condition.class == Hash && condition.keys.first == 'bucket'
         
     | 
| 
       37 
     | 
    
         
            -
            						@bucket = condition.values.first
         
     | 
| 
       38 
     | 
    
         
            -
            						return
         
     | 
| 
       39 
     | 
    
         
            -
            					end
         
     | 
| 
       40 
     | 
    
         
            -
            				end
         
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
       42 
     | 
    
         
            -
            				raise 'No bucket name in policy Exception'
         
     | 
| 
       43 
     | 
    
         
            -
            			end
         
     | 
| 
       44 
     | 
    
         
            -
             
     | 
| 
       45 
     | 
    
         
            -
            			def tags(key)
         
     | 
| 
       46 
     | 
    
         
            -
            				av = ActionView::Base.new
         
     | 
| 
       47 
     | 
    
         
            -
            				tag = ''
         
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
     | 
    
         
            -
            				([{key: key}] + @policy['conditions']).each do |condition|
         
     | 
| 
       50 
     | 
    
         
            -
            					if condition.class == Hash
         
     | 
| 
       51 
     | 
    
         
            -
            						tag += av.hidden_field_tag condition.keys.first, condition.values.first, id: nil
         
     | 
| 
       52 
     | 
    
         
            -
            					elsif condition.class == Array
         
     | 
| 
       53 
     | 
    
         
            -
            						if condition[0] == 'eq' || condition[0] == 'starts-with'
         
     | 
| 
       54 
     | 
    
         
            -
            							tag += av.hidden_field_tag condition[1][1..-1], condition[2], id: nil unless condition[1] == '$key'
         
     | 
| 
       55 
     | 
    
         
            -
            						end
         
     | 
| 
       56 
     | 
    
         
            -
            					else
         
     | 
| 
       57 
     | 
    
         
            -
            						raise 'Something in policy unexpected'
         
     | 
| 
       58 
     | 
    
         
            -
            					end
         
     | 
| 
       59 
     | 
    
         
            -
            				end
         
     | 
| 
       60 
     | 
    
         
            -
            				tag += av.hidden_field_tag :AWSAccessKeyId, @config['access_key_id'], id: nil
         
     | 
| 
       61 
     | 
    
         
            -
            				tag += av.hidden_field_tag :Policy, policy_encoded, id: nil
         
     | 
| 
       62 
     | 
    
         
            -
            				tag += av.hidden_field_tag :Signature, signature, id: nil
         
     | 
| 
       63 
     | 
    
         
            -
            				tag += av.file_field_tag :file, multiple: true, accept: 'image/*'
         
     | 
| 
       64 
     | 
    
         
            -
             
     | 
| 
       65 
     | 
    
         
            -
            				tag += '<object id="swfUploader" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=11,1,0,0">'
         
     | 
| 
       66 
     | 
    
         
            -
            				tag += '<param name="movie" value="/assets/S3Uploader.swf">'
         
     | 
| 
       67 
     | 
    
         
            -
            				tag += '<param name="wmode" value="transparent">'
         
     | 
| 
       68 
     | 
    
         
            -
            				tag += '</object>'
         
     | 
| 
       69 
     | 
    
         
            -
            			end
         
     | 
| 
       70 
     | 
    
         
            -
             
     | 
| 
       71 
     | 
    
         
            -
            			def bucket
         
     | 
| 
       72 
     | 
    
         
            -
            				@bucket
         
     | 
| 
       73 
     | 
    
         
            -
            			end
         
     | 
| 
       74 
     | 
    
         
            -
             
     | 
| 
       75 
     | 
    
         
            -
            			def url
         
     | 
| 
       76 
     | 
    
         
            -
            				"http://#{@bucket}.s3.amazonaws.com"
         
     | 
| 
       77 
     | 
    
         
            -
            			end
         
     | 
| 
       78 
     | 
    
         
            -
             
     | 
| 
       79 
     | 
    
         
            -
            			def policy_encoded
         
     | 
| 
       80 
     | 
    
         
            -
            				Base64.encode64(@policy.to_json).gsub("\n","")
         
     | 
| 
       81 
     | 
    
         
            -
            			end
         
     | 
| 
       82 
     | 
    
         
            -
             
     | 
| 
       83 
     | 
    
         
            -
            			def signature
         
     | 
| 
       84 
     | 
    
         
            -
            				Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), @config['secret_access_key'], policy_encoded)).gsub("\n","")
         
     | 
| 
       85 
     | 
    
         
            -
            			end
         
     | 
| 
       86 
     | 
    
         
            -
            		end
         
     | 
| 
       87 
     | 
    
         
            -
            	end
         
     | 
| 
       88 
     | 
    
         
            -
            end
         
     | 
| 
      
 7 
     | 
    
         
            +
            require 'universal_s3_uploader/engine'
         
     | 
| 
      
 8 
     | 
    
         
            +
            require 'universal_s3_uploader/view_helper'
         
     | 
| 
       89 
9 
     | 
    
         | 
| 
       90 
10 
     | 
    
         
             
            class ActionView::Base
         
     | 
| 
       91 
11 
     | 
    
         
             
            	include UniversalS3Uploader::ViewHelper
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: universal_s3_uploader
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.3
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Dohan Kim
         
     | 
| 
         @@ -19,6 +19,8 @@ extensions: [] 
     | 
|
| 
       19 
19 
     | 
    
         
             
            extra_rdoc_files: []
         
     | 
| 
       20 
20 
     | 
    
         
             
            files:
         
     | 
| 
       21 
21 
     | 
    
         
             
            - lib/universal_s3_uploader.rb
         
     | 
| 
      
 22 
     | 
    
         
            +
            - lib/universal_s3_uploader/engine.rb
         
     | 
| 
      
 23 
     | 
    
         
            +
            - lib/universal_s3_uploader/view_helper.rb
         
     | 
| 
       22 
24 
     | 
    
         
             
            - vendor/assets/flash/S3Uploader.as
         
     | 
| 
       23 
25 
     | 
    
         
             
            - vendor/assets/flash/S3Uploader.fla
         
     | 
| 
       24 
26 
     | 
    
         
             
            - vendor/assets/flash/S3Uploader.swf
         
     |