2011-06-08 14:34:37 +00:00
|
|
|
module Locomotive
|
|
|
|
module CarrierWave
|
|
|
|
module Uploader
|
|
|
|
module Asset
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
|
|
|
|
process :set_content_type
|
|
|
|
process :set_size
|
|
|
|
process :set_width_and_height
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
|
|
|
|
def content_types
|
|
|
|
{
|
|
|
|
:image => ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg', 'image/x-icon'],
|
2011-12-07 01:09:13 +00:00
|
|
|
:media => [/^video/, 'application/x-shockwave-flash', 'application/x-flash-video', 'application/x-swf', /^audio/, 'application/ogg', 'application/x-mp3'],
|
2011-06-08 14:34:37 +00:00
|
|
|
:pdf => ['application/pdf', 'application/x-pdf'],
|
|
|
|
:stylesheet => ['text/css'],
|
2011-08-29 08:46:31 +00:00
|
|
|
:javascript => ['text/javascript', 'text/js', 'application/x-javascript', 'application/javascript', 'text/x-component'],
|
2011-06-08 14:34:37 +00:00
|
|
|
:font => ['application/x-font-ttf', 'application/vnd.ms-fontobject', 'image/svg+xml', 'application/x-woff']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-02-10 23:39:06 +00:00
|
|
|
def set_content_type(*args)
|
|
|
|
value = :other
|
2011-06-08 14:34:37 +00:00
|
|
|
|
2012-02-10 23:39:06 +00:00
|
|
|
content_type = file.content_type == 'application/octet-stream' ? File.mime_type?(original_filename) : file.content_type
|
2011-06-08 14:34:37 +00:00
|
|
|
|
2012-02-10 23:39:06 +00:00
|
|
|
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
|
2011-06-08 14:34:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-10 23:39:06 +00:00
|
|
|
model.content_type = value
|
|
|
|
end
|
2011-06-08 14:34:37 +00:00
|
|
|
|
2012-02-10 23:39:06 +00:00
|
|
|
def set_size(*args)
|
|
|
|
model.size = file.size
|
|
|
|
end
|
2011-06-08 14:34:37 +00:00
|
|
|
|
2012-02-10 23:39:06 +00:00
|
|
|
def set_width_and_height
|
|
|
|
if model.image?
|
|
|
|
magick = ::Magick::Image.read(current_path).first
|
|
|
|
model.width, model.height = magick.columns, magick.rows
|
2011-06-08 14:34:37 +00:00
|
|
|
end
|
2012-02-10 23:39:06 +00:00
|
|
|
end
|
2011-06-08 14:34:37 +00:00
|
|
|
|
2012-02-10 23:39:06 +00:00
|
|
|
def image?(file)
|
|
|
|
model.image?
|
2011-06-08 14:34:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|