Sha256: 259e7ad9554ebc68abc329e7494415744bdf245ef89d670e2e67130d96815c11

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

require 'mini_magick'
require 'carrierwave'
require 'fastimage'

module Lanes::Concerns

    class AssetUploader < CarrierWave::Uploader::Base

        include CarrierWave::MiniMagick

        process :store_attributes

        version :medium,  :if => :image? do
            process :resize_to_fit => [800, 800]
        end

        version :thumbnail,  :if => :image? do
            process :resize_to_fit => [200,200]
        end

        def cache_dir
            '/tmp'
        end

        def filename
            if original_filename && model && model.read_attribute(mounted_as).present?
                model.read_attribute(mounted_as)
            end
        end

        def store_dir
            token = secure_token
            "#{token[0, 2]}/#{token[2, 2]}"
        end

        protected

        def store_attributes
            if file && model
                model.metadata['size'] = file.size
                model.metadata['content_type'] = file.content_type
                if image?(file)
                    img = ::MiniMagick::Image.open(file.file)
                    model.metadata['width'] = img.width
                    model.metadata['height'] = img.height
                end
            end
        end

        def image?(new_file)
            new_file.content_type.include? 'image'
        end

        def secure_token
            model[mounted_as] ||= ::Lanes::Strings.random
        end

    end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lanes-0.5.6 lib/lanes/concerns/asset_uploader.rb
lanes-0.5.5 lib/lanes/concerns/asset_uploader.rb
lanes-0.5.0 lib/lanes/concerns/asset_uploader.rb