Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

CarrierWave mixed content upload processing

View ImageUploader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
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

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 commented

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?

Thanks mate ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.