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



119
120
121
122
# File 'app/models/caboose/media.rb', line 119

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



89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/caboose/media.rb', line 89

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_file_from_url(url) ⇒ Object



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

def download_file_from_url(url)
  self.image = URI.parse(url)
  self.processed = true
  self.save
end

#download_image_from_url(url) ⇒ Object



77
78
79
80
81
# File 'app/models/caboose/media.rb', line 77

def download_image_from_url(url)
  self.image = URI.parse(url)
  self.processed = true
  self.save
end

#duplicate(site_id) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/caboose/media.rb', line 133

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.download_image_from_url(self.image.url(:original)) if self.image
  m.delay.download_file_from_url(self.file.url) if self.file
  return m
end

#file_urlObject



124
125
126
127
# File 'app/models/caboose/media.rb', line 124

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



109
110
111
112
113
114
115
116
117
# File 'app/models/caboose/media.rb', line 109

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

Returns:

  • (Boolean)


102
103
104
105
106
107
# File 'app/models/caboose/media.rb', line 102

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

#processObject

before_post_process :set_content_dispositon def set_content_dispositon

self.sample.options.merge({ :s3_headers => { "Content-Disposition" => "attachment; filename=#{self.name}" }})

end



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
68
69
70
71
72
73
74
75
# File 'app/models/caboose/media.rb', line 36

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}"    
  
  image_extensions = ['.jpg', '.jpeg', '.gif', '.png', '.tif']
  ext = File.extname(key).downcase
  mimetype = Caboose::Mimetype.mimetype_for_extension(ext)        
  if image_extensions.include?(ext)    
    self.image = URI.parse(uri)
    self.image_content_type = mimetype if mimetype
  else
    self.file = URI.parse(uri)
    self.file_content_type = mimetype if mimetype      
  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_imageObject



129
130
131
# File 'app/models/caboose/media.rb', line 129

def reprocess_image
  self.image.reprocess!
end