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’)



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

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(‘’)



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

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



68
69
70
# File 'app/models/effective/asset.rb', line 68

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

.string_base_pathObject



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

def string_base_path
  "string://"
end

Instance Method Details

#audio?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/models/effective/asset.rb', line 166

def audio?
  content_type.include? 'audio'
end

#authenticated_url(version = nil, expire_in = 60.minutes) ⇒ Object



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

def authenticated_url(version = nil, expire_in = 60.minutes)
  data.fog_authenticated_url_expiration = expire_in
  version.present? ? data.send(version).file.authenticated_url : data.file.authenticated_url
end

#extraObject



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

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

#file_nameObject



150
151
152
# File 'app/models/effective/asset.rb', line 150

def file_name
  upload_file.to_s.split('/').last rescue upload_file
end

#icon?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'app/models/effective/asset.rb', line 162

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

#image?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'app/models/effective/asset.rb', line 158

def image?
  content_type.include? 'image'
end

#public_url(version = nil) ⇒ Object



137
138
139
140
141
142
143
# File 'app/models/effective/asset.rb', line 137

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

#reprocess!Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/models/effective/asset.rb', line 178

def reprocess!
  begin
    Rails.logger.info "Reprocessing ##{id}..."
    print "Reprocessing ##{id}..."

    raise 'must be processed first before reprocessed' unless processed?

    data.cache_stored_file!
    data.retrieve_from_cache!(data.cache_name)
    data.recreate_versions!
    save!

    Rails.logger.info "Successfully reprocessed ##{id}"
    print "success"; puts ''
    true
  rescue => e
    Rails.logger.info  "Error: #{id} -> #{e.to_s}"
    print "error: #{e.to_s}"; puts ''
    false
  end
end

#titleObject

End of Class methods



125
126
127
# File 'app/models/effective/asset.rb', line 125

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

#to_sObject



174
175
176
# File 'app/models/effective/asset.rb', line 174

def to_s
  title
end

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



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

def url(version = nil, expire_in = nil)
  aws_acl == 'authenticated-read' ? authenticated_url(version, expire_in) : public_url(version)
end

#versions_infoObject



170
171
172
# File 'app/models/effective/asset.rb', line 170

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

#video?Boolean

Returns:

  • (Boolean)


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

def video?
  content_type.include? 'video'
end