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_image_from_url(url) ⇒ 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
Class Method Details
.upload_name(str) ⇒ Object
105 106 107 108 |
# File 'app/models/caboose/media.rb', line 105 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
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/models/caboose/media.rb', line 75 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' } end |
#download_image_from_url(url) ⇒ Object
69 70 71 72 73 |
# File 'app/models/caboose/media.rb', line 69 def download_image_from_url(url) self.image = URI.parse(url) self.processed = true self.save end |
#file_url ⇒ Object
110 111 112 113 |
# File 'app/models/caboose/media.rb', line 110 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
95 96 97 98 99 100 101 102 103 |
# File 'app/models/caboose/media.rb', line 95 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
88 89 90 91 92 93 |
# File 'app/models/caboose/media.rb', line 88 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/models/caboose/media.rb', line 35 def process #return if self.processed config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env] 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}" image_extensions = ['.jpg', '.jpeg', '.gif', '.png', '.tif'] ext = File.extname(key).downcase if image_extensions.include?(ext) self.image = URI.parse(uri) else self.file = URI.parse(uri) end self.processed = true self.save # 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
115 116 117 |
# File 'app/models/caboose/media.rb', line 115 def reprocess_image self.image.reprocess! end |