Class: S3Assets::Uploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
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

Instance Method Details

#aclObject



90
91
92
# File 'lib/s3_assets/uploader.rb', line 90

def acl
  'public-read'
end

#asset_hostObject



41
42
43
# File 'lib/s3_assets/uploader.rb', line 41

def asset_host
  "https://#{::S3Assets.fog_permanent_bucket}.s3.amazonaws.com"
end

#fog_attributesObject



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_expirationObject



45
46
47
# File 'lib/s3_assets/uploader.rb', line 45

def fog_authenticated_url_expiration
  10.hours
end

#fog_credentialsObject



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_directoryObject



37
38
39
# File 'lib/s3_assets/uploader.rb', line 37

def fog_directory
  ::S3Assets.fog_permanent_bucket
end

#fog_publicObject



19
20
21
# File 'lib/s3_assets/uploader.rb', line 19

def fog_public
  true
end

#ignore_download_errorsObject



57
58
59
# File 'lib/s3_assets/uploader.rb', line 57

def ignore_download_errors
  false
end

#ignore_integrity_errorsObject



49
50
51
# File 'lib/s3_assets/uploader.rb', line 49

def ignore_integrity_errors
  false
end

#ignore_processing_errorsObject



53
54
55
# File 'lib/s3_assets/uploader.rb', line 53

def ignore_processing_errors
  false
end

#original_filenameObject



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(options={})
  Base64.encode64(policy_doc(options).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(options={})
  options[:expiration] ||= UPLOAD_EXPIRATION
  options[:max_file_size] ||= MAX_FILE_SIZE
  success_action_status = options[:success_action_status]
  store_dir = self.temp_store_dir

  doc = {
      'expiration' => Time.now.utc + options[:expiration],
      'conditions' => [
          ["starts-with", "$key", store_dir],
          {"bucket" => ::S3Assets.fog_temp_bucket},
          {"acl" => acl},
          ["content-length-range", 1, options[: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_regexpObject



15
16
17
# File 'lib/s3_assets/uploader.rb', line 15

def sanitize_regexp
  /[^[:word:]\.\-\+]/
end

#set_model_content_typeObject



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

#storageObject



33
34
35
# File 'lib/s3_assets/uploader.rb', line 33

def storage
  return :fog
end

#store_dirObject



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_dirObject



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_urlObject



61
62
63
# File 'lib/s3_assets/uploader.rb', line 61

def upload_url
  "https://#{::S3Assets.fog_temp_bucket}.s3.amazonaws.com"
end

#urlObject

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