class Smooster::Deploy::MediaAsset

Attributes

checksum[RW]
file_path[RW]
folder_path[RW]
smo_id[RW]

Public Instance Methods

attributes() click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 12
def attributes
    {'file_path' => file_path, 'checksum' => checksum, 'smo_id' => smo_id}
end
download(keep=true) click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 39
def download(keep=true)
  begin
    response = RestClient.get("#{Smooster::Application.instance.api_url()}/#{Smooster::Application.instance.site_id()}/media_assets/find_by_slug/#{self.folder_path}/#{File.basename(self.file_path)}", {"Authorization" => 'Token token="'+ Smooster::Application.instance.api_key() +'"', "Accept" => 'application/vnd.smoosterid.v2'})
    data = JSON.parse(response)
    self.checksum = open(data['file']['url']) {|f| Digest::MD5.hexdigest(f.read) }
    self.smo_id = data['smo_id']
    self.save
  rescue => e
    Smooster::Application.instance.logger.error "Error in media_asset download for #{self.file_path}!: #{e} / #{e.backtrace.inspect}"
    puts "This file failed to load: #{self.file_path}".colorize(:red)
    File.delete(self.file_path) if keep == false
  end
end
file_path=(file_path) click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 16
def file_path=(file_path)
  @file_path = file_path
  file_path = file_path.gsub("#{Smooster::Application.instance.base_dir}/#{Smooster::Application.instance.html_folder()}/","")
  self.folder_path= File.dirname(file_path)
  @file_path
end
load_checksum() click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 27
def load_checksum
  store.transaction { store[self.short_file_path][:checksum] if store[self.short_file_path].present? }
end
load_smo_id() click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 31
def load_smo_id
  store.transaction { store[self.short_file_path][:smo_id] if store[self.short_file_path].present? }
end
save() click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 85
def save
  store.transaction do
    store[self.short_file_path] = {:checksum => self.checksum.to_s, :smo_id => self.smo_id}
  end
end
short_file_path() click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 35
def short_file_path
  self.file_path.gsub("#{Smooster::Application.instance.base_dir}/#{Smooster::Application.instance.html_folder()}","")
end
store() click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 23
def store
  Smooster::Application.instance.media_assets_store
end
upload() click to toggle source
# File lib/smooster/deploy/media_asset.rb, line 53
def upload
  return false if self.checksum.to_s == self.load_checksum.to_s

  unless /^[a-zA-Z0-9\/\-]+$/.match(self.folder_path)
    puts "The folder names must contain ONLY letters, numbers and hyphens (-): #{self.folder_path}".colorize(:red)
    return false
  end

  if self.smo_id.present?
    begin
      response = RestClient.put "#{Smooster::Application.instance.api_url()}/#{Smooster::Application.instance.site_id()}/media_assets/#{self.smo_id}", {:media_asset => {:file => File.new(self.file_path, 'rb'), :folder_path => self.folder_path}}, {"Authorization" => 'Token token="'+ Smooster::Application.instance.api_key() +'"', "Accept" => 'application/vnd.smoosterid.v2'}
    rescue => e
      puts "This file couldn't be updated: #{self.file_path}".colorize(:red)
    end
  else
    begin
      response = RestClient.post "#{Smooster::Application.instance.api_url()}/#{Smooster::Application.instance.site_id()}/media_assets", {:media_asset => {:file => File.new(self.file_path, 'rb'), :folder_path => self.folder_path}}, {"Authorization" => 'Token token="'+ Smooster::Application.instance.api_key() +'"', "Accept" => 'application/vnd.smoosterid.v2'}
    rescue => e
      Smooster::Application.instance.logger.error "Error in media_asset upload for #{self.file_path}!: #{e}"
      puts "This file couldn't be uploaded: #{self.file_path}".colorize(:red)
    end
  end

  if response.present?
    data = JSON.parse(response)
    #RestClient.purge data['file']['url'] if self.smo_id.present? && Smooster::Application.instance.api_url() != "http://127.0.0.1/api"
    self.smo_id = data['smo_id']
    self.save
  end

end