A paperclip compatible carrierwave uploader
This commit is contained in:
parent
e3c457267c
commit
cea9081347
2 changed files with 85 additions and 0 deletions
81
app/uploaders/picture_uploader.rb
Normal file
81
app/uploaders/picture_uploader.rb
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
class PictureUploader < CarrierWave::Uploader::Base
|
||||||
|
include CarrierWave::MiniMagick
|
||||||
|
include CarrierWave::Compatibility::Paperclip
|
||||||
|
|
||||||
|
def paperclip_path
|
||||||
|
"system/#{object_class_name}/#{extra_store_dir}/#{id_partition}/:style/:basename.:extension"
|
||||||
|
end
|
||||||
|
|
||||||
|
def object_class_name
|
||||||
|
model.class.to_s.underscore.pluralize
|
||||||
|
end
|
||||||
|
|
||||||
|
# compatibility with our paperclip storage paths..
|
||||||
|
def extra_store_dir
|
||||||
|
case object_class_name
|
||||||
|
when 'conferences'
|
||||||
|
'logos'
|
||||||
|
when 'lodgings'
|
||||||
|
'photos'
|
||||||
|
when 'sponsors'
|
||||||
|
'logos'
|
||||||
|
when 'venues'
|
||||||
|
'photos'
|
||||||
|
else mounted_as
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the id of the instance in a split path form. e.g. returns
|
||||||
|
# 000/001/234 for an id of 1234. Stolen from paperclip...
|
||||||
|
def id_partition
|
||||||
|
('%09d'.freeze % model.id).scan(/\d{3}/).join('/'.freeze)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Override the directory where uploaded files will be stored.
|
||||||
|
# This is a sensible default for uploaders that are meant to be mounted:
|
||||||
|
def store_dir
|
||||||
|
"system/#{object_class_name}/#{mounted_as}/#{model.id}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Create different versions of your uploaded files:
|
||||||
|
version :large do
|
||||||
|
process resize_to_fit: [300, 300]
|
||||||
|
end
|
||||||
|
|
||||||
|
version :thumb, from_version: :large do
|
||||||
|
process resize_to_fit: [100, 100]
|
||||||
|
end
|
||||||
|
|
||||||
|
version :first, if: :sponsor?
|
||||||
|
version :first do
|
||||||
|
process resize_to_fill: [320, 180]
|
||||||
|
end
|
||||||
|
|
||||||
|
version :second, if: :sponsor?
|
||||||
|
version :second, from_version: :first do
|
||||||
|
process resize_to_fill: [320, 150]
|
||||||
|
end
|
||||||
|
|
||||||
|
version :others, if: :sponsor?
|
||||||
|
version :others, from_version: :second do
|
||||||
|
process resize_to_fill: [320, 120]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Add a white list of extensions which are allowed to be uploaded.
|
||||||
|
# For images you might use something like this:
|
||||||
|
def extension_white_list
|
||||||
|
%w(jpg jpeg gif png)
|
||||||
|
end
|
||||||
|
|
||||||
|
def content_type_whitelist
|
||||||
|
%r{/image\//}
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def sponsor?(_picture)
|
||||||
|
object_class_name == 'sponsors'
|
||||||
|
end
|
||||||
|
end
|
||||||
4
config/initializers/carrierwave.rb
Normal file
4
config/initializers/carrierwave.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
CarrierWave.configure do |config|
|
||||||
|
config.storage = :file
|
||||||
|
config.cache_dir = "#{Rails.root}/tmp/uploads"
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue