Class: S3Assets::Uploader
- Inherits:
-
CarrierWave::Uploader::Base
- Object
- CarrierWave::Uploader::Base
- S3Assets::Uploader
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion
- Defined in:
- lib/s3_assets/uploader.rb
Constant Summary collapse
- UPLOAD_EXPIRATION =
2.hours
- MAX_FILE_SIZE =
20.megabytes
Instance Method Summary collapse
- #acl ⇒ Object
- #asset_host ⇒ Object
- #fog_attributes ⇒ Object
- #fog_authenticated_url_expiration ⇒ Object
- #fog_credentials ⇒ Object
- #fog_directory ⇒ Object
- #fog_public ⇒ Object
- #ignore_download_errors ⇒ Object
- #ignore_integrity_errors ⇒ Object
- #ignore_processing_errors ⇒ Object
- #original_filename ⇒ Object
- #policy(options = {}) ⇒ Object
- #policy_doc(options = {}) ⇒ Object
- #sanitize_regexp ⇒ Object
- #set_model_content_type ⇒ Object
- #signature(policy = nil) ⇒ Object
- #storage ⇒ Object
- #store_dir ⇒ Object
- #temp_store_dir ⇒ Object
- #upload_url ⇒ Object
-
#url ⇒ Object
override the url to return absolute url if available and revert back to standard functionality if it is not available.
Instance Method Details
#acl ⇒ Object
90 91 92 |
# File 'lib/s3_assets/uploader.rb', line 90 def acl 'public-read' end |
#asset_host ⇒ Object
41 42 43 |
# File 'lib/s3_assets/uploader.rb', line 41 def asset_host "https://#{::S3Assets.fog_permanent_bucket}.s3.amazonaws.com" end |
#fog_attributes ⇒ Object
65 66 67 68 |
# File 'lib/s3_assets/uploader.rb', line 65 def fog_attributes # cached for 1 year {'Cache-Control' => "public, max-age=#{60*60*24*365}"} end |
#fog_authenticated_url_expiration ⇒ Object
45 46 47 |
# File 'lib/s3_assets/uploader.rb', line 45 def fog_authenticated_url_expiration 10.hours end |
#fog_credentials ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/s3_assets/uploader.rb', line 23 def fog_credentials { :provider => 'AWS', :aws_access_key_id => ::S3Assets.aws_access_key_id, :aws_secret_access_key => ::S3Assets.aws_secret_access_key, :region => ::S3Assets.aws_region, :host => ::S3Assets.fog_host, } end |
#fog_directory ⇒ Object
37 38 39 |
# File 'lib/s3_assets/uploader.rb', line 37 def fog_directory ::S3Assets.fog_permanent_bucket end |
#fog_public ⇒ Object
19 20 21 |
# File 'lib/s3_assets/uploader.rb', line 19 def fog_public true end |
#ignore_download_errors ⇒ Object
57 58 59 |
# File 'lib/s3_assets/uploader.rb', line 57 def ignore_download_errors false end |
#ignore_integrity_errors ⇒ Object
49 50 51 |
# File 'lib/s3_assets/uploader.rb', line 49 def ignore_integrity_errors false end |
#ignore_processing_errors ⇒ Object
53 54 55 |
# File 'lib/s3_assets/uploader.rb', line 53 def ignore_processing_errors false end |
#original_filename ⇒ Object
10 11 12 13 |
# File 'lib/s3_assets/uploader.rb', line 10 def original_filename name = super return URI.decode(name) if name.present? end |
#policy(options = {}) ⇒ Object
114 115 116 |
# File 'lib/s3_assets/uploader.rb', line 114 def policy(={}) Base64.encode64(policy_doc().to_json).gsub("\n", "") end |
#policy_doc(options = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/s3_assets/uploader.rb', line 94 def policy_doc(={}) [:expiration] ||= UPLOAD_EXPIRATION [:max_file_size] ||= MAX_FILE_SIZE success_action_status = [:success_action_status] store_dir = self.temp_store_dir doc = { 'expiration' => Time.now.utc + [:expiration], 'conditions' => [ ["starts-with", "$key", store_dir], {"bucket" => ::S3Assets.fog_temp_bucket}, {"acl" => acl}, ["content-length-range", 1, [:max_file_size]] ] } doc['conditions'] << {"success_action_status" => success_action_status.to_s} unless success_action_status.blank? doc['conditions'] << ["starts-with", "$Content-type", ""] doc end |
#sanitize_regexp ⇒ Object
15 16 17 |
# File 'lib/s3_assets/uploader.rb', line 15 def sanitize_regexp /[^[:word:]\.\-\+]/ end |
#set_model_content_type ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/s3_assets/uploader.rb', line 128 def set_model_content_type return if file.blank? type = File.magic_number_type(file.path.to_s) rescue nil content_types = MIME::Types.type_for(type.to_s) type = File.extname(file.path) content_types |= MIME::Types.type_for(type.to_s) model.content_type = content_types.first.to_s end |
#signature(policy = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/s3_assets/uploader.rb', line 118 def signature policy=nil policy = self.policy unless policy Base64.encode64( OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha1'), AWS_SECRET_ACCESS_KEY, policy ) ).gsub("\n", "") end |
#storage ⇒ Object
33 34 35 |
# File 'lib/s3_assets/uploader.rb', line 33 def storage return :fog end |
#store_dir ⇒ Object
70 71 72 73 |
# File 'lib/s3_assets/uploader.rb', line 70 def store_dir return "prod/#{model.id.to_s}" if Rails.env.production? return "dev/#{model.id.to_s}" end |
#temp_store_dir ⇒ Object
75 76 77 78 |
# File 'lib/s3_assets/uploader.rb', line 75 def temp_store_dir return "prod/#{model.id.to_s}" if Rails.env.production? return "dev/#{model.id.to_s}" end |
#upload_url ⇒ Object
61 62 63 |
# File 'lib/s3_assets/uploader.rb', line 61 def upload_url "https://#{::S3Assets.fog_temp_bucket}.s3.amazonaws.com" end |
#url ⇒ Object
override the url to return absolute url if available and revert back to standard functionality if it is not available
82 83 84 85 86 87 88 |
# File 'lib/s3_assets/uploader.rb', line 82 def url if model.processable? model.absolute_url else super end end |