您可以执行以下几项操作,具体取决于您是否使用process
or version
去做这个。
如果它是一个版本,Carrierwave wiki 有一种方法可以做条件版本。https://github.com/jnicklas/rierwave/wiki/How-to%3A-Do-conditional-processing
version :big, :if => :png? do
process ...
end
protected
def png?(new_file)
new_file.content_type.include? 'png'
end
如果您正在使用process
方法,你可能想看看这个:https://gist.github.com/995663.
将这些添加到您的代码中以绕过以下限制process
has
# 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
然后,您可以使用它来调用以前的进程,有选择地针对每种文件类型
PNG = %w(png)
JPG = %w(jpg jpeg)
GIF = %w(gif)
def extension_white_list
PNG + JPG + GIF
end
process_extensions PNG, :resize_to_fit => [1024, 768]
process_extensions JPG, :...
process_extensions GIF, :...