Skip to content

Instantly share code, notes, and snippets.

@ahamid
Created May 27, 2011 16:51
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ahamid/995663 to your computer and use it in GitHub Desktop.
Save ahamid/995663 to your computer and use it in GitHub Desktop.
CarrierWave mixed content upload processing
require 'carrierwave/processing/mini_magick'
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
IMAGE_EXTENSIONS = %w(jpg jpeg gif png)
DOCUMENT_EXTENSIONS = %w(exe pdf doc docm xls)
def store_dir
"files/#{model.id}"
end
def cache_dir
"#{::Rails.root.to_s}/tmp/uploads"
end
def extension_white_list
IMAGE_EXTENSIONS + DOCUMENT_EXTENSIONS
end
def default_url
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
# create a new "process_extensions" method. It is like "process", except
# it takes an array of extensions as the first parameter, and registers
# a trampoline method which checks the extension before invocation
def self.process_extensions(*args)
extensions = args.shift
args.each do |arg|
if arg.is_a?(Hash)
arg.each do |method, args|
processors.push([:process_trampoline, [extensions, method, args]])
end
else
processors.push([:process_trampoline, [extensions, arg, []]])
end
end
end
# our trampoline method which only performs processing if the extension matches
def process_trampoline(extensions, method, args)
extension = File.extname(original_filename).downcase
extension = extension[1..-1] if extension[0,1] == '.'
self.send(method, *args) if extensions.include?(extension)
end
# version actually defines a class method with the given block
# therefore this code does not run in the context of an object instance
# and we cannot access uploader instance fields from this block
version :top do
process_extensions ImageUploader::IMAGE_EXTENSIONS, :resize_to_fit => [910,1800]
end
version :logo do
process_extensions ImageUploader::IMAGE_EXTENSIONS, :resize_to_fit => [210,100]
end
version :gallery do
process_extensions ImageUploader::IMAGE_EXTENSIONS, :resize_to_fill => [152,114]
end
version :side do
process_extensions ImageUploader::IMAGE_EXTENSIONS, :resize_to_fit => [455,1800]
end
version :preview do
process_extensions ImageUploader::IMAGE_EXTENSIONS, :resize_to_fit => [120, 1800]
end
version :thumb do
process_extensions ImageUploader::IMAGE_EXTENSIONS, :resize_to_fill => [35, 35]
end
end
@dimitrismistriotis
Copy link

A fast comment if allowed.
Just used this GIST and saved tons of time, one question that arose from the integration process:
I believe that we can have

IMAGE_EXTENSIONS

instead of

ImageUploader::IMAGE_EXTENSIONS

So that it can be easier for people copying and pasting snippets into their existing uploaders.

Thanks for the GIST.
Dimitry.

@jepzen
Copy link

jepzen commented Jul 21, 2014

I tried using this. Added the txt extension to the DOCUMENT_EXTENSIONS. This will still create a copy of the file uploaded named "thumb_uploadme.txt" It has not been resized(which is good since it ain't an image file) but seems not so clever to have an exact copy of the uploaded file. Or did I miss out something?

@crivotz
Copy link

crivotz commented Jun 11, 2015

Thanks mate ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment