require 'open-uri' module Smooster module Deploy class MediaAsset include ActiveModel::Model include ActiveModel::Serializers::JSON extend Smooster::Deploy::MediaAssets attr_accessor :file_path, :folder_path, :checksum, :smo_id def attributes {'file_path' => file_path, 'checksum' => checksum, 'smo_id' => smo_id} end 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 def store Smooster::Application.instance.media_assets_store end def load_checksum store.transaction { store[self.file_path][:checksum] if store[self.file_path].present? } end def load_smo_id store.transaction { store[self.file_path][:smo_id] if store[self.file_path].present? } end def download 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['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) end end def upload return false if self.checksum.to_s == self.load_checksum.to_s 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['id'] self.save end end def save store.transaction do store[self.file_path] = {:checksum => self.checksum.to_s, :smo_id => self.smo_id} end end end end end