Sha256: 196cf4c58c21c5e13a3c709a7bbf61ddde61e3a8f7bf4da69fbfa7d75f562855

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

# encoding: utf-8

class AssetUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  def store_dir
    "sites/#{model.collection.site_id}/assets/#{model.id}"
  end
  
  def cache_dir
    "#{Rails.root}/tmp/uploads"
  end
  
  version :thumb do
    process :resize_to_fill => [50, 50]
    process :convert => 'png'
  end
  
  version :medium do
    process :resize_to_fill => [80, 80]
    process :convert => 'png'
  end

  version :preview do
    process :resize_to_fit => [880, 1100]
    process :convert => 'png'    
  end
  
  process :set_content_type  
  process :set_size
  process :set_width_and_height

  def set_content_type
    value = :other
    
    content_type = file.content_type == 'application/octet-stream' ? File.mime_type?(original_filename) : file.content_type
    
    self.class.content_types.each_pair do |type, rules|
      rules.each do |rule|
        case rule
        when String then value = type if content_type == rule
        when Regexp then value = type if (content_type =~ rule) == 0
        end
      end
    end
    
    puts "content_type = #{value}"
    
    model.content_type = value
  end
  
  def set_size
    model.size = file.size
  end
  
  def set_width_and_height
    if model.image?
      model.width, model.height = `identify -format "%wx%h" #{file.path}`.split(/x/).collect(&:to_i)
    end
  end
    
  def self.content_types
    {
      :image      => ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg'],
      :movie      => [/^video/, 'application/x-shockwave-flash', 'application/x-swf'],
      :audio      => [/^audio/, 'application/ogg', 'application/x-mp3'],
      :pdf        => ['application/pdf', 'application/x-pdf'],
      :stylesheet => ['text/css'],
      :javascript => ['text/javascript', 'text/js', 'application/x-javascript', 'application/javascript']
    }  
  end
    
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
locomotive_cms-0.0.3.1 app/uploaders/asset_uploader.rb
locomotive_cms-0.0.2.9 app/uploaders/asset_uploader.rb
locomotive_cms-0.0.2.8 app/uploaders/asset_uploader.rb