2010-05-12 00:16:39 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
class AssetUploader < CarrierWave::Uploader::Base
|
|
|
|
|
|
|
|
include CarrierWave::RMagick
|
|
|
|
|
|
|
|
def store_dir
|
|
|
|
"sites/#{model.collection.site_id}/assets/#{model.id}"
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-12 21:47:14 +00:00
|
|
|
def cache_dir
|
|
|
|
"#{Rails.root}/tmp/uploads"
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
version :thumb do
|
|
|
|
process :resize_to_fill => [50, 50]
|
|
|
|
process :convert => 'png'
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
version :medium do
|
|
|
|
process :resize_to_fill => [80, 80]
|
|
|
|
process :convert => 'png'
|
|
|
|
end
|
|
|
|
|
|
|
|
version :preview do
|
|
|
|
process :resize_to_fit => [880, 1100]
|
2010-07-23 20:09:54 +00:00
|
|
|
process :convert => 'png'
|
2010-05-12 00:16:39 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-10-10 23:16:43 +00:00
|
|
|
process :set_content_type
|
|
|
|
process :set_size
|
2010-05-12 00:16:39 +00:00
|
|
|
process :set_width_and_height
|
|
|
|
|
2010-10-10 14:37:34 +00:00
|
|
|
def set_content_type(*args)
|
2010-05-12 00:16:39 +00:00
|
|
|
value = :other
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-02 14:31:01 +00:00
|
|
|
content_type = file.content_type == 'application/octet-stream' ? File.mime_type?(original_filename) : file.content_type
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
self.class.content_types.each_pair do |type, rules|
|
|
|
|
rules.each do |rule|
|
|
|
|
case rule
|
2010-06-08 00:45:49 +00:00
|
|
|
when String then value = type if content_type == rule
|
|
|
|
when Regexp then value = type if (content_type =~ rule) == 0
|
2010-05-12 00:16:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
model.content_type = value
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-10-10 14:37:34 +00:00
|
|
|
def set_size(*args)
|
2010-05-12 00:16:39 +00:00
|
|
|
model.size = file.size
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
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
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
def self.content_types
|
|
|
|
{
|
2011-01-03 10:50:09 +00:00
|
|
|
:image => ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg', 'image/x-icon'],
|
2010-10-07 00:45:41 +00:00
|
|
|
:video => [/^video/, 'application/x-shockwave-flash', 'application/x-swf'],
|
2010-05-12 00:16:39 +00:00
|
|
|
:audio => [/^audio/, 'application/ogg', 'application/x-mp3'],
|
2010-06-02 14:31:01 +00:00
|
|
|
:pdf => ['application/pdf', 'application/x-pdf'],
|
|
|
|
:stylesheet => ['text/css'],
|
2010-07-17 20:51:52 +00:00
|
|
|
:javascript => ['text/javascript', 'text/js', 'application/x-javascript', 'application/javascript'],
|
2010-10-07 00:45:41 +00:00
|
|
|
:font => ['application/x-font-ttf', 'application/vnd.ms-fontobject', 'image/svg+xml', 'application/x-woff']
|
2010-07-23 20:09:54 +00:00
|
|
|
}
|
2010-05-12 00:16:39 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-12 00:16:39 +00:00
|
|
|
end
|