Last active
December 16, 2015 16:40
-
-
Save carlosipe/5465086 to your computer and use it in GitHub Desktop.
CarrierWave ImageUploader, thumb resizing on the fly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class ImageUploader < CarrierWave::Uploader::Base | |
include CarrierWave::MiniMagick | |
storage :file | |
# storage :fog | |
# Process files as they are uploaded (if width >1024): | |
process :resize_to_limit =>[1024, 10000] | |
def thumb(size) | |
return default_url if url == default_url | |
uploader = Class.new(self.class) | |
uploader.version_names = [size] | |
img = uploader.new(model, mounted_as) | |
img.retrieve_from_store!(identifier) | |
cached = File.join(CarrierWave.root, img.url) | |
unless File.exist?(cached) | |
img.cache!(self) | |
size = size.split('x').map(&:to_i) | |
resizer = case size | |
when /[!#]/ then :resize_to_fit | |
# add more like when />/ then ... | |
else :resize_to_fill | |
end | |
img.send(resizer, *size) | |
img.store! | |
#puts 'RESIZING' | |
end | |
img | |
end | |
def store_dir | |
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" | |
end | |
def default_url | |
"/assets/defaults/#{model.class.to_s.underscore}.png" #[version_name, "esparrago.jpg"].compact.join('_') | |
end | |
# def extension_white_list | |
# %w(jpg jpeg gif png) | |
# end | |
end |
Another idea: resizing by url (example, to offer an api or to work easily on javascript)
ResizerController
def index
model, id, mounted_as, operation, args = params[:url].split '/'
Class.new(model.constantize).find(id).send(mounted_as, *operation, args) || raise 404
end
//If the file already exists, nginx won't call Rails, if find fails, ActiveRecord raises an exception that produces 404, if the result of the operation is nil, I raise a 404, if it's not nil is an image_url
Controller should validate acceptable operations and args (to stop DDOS), it can also require a token.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Btw, I should serve the files over a CDN, or at least a cookieless domain