Sha256: 5ad392dcb851af34921773cc2cecb7974f9dc533134a1796516fa8f1fa52d030

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

class EffectiveAssetsUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :fog

  def store_dir
    "#{EffectiveAssets.aws_final_path.chomp('/')}/#{model.id}"
  end

  # Returns a Hash as per the versions above
  # {:thumb=>{:width=>256, :height=>70}, :full_page=>{:width=>940, :height=>nil}}
  def versions_info
    @versions_info ||= calculate_versions_info
  end

  protected

  # record_info
  # Messy hash merging to a serialized field.
  # It has the effect of setting asset.versions_info to a Hash, such as
  #---
  # :medium:
  #   :data_size: 22259
  #   :height: 400
  #   :width: 400
  # :thumb:
  #   :data_size: 3105
  #   :height: 128
  #   :width: 128

  def record_info(version)
    if model and model.respond_to?(:versions_info) and @file.present?
      info = {}
      info[:data_size] = @file.size

      img = MiniMagick::Image.open(@file.file)
      info[:width] = img[:width]
      info[:height] = img[:height]

      model.versions_info.merge!({version.to_sym => info})
    end
  end

  def image?(new_file)
    new_file.present? and new_file.content_type.to_s.include?('image') and !(new_file.content_type.include?('icon'))
  end

  def calculate_versions_info
    retval = {}

    self.class.versions.each do |k, v|
      v[:uploader].processors.each do |processor|
        dimensions = processor[1]

        if processor[0].to_s.include?('resize') and dimensions.kind_of?(Array) and dimensions.length == 2
          retval[k] = {:width => dimensions.first, :height => dimensions.last}
          break
        end
      end
    end

    retval
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
effective_assets-0.1 app/uploaders/effective_assets_uploader.rb