Class: Effective::Asset

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/asset.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_string(str, options = {}) ⇒ Object

This loads the raw contents of a string into a file and uploads that file to s3 Expect to be passed something like Asset.create_from_string(‘some binary stuff from a string’, :filename => ‘icon_close.png’, :content_type => ‘image/png’)



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/effective/asset.rb', line 102

def create_from_string(str, options = {})
  filename = options.delete(:filename) || "file-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}.txt"

  filename = URI.escape(filename).gsub(/%\d\d|[^a-zA-Z0-9.-]/, '_')  # Replace anything not A-Z, a-z, 0-9, . -

  opts = { upload_file: "#{Asset.string_base_path}#{filename}", user_id: 1, aws_acl: EffectiveAssets.aws_acl }.merge(options)

  attempts = 3  # Try to upload this string file 3 times
  begin
    asset = Asset.new(opts)
    asset.data = AssetStringIO.new(filename, str)

    if asset.save
      asset
    else
      puts asset.errors.inspect
      Rails.logger.info asset.errors.inspect
      false
    end
  rescue => e
    (attempts -= 1) >= 0 ? (sleep 2; retry) : false
  end

end

.create_from_url(url, options = {}) ⇒ Object

Just call me with Asset.create_from_url(‘’)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/effective/asset.rb', line 80

def create_from_url(url, options = {})
  opts = { upload_file: url, user_id: 1, aws_acl: EffectiveAssets.aws_acl }.merge(options)

  attempts = 3  # Try to upload this string file 3 times
  begin
    asset = Asset.new(opts)

    if asset.save
      asset
    else
      puts asset.errors.inspect
      Rails.logger.info asset.errors.inspect
      false
    end
  rescue => e
    (attempts -= 1) >= 0 ? (sleep 2; retry) : false
  end
end

.s3_base_pathObject



71
72
73
# File 'app/models/effective/asset.rb', line 71

def s3_base_path
  "https://#{EffectiveAssets.aws_bucket}.s3.amazonaws.com/"
end

.string_base_pathObject



75
76
77
# File 'app/models/effective/asset.rb', line 75

def string_base_path
  "string://"
end

Instance Method Details

#audio?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'app/models/effective/asset.rb', line 176

def audio?
  content_type.include? 'audio/'
end

#authenticated_url(version = nil, expire_in: nil) ⇒ Object



153
154
155
156
157
158
# File 'app/models/effective/asset.rb', line 153

def authenticated_url(version = nil, expire_in: nil)
  if data.present?
    data.aws_authenticated_url_expiration = (expire_in || 60.minutes).to_i
    (version.present? ? data.send(version).file.try(:authenticated_url) : data.file.try(:authenticated_url))
  end || '#'
end

#extraObject



136
137
138
# File 'app/models/effective/asset.rb', line 136

def extra
  self[:extra] || {}
end

#file_nameObject



160
161
162
# File 'app/models/effective/asset.rb', line 160

def file_name
  upload_file.to_s.split('/').last.gsub(/\?.+/, '') rescue upload_file
end

#icon?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'app/models/effective/asset.rb', line 172

def icon?
  content_type.include? 'image/x-icon'
end

#image?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'app/models/effective/asset.rb', line 168

def image?
  content_type.include? 'image/'
end

#placeholder?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'app/models/effective/asset.rb', line 180

def placeholder?
  upload_file.blank? || upload_file == 'placeholder'.freeze
end

#process!Object Also known as: reprocess!

This method is called asynchronously by an after_commit filter



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/models/effective/asset.rb', line 189

def process!
  if placeholder?
    puts 'Placeholder Asset processing not required (this is a soft error)'
    # Do nothing
  elsif image? == false
    puts 'Non-image Asset processing not required'
    # Do Nothing
  elsif upload_file.include?(Effective::Asset.string_base_path)
    puts 'String-based Asset processing and uploading'
    data.recreate_versions!
  elsif upload_file.include?(Effective::Asset.s3_base_path)
    # Carrierwave must download the file, process it, then upload the generated versions to S3
    puts 'S3 Uploaded Asset downloading and processing'

    self.remote_data_url = url
  else
    puts 'Non S3 Asset downloading and processing'
    puts "Downloading #{upload_file}"

    self.remote_data_url = upload_file
  end

  self.processed = true
  save!
end

#public_url(version = nil) ⇒ Object



144
145
146
147
148
149
150
151
# File 'app/models/effective/asset.rb', line 144

def public_url(version = nil)
  if data.present?
    url = (version.present? ? data.send(version).file.try(:public_url) : data.file.try(:public_url))
    url || '#'
  else
   "#{Asset.s3_base_path.chomp('/')}/#{EffectiveAssets.aws_path.chomp('/')}/#{id.to_i}/#{file_name}"
  end
end

#sync_aws_acl!Object

This will set the AWS permission to aws_acl



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'app/models/effective/asset.rb', line 217

def sync_aws_acl!
  if aws_acl == EffectiveAssets::AWS_PRIVATE
    if Net::HTTP.get_response(URI(public_url)).code.to_i != 403  # Forbidden
      self.remote_data_url = public_url
      save!
    end
  else
    # Make sure I can access it at the public url
    if Net::HTTP.get_response(URI(public_url)).code.to_i != 200  # Success
      self.remote_data_url = authenticated_url
      save!
    end
  end
end

#titleObject



132
133
134
# File 'app/models/effective/asset.rb', line 132

def title
  self[:title].presence || file_name
end

#to_sObject

End of Class methods



128
129
130
# File 'app/models/effective/asset.rb', line 128

def to_s
  title
end

#url(version = nil, expire_in: nil) ⇒ Object



140
141
142
# File 'app/models/effective/asset.rb', line 140

def url(version = nil, expire_in: nil)
  aws_acl == EffectiveAssets::AWS_PRIVATE ? authenticated_url(version, expire_in: expire_in) : public_url(version)
end

#versions_infoObject



184
185
186
# File 'app/models/effective/asset.rb', line 184

def versions_info
  self[:versions_info] || {}
end

#video?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'app/models/effective/asset.rb', line 164

def video?
  content_type.include? 'video/'
end