Class: Gluttonberg::Asset

Inherits:
ActiveRecord::Base
  • Object
show all
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

Constants included from Library::AttachmentMixin

Library::AttachmentMixin::DEFAULT_THUMBNAILS, Library::AttachmentMixin::MAX_IMAGE_SIZE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Library::AttachmentMixin

included

Class Method Details

.clear_all_asset_typesObject



86
87
88
89
90
91
# File 'app/models/gluttonberg/asset.rb', line 86

def self.clear_all_asset_types
  all.each do |asset|
    asset.asset_type = nil
    asset.save
  end
end

.create_assets_from_ftpObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/gluttonberg/asset.rb', line 112

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_typesObject



79
80
81
82
83
84
# File 'app/models/gluttonberg/asset.rb', line 79

def self.refresh_all_asset_types
  all.each do |asset|
    asset.auto_set_asset_type
    asset.save
  end
end

Instance Method Details

#absolute_file_pathObject



100
101
102
# File 'app/models/gluttonberg/asset.rb', line 100

def absolute_file_path
  Rails.root.to_s + "/public" + self.url.to_s
end

#absolute_file_path_without_extensionObject



108
109
110
# File 'app/models/gluttonberg/asset.rb', line 108

def absolute_file_path_without_extension
  Rails.root.to_s + "/public" + self.url.split(".").first unless self.file_name.blank?
end

#alt_or_titleObject



27
28
29
30
31
32
33
# File 'app/models/gluttonberg/asset.rb', line 27

def alt_or_title
  unless alt.blank?
    alt
  else
    title
  end
end

#auto_set_asset_typeObject



69
70
71
72
73
74
75
76
77
# File 'app/models/gluttonberg/asset.rb', line 69

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

#categoryObject

returns category of asset



36
37
38
39
40
41
42
# File 'app/models/gluttonberg/asset.rb', line 36

def category
  if asset_type.nil? then
    Library::UNCATEGORISED_CATEGORY
  else
    asset_type.asset_category.name
  end
end

#copy_audios_to_s3Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/gluttonberg/asset.rb', line 130

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_extensionObject



104
105
106
# File 'app/models/gluttonberg/asset.rb', line 104

def filename_without_extension
  self.file_name.split(".").first unless self.file_name.blank?
end

#formatted_file_sizeObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/gluttonberg/asset.rb', line 56

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_typeObject

find out and set type and category of file



94
95
96
97
98
# File 'app/models/gluttonberg/asset.rb', line 94

def set_category_and_type
  unless file.nil?
    auto_set_asset_type
  end
end

#titleObject



44
45
46
# File 'app/models/gluttonberg/asset.rb', line 44

def title
  self.name
end

#type_nameObject



48
49
50
51
52
53
54
# File 'app/models/gluttonberg/asset.rb', line 48

def type_name
  if asset_type.nil? then
    Library::UNCATEGORISED_CATEGORY
  else
    asset_type.name
  end
end