Class: S3DirectUp::Uploader
- Inherits:
-
Object
- Object
- S3DirectUp::Uploader
- Defined in:
- lib/s3_direct_up/uploader.rb
Constant Summary collapse
- DefaultOptions =
{ expiration: 3600, min_file_size: 1, max_file_size: 10.megabytes }
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#uploader ⇒ Object
readonly
Returns the value of attribute uploader.
Instance Method Summary collapse
- #direct_upload_url ⇒ Object
- #encoded_policy ⇒ Object
- #get_bucket ⇒ Object
- #get_fog_file(file_path) ⇒ Object
- #get_fog_storage ⇒ Object
-
#initialize(uploader = nil, options = {}) ⇒ Uploader
constructor
A new instance of Uploader.
- #move_file(original_file, dest_file) ⇒ Object
- #plupload_params ⇒ Object
- #policy_raw ⇒ Object
- #refresh_policy ⇒ Object
- #signature ⇒ Object
- #store_dir ⇒ Object
Constructor Details
#initialize(uploader = nil, options = {}) ⇒ Uploader
Returns a new instance of Uploader.
13 14 15 16 17 |
# File 'lib/s3_direct_up/uploader.rb', line 13 def initialize(uploader=nil, ={}) @uploader=uploader||CarrierWave::Uploader::Base @options=DefaultOptions.merge() refresh_policy end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/s3_direct_up/uploader.rb', line 11 def @options end |
#uploader ⇒ Object (readonly)
Returns the value of attribute uploader.
11 12 13 |
# File 'lib/s3_direct_up/uploader.rb', line 11 def uploader @uploader end |
Instance Method Details
#direct_upload_url ⇒ Object
44 45 46 |
# File 'lib/s3_direct_up/uploader.rb', line 44 def direct_upload_url CarrierWave::Storage::Fog::File.new(uploader, CarrierWave::Storage::Fog.new(uploader), nil).public_url end |
#encoded_policy ⇒ Object
85 86 87 |
# File 'lib/s3_direct_up/uploader.rb', line 85 def encoded_policy Base64.encode64(@policy_raw.to_json).gsub("\n", '') end |
#get_bucket ⇒ Object
27 28 29 |
# File 'lib/s3_direct_up/uploader.rb', line 27 def get_bucket get_fog_storage.directories.get(@uploader.fog_directory) end |
#get_fog_file(file_path) ⇒ Object
31 32 33 34 |
# File 'lib/s3_direct_up/uploader.rb', line 31 def get_fog_file(file_path) bucket=self.get_bucket return bucket.files.get(file_path) end |
#get_fog_storage ⇒ Object
23 24 25 |
# File 'lib/s3_direct_up/uploader.rb', line 23 def get_fog_storage @fog_storage||=Fog::Storage.new(@uploader.fog_credentials) end |
#move_file(original_file, dest_file) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/s3_direct_up/uploader.rb', line 36 def move_file(original_file, dest_file) remote_file=get_fog_file(original_file) new_remote_file=remote_file.copy(@uploader.fog_directory, dest_file) new_remote_file.public=remote_file.public_url.present? new_remote_file.save remote_file.destroy end |
#plupload_params ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/s3_direct_up/uploader.rb', line 74 def plupload_params { key: "#{store_dir}/${filename}", AWSAccessKeyId: uploader.fog_credentials[:aws_access_key_id], acl: uploader.fog_public ? 'public-read' : 'private', success_action_status: '201', policy: encoded_policy, signature: signature } end |
#policy_raw ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/s3_direct_up/uploader.rb', line 52 def policy_raw conditions = [ ["starts-with", "$key", self.store_dir] ] conditions << ["starts-with", "$Content-Type", ""] if [:will_include_content_type] conditions += [:policy_conditions] if [:policy_conditions].present? conditions_sub_policy = [ {"bucket" => uploader.fog_directory}, {"acl" => uploader.fog_public ? 'public-read' : 'private'}, ["content-length-range", [:min_file_size], [:max_file_size]] ] conditions_sub_policy << ([:success_action_redirect].present? ? {"success_action_redirect"=>[:success_action_redirect]} : {"success_action_status"=>([:success_action_status] || '201')}) policy_hash={ 'expiration' => Time.now.utc + [:expiration], 'conditions' => conditions + conditions_sub_policy } return policy_hash end |
#refresh_policy ⇒ Object
19 20 21 |
# File 'lib/s3_direct_up/uploader.rb', line 19 def refresh_policy @policy_raw=policy_raw end |
#signature ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/s3_direct_up/uploader.rb', line 89 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), uploader.fog_credentials[:aws_secret_access_key], encoded_policy ) ).gsub("\n","") end |
#store_dir ⇒ Object
48 49 50 |
# File 'lib/s3_direct_up/uploader.rb', line 48 def store_dir [:store_dir]|| @uploader.respond_to?(:direct_store_dir) ? @uploader.direct_store_dir : nil end |