Class: Gluttonberg::Asset
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Gluttonberg::Asset
- Includes:
- Library::AttachmentMixin
- Defined in:
- app/models/gluttonberg/asset.rb
Constant Summary collapse
- GIGA_SIZE =
constants for formatted file size
1073741824.0
- MEGA_SIZE =
1048576.0
- KILO_SIZE =
1024.0
Instance Attribute Summary collapse
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #absolute_file_path ⇒ Object
- #absolute_file_path_without_extension ⇒ Object
- #alt_or_title ⇒ Object
- #auto_set_asset_type ⇒ Object
-
#category ⇒ Object
returns category of asset.
- #copy_audios_to_s3 ⇒ Object
- #filename_without_extension ⇒ Object
- #formatted_file_size ⇒ Object
-
#set_category_and_type ⇒ Object
find out and set type and category of file.
- #title ⇒ Object
- #type_name ⇒ Object
Methods included from Library::AttachmentMixin
Instance Attribute Details
#type ⇒ Object
Returns the value of attribute type.
30 31 32 |
# File 'app/models/gluttonberg/asset.rb', line 30 def type @type end |
Class Method Details
.clear_all_asset_types ⇒ Object
96 97 98 99 100 101 |
# File 'app/models/gluttonberg/asset.rb', line 96 def self.clear_all_asset_types all.each do |asset| asset.asset_type = nil asset.save end end |
.create_assets_from_ftp ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/models/gluttonberg/asset.rb', line 122 def self.create_assets_from_ftp collection = AssetCollection.find_by_name("BULKS") files = Dir.entries(Rails.root+"/bulks") files.each do |entry| unless entry.starts_with?(".") || entry.starts_with?("__") file = MyFile2.init(entry) asset_name_with_extention = entry.split(".").first asset_params = {:name => asset_name_with_extention , :file => file } @asset = Asset.create(asset_params.merge({:asset_collection_ids => collection.id.to_s})) end end end |
.refresh_all_asset_types ⇒ Object
89 90 91 92 93 94 |
# File 'app/models/gluttonberg/asset.rb', line 89 def self.refresh_all_asset_types all.each do |asset| asset.auto_set_asset_type asset.save end end |
Instance Method Details
#absolute_file_path ⇒ Object
110 111 112 |
# File 'app/models/gluttonberg/asset.rb', line 110 def absolute_file_path Rails.root.to_s + "/public" + self.url.to_s end |
#absolute_file_path_without_extension ⇒ Object
118 119 120 |
# File 'app/models/gluttonberg/asset.rb', line 118 def absolute_file_path_without_extension Rails.root.to_s + "/public" + self.url.split(".").first unless self.file_name.blank? end |
#alt_or_title ⇒ Object
37 38 39 40 41 42 43 |
# File 'app/models/gluttonberg/asset.rb', line 37 def alt_or_title unless alt.blank? alt else title end end |
#auto_set_asset_type ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'app/models/gluttonberg/asset.rb', line 79 def auto_set_asset_type self.asset_type = AssetType.for_file(mime_type, file_name) cat = self.category.to_s.downcase if cat == "image" self.type = "Photo" elsif cat == "video" self.type = "Video" end end |
#category ⇒ Object
returns category of asset
46 47 48 49 50 51 52 |
# File 'app/models/gluttonberg/asset.rb', line 46 def category if asset_type.nil? then Library::UNCATEGORISED_CATEGORY else asset_type.asset_category.name end end |
#copy_audios_to_s3 ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/gluttonberg/asset.rb', line 140 def copy_audios_to_s3 puts "--------copy_audios_to_s3" key_id = Gluttonberg::Setting.get_setting("s3_key_id") key_val = Gluttonberg::Setting.get_setting("s3_access_key") s3_server_url = Gluttonberg::Setting.get_setting("s3_server_url") s3_bucket = Gluttonberg::Setting.get_setting("s3_bucket") if !key_id.blank? && !key_val.blank? && !s3_server_url.blank? && !s3_bucket.blank? s3 = Aws::S3.new(key_id, key_val, {:server => s3_server_url}) bucket = s3.bucket(s3_bucket) begin local_file = Pathname.new(location_on_disk) base_name = File.basename(local_file) folder = self.asset_hash date = Time.now+1.years puts "Copying #{base_name} to #{s3_bucket}" key = bucket.key("user_assets/" + folder + "/" + base_name, true) key.put(File.open(local_file), 'public-read', {"Expires" => date.rfc2822, "content-type" => "audio/mp3"}) self.update_attributes(:copied_to_s3 => true) puts "Copied" rescue => e puts "#{base_name} failed to copy" puts "** #{e} **" end end end |
#filename_without_extension ⇒ Object
114 115 116 |
# File 'app/models/gluttonberg/asset.rb', line 114 def filename_without_extension self.file_name.split(".").first unless self.file_name.blank? end |
#formatted_file_size ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/gluttonberg/asset.rb', line 66 def formatted_file_size precision = 2 unless size.blank? case when size == 1 then "1 Byte" when size < KILO_SIZE then "%d Bytes" % size when size < MEGA_SIZE then "%.#{precision}f KB" % (size / KILO_SIZE) when size < GIGA_SIZE then "%.#{precision}f MB" % (size / MEGA_SIZE) else "%.#{precision}f GB" % (size / GIGA_SIZE) end end end |
#set_category_and_type ⇒ Object
find out and set type and category of file
104 105 106 107 108 |
# File 'app/models/gluttonberg/asset.rb', line 104 def set_category_and_type unless file.nil? auto_set_asset_type end end |
#title ⇒ Object
54 55 56 |
# File 'app/models/gluttonberg/asset.rb', line 54 def title self.name end |
#type_name ⇒ Object
58 59 60 61 62 63 64 |
# File 'app/models/gluttonberg/asset.rb', line 58 def type_name if asset_type.nil? then Library::UNCATEGORISED_CATEGORY else asset_type.name end end |