Class: Caboose::Media

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/media.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.upload_name(str) ⇒ Object



90
91
92
93
# File 'app/models/caboose/media.rb', line 90

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_hashObject



69
70
71
72
73
74
75
76
77
78
# File 'app/models/caboose/media.rb', line 69

def api_hash
  {
    :id           => self.id,
    :name         => self.name,
    :description  => self.description,
    :processed    => self.processed,
    :image_urls   => self.image_urls,
    :file_url     => self.file ? self.file.url : nil       
  }    
end

#file_urlObject



95
96
97
98
# File 'app/models/caboose/media.rb', line 95

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_urlsObject



80
81
82
83
84
85
86
87
88
# File 'app/models/caboose/media.rb', line 80

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

#processObject

before_post_process :set_content_dispositon def set_content_dispositon

self.sample.options.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']
      
  key = "#{self.media_category_id}_#{self.original_name}"
  uri = "http://#{bucket}-uploads.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
  
  # Set the downloadable file names
  #bucket = AWS::S3::Bucket.new(bucket)
  #['tiny', 'thumb', 'large'].each do |style|            
  #  obj = bucket.objects["media/#{self.id}_#{style}#{ext}"]
  #  obj.content_disposition = 'attachment; filename="' + self.name + '"'
  #  obj.store
  #end
         
  # Remove the temp file            
  bucket = AWS::S3::Bucket.new("#{bucket}-uploads")
  obj = bucket.objects[key]
  obj.delete
       
end

#reprocess_imageObject



100
101
102
# File 'app/models/caboose/media.rb', line 100

def reprocess_image
  self.image.reprocess!
end