Module: CarrierWaveDirect::Uploader
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/carrierwave_direct.rb,
lib/carrierwave_direct/uploader.rb,
lib/carrierwave_direct/uploader/configuration.rb
Defined Under Namespace
Modules: Configuration
Constant Summary collapse
- FILENAME_WILDCARD =
"${filename}"
Instance Method Summary collapse
- #acl ⇒ Object
- #direct_fog_url(options = {}) ⇒ Object
- #extension_regexp ⇒ Object
- #filename ⇒ Object
- #guid ⇒ Object
- #has_key? ⇒ Boolean
- #key ⇒ Object
- #key=(k) ⇒ Object
- #key_regexp ⇒ Object
- #persisted? ⇒ Boolean
- #policy(options = {}) ⇒ Object
- #signature ⇒ Object
- #url_scheme_white_list ⇒ Object
Instance Method Details
#acl ⇒ Object
56 57 58 |
# File 'lib/carrierwave_direct/uploader.rb', line 56 def acl fog_public ? 'public-read' : 'private' end |
#direct_fog_url(options = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/carrierwave_direct/uploader.rb', line 22 def direct_fog_url( = {}) fog_uri = CarrierWave::Storage::Fog::File.new(self, CarrierWave::Storage::Fog.new(self), nil).public_url if [:with_path] uri = URI.parse(fog_uri) path = "/#{key}" uri.path = URI.escape(path) fog_uri = uri.to_s end fog_uri end |
#extension_regexp ⇒ Object
111 112 113 114 |
# File 'lib/carrierwave_direct/uploader.rb', line 111 def extension_regexp allowed_file_types = extension_white_list extension_regexp = allowed_file_types.present? && allowed_file_types.any? ? "(#{allowed_file_types.join("|")})" : "\\w+" end |
#filename ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/carrierwave_direct/uploader.rb', line 92 def filename unless has_key? # Use the attached models remote url to generate a new key otherwise return nil remote_url = model.send("remote_#{mounted_as}_url") remote_url ? key_from_file(CarrierWave::SanitizedFile.new(remote_url).filename) : return end key_path = key.split("/") filename_parts = [] filename_parts.unshift(key_path.pop) unique_key = key_path.pop filename_parts.unshift(unique_key) if unique_key filename_parts.join("/") end |
#guid ⇒ Object
33 34 35 |
# File 'lib/carrierwave_direct/uploader.rb', line 33 def guid UUID.generate end |
#has_key? ⇒ Boolean
52 53 54 |
# File 'lib/carrierwave_direct/uploader.rb', line 52 def has_key? key !~ /#{Regexp.escape(FILENAME_WILDCARD)}\z/ end |
#key ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/carrierwave_direct/uploader.rb', line 42 def key return @key if @key.present? if url.present? self.key = URI.parse(url).path # explicitly set key else @key = "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}" end @key end |
#key=(k) ⇒ Object
37 38 39 40 |
# File 'lib/carrierwave_direct/uploader.rb', line 37 def key=(k) @key = k update_version_keys(:with => @key) end |
#key_regexp ⇒ Object
107 108 109 |
# File 'lib/carrierwave_direct/uploader.rb', line 107 def key_regexp /\A#{store_dir}\/[a-f\d\-]+\/.+\.#{extension_regexp}\z/ end |
#persisted? ⇒ Boolean
88 89 90 |
# File 'lib/carrierwave_direct/uploader.rb', line 88 def persisted? false end |
#policy(options = {}) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/carrierwave_direct/uploader.rb', line 60 def policy( = {}) [:expiration] ||= self.class.upload_expiration [:max_file_size] ||= self.class.max_file_size Base64.encode64( { 'expiration' => Time.now.utc + [:expiration], 'conditions' => [ ["starts-with", "$utf8", ""], ["starts-with", "$key", store_dir], {"bucket" => fog_directory}, {"acl" => acl}, {"success_action_redirect" => success_action_redirect}, ["content-length-range", 1, [:max_file_size]] ] }.to_json ).gsub("\n","") end |
#signature ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/carrierwave_direct/uploader.rb', line 79 def signature Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest::Digest.new('sha1'), aws_secret_access_key, policy ) ).gsub("\n","") end |
#url_scheme_white_list ⇒ Object
116 117 118 |
# File 'lib/carrierwave_direct/uploader.rb', line 116 def url_scheme_white_list nil end |