Class: Caboose::Media
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Media
- Defined in:
- app/models/caboose/media.rb
Class Method Summary collapse
Instance Method Summary collapse
- #api_hash ⇒ Object
- #download_file_from_url(url) ⇒ Object
- #download_image_from_url(url) ⇒ Object
- #duplicate(site_id) ⇒ Object
- #file_url ⇒ Object
- #image_urls ⇒ Object
- #is_image? ⇒ Boolean
-
#process ⇒ Object
before_post_process :set_content_dispositon def set_content_dispositon self.sample.options.merge({ :s3_headers => { “Content-Disposition” => “attachment; filename=#selfself.name” }}) end.
- #reprocess_image ⇒ Object
- #set_file_content_type(content_type) ⇒ Object
- #set_image_content_type(content_type) ⇒ Object
Class Method Details
.upload_name(str) ⇒ Object
162 163 164 165 |
# File 'app/models/caboose/media.rb', line 162 def self.upload_name(str) return '' if str.nil? return File.basename(str, File.extname(str)).downcase.gsub(' ', '-').gsub(/[^\w-]/, '') end |
Instance Method Details
#api_hash ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/caboose/media.rb', line 131 def api_hash { :id => self.id, :name => self.name, :original_name => self.original_name, :description => self.description, :processed => self.processed, :image_urls => self.image_urls, :file_url => self.file ? self.file.url : nil, :media_type => self.is_image? ? 'image' : 'file', :media_category_id => self.media_category_id } end |
#download_file_from_url(url) ⇒ Object
125 126 127 128 129 |
# File 'app/models/caboose/media.rb', line 125 def download_file_from_url(url) self.image = URI.parse(url) self.processed = true self.save end |
#download_image_from_url(url) ⇒ Object
119 120 121 122 123 |
# File 'app/models/caboose/media.rb', line 119 def download_image_from_url(url) self.image = URI.parse(url) self.processed = true self.save end |
#duplicate(site_id) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/models/caboose/media.rb', line 176 def duplicate(site_id) cat = Caboose::MediaCategory.top_category(site_id) m = Caboose::Media.create( :media_category_id => cat.id , :name => self.name , :description => self.description , :original_name => self.original_name , :image_file_name => self.image_file_name , :image_content_type => self.image_content_type , :image_file_size => self.image_file_size , :image_updated_at => self.image_updated_at , :file_file_name => self.file_file_name , :file_content_type => self.file_content_type , :file_file_size => self.file_file_size , :file_updated_at => self.file_updated_at , :processed => false ) m.delay(:queue => 'caboose_media').download_image_from_url(self.image.url(:original)) if self.image m.delay(:queue => 'caboose_media').download_file_from_url(self.file.url) if self.file return m end |
#file_url ⇒ Object
167 168 169 170 |
# File 'app/models/caboose/media.rb', line 167 def file_url return self.image.url(:original) if self.image && !self.image.url(:original).starts_with?('http://placehold.it') return self.file.url end |
#image_urls ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'app/models/caboose/media.rb', line 152 def image_urls return nil if self.image.nil? || self.image.url(:tiny).starts_with?('http://placehold.it') return { :tiny_url => self.image.url(:tiny), :thumb_url => self.image.url(:thumb), :large_url => self.image.url(:large), :original_url => self.image.url(:original) } end |
#is_image? ⇒ Boolean
145 146 147 148 149 150 |
# File 'app/models/caboose/media.rb', line 145 def is_image? image_extensions = ['.jpg', '.jpeg', '.gif', '.png', '.tif'] ext = File.extname(self.original_name).downcase return true if image_extensions.include?(ext) return false end |
#process ⇒ Object
before_post_process :set_content_dispositon def set_content_dispositon
self.sample..merge({ :s3_headers => { "Content-Disposition" => "attachment; filename=#{self.name}" }})
end
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/models/caboose/media.rb', line 45 def process #return if self.processed config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env] AWS.config({ :access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'] }) bucket = config['bucket'] bucket = Caboose::uploads_bucket && Caboose::uploads_bucket.strip.length > 0 ? Caboose::uploads_bucket : "#{bucket}-uploads" key = "#{self.media_category_id}_#{self.original_name}" key = URI.encode(key.gsub(' ', '+')) uri = "http://#{bucket}.s3.amazonaws.com/#{key}" content_type = self.image_content_type || self.file_content_type if is_image? self.image = URI.parse(uri) self.image_content_type = content_type else self.file = URI.parse(uri) self.file_content_type = content_type end self.processed = true self.save # Set the content-type metadata on S3 if !is_image? self.set_file_content_type(content_type) end # Remember when the last upload processing happened s = Caboose::Setting.where(:site_id => self.media_category.site_id, :name => 'last_upload_processed').first s = Caboose::Setting.create(:site_id => self.media_category.site_id, :name => 'last_upload_processed') if s.nil? s.value = DateTime.now.utc.strftime("%FT%T%z") s.save # Remove the temp file bucket = AWS::S3::Bucket.new(bucket) obj = bucket.objects[key] obj.delete end |
#reprocess_image ⇒ Object
172 173 174 |
# File 'app/models/caboose/media.rb', line 172 def reprocess_image self.image.reprocess! end |
#set_file_content_type(content_type) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/caboose/media.rb', line 105 def set_file_content_type(content_type) config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env] AWS.config({ :access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'] }) s3 = AWS::S3.new bucket = s3.buckets[config['bucket']] ext = File.extname(self.file_file_name)[1..-1] # has_attached_file :file, :path => ':caboose_prefixmedia/:id_:media_name.:extension' k = "media/#{self.id}_#{self.name}.#{ext}" bucket.objects[k].copy_from(k, :content_type => content_type) # a copy needs to be done to change the content-type end |
#set_image_content_type(content_type) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/caboose/media.rb', line 90 def set_image_content_type(content_type) config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env] AWS.config({ :access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'] }) s3 = AWS::S3.new bucket = s3.buckets[config['bucket']] ext = File.extname(self.image_file_name)[1..-1] self.image.styles.each do |style| k = "media/#{self.id}_#{self.name}_#{style}.#{ext}" bucket.objects[k].copy_from(k, :content_type => content_type) # a copy needs to be done to change the content-type end end |