当我仅为版本指定时,CarrierWave重命名原始版本

时间:2011-12-19 03:16:58

标签: ruby-on-rails ruby-on-rails-3.1 carrierwave

最终目标是能够上传PDF,创建缩略图并将其保存为PNG。我应该提到的一点是,许多PDF都是多页的。我有一个临时的工作,因为CarrierWave目前不喜欢它们。它包含在我自己定义的操作函数中。 目前我所拥有的是工作,除了原始PDF文件的扩展名改为.png,即使我不相信我告诉它。内容仍然是PDF,这是我想要的,但扩展更改,打破了一切。这是我的整个上传者:

class PdfUploader < CarrierWave::Uploader::Base
  def cache_dir
    "#{Rails.root}/tmp/uploads"
  end 
  include CarrierWave::RMagick
  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Create different versions of your uploaded files:

  version :for_crop  do 
    convert('png')
    process :resize_to_limit_png => [700, 700]
  end

  version :thumb do
    convert('png')
    process :crop
    process :resize_to_limit_png => [100, 100]
  end

  version :medium do
    convert('png')
    process :crop
    process :resize_to_limit_png => [300, 300]
  end

  version :large do
    convert('png')
    process :crop
    process :resize_to_limit_png => [700, 700]
  end

   def extension_white_list
     %w(jpg jpeg gif png pdf)
   end

  # Crop Method, grabs the crop ratio and applies the crop
  # Called when creating versions

  def crop
    if model.crop_x.present?
      manipulate!(:format => 'png') do |img|
        x = model.crop_x.to_i
        y = model.crop_y.to_i
        w = model.crop_w.to_i
        h = model.crop_h.to_i
        img.crop!(x, y, w, h)
      end
    end
  end


    def manipulate!(options={})
      cache_stored_file! if !cached?
      image = ::Magick::Image.read(current_path)

      frames = if image.size > 1
        list = ::Magick::ImageList.new
        #image.each do |frame|
         # list << yield( frame )
        #end
        list = image.first 
        list
      else
        frame = image.first
        frame = yield( frame ) if block_given?
        frame
      end

      if options[:format]
        frames.write("#{options[:format]}:#{current_path}")
      else
        frames.write(current_path)
      end
      destroy_image(frames)
    rescue ::Magick::ImageMagickError => e
      raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.rmagick_processing_error", :e => e)
    end

    def resize_to_limit_png(width, height)
      manipulate!(:format => 'png') do |img|
        geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
        new_img = img.change_geometry(geometry) do |new_width, new_height|
          img.resize(new_width, new_height)
        end
        destroy_image(img)
        new_img = yield(new_img) if block_given?
        new_img
      end
     def filename
      (super.chomp(File.extname(super))).to_s + '.png'
     end
    end  
end

所以,我做了一些临时的janky自定义只是为了使这个工作,直到CarrierWave处理多页PDF。但我想把它包括在我搞砸到那里的某个地方。任何帮助将非常感激!谢谢!

0 个答案:

没有答案