How can I make carrierwave not save the original f

2020-02-09 12:00发布

I'm using CarrierWave for my file uploads in Rails 3.1, and I'm looking for a way to save server space. Many of the photos being uploaded are upwards of 20MB, so after processing them down to 1024 x 1024, I would like to remove the original. Is there any easy way to do that in the uploader class?

Thanks, --Mark

5条回答
时光不老,我们不散
2楼-- · 2020-02-09 12:03

You could define an after_save callback in you model and delete the photo..

I dont know your model but something like this may work if you customize it:

class User << ActiveRecord::Base

  after_create :convert_file
  after_create :delete_original_file

  def convert_file
    # do the things you have to do
  end 

  def delete_original_file
    File.delete self.original_file_path if File.exists? self.original_file_path
  end
end
查看更多
一纸荒年 Trace。
3楼-- · 2020-02-09 12:04

I used to have two versions and realized that I did not not need the original

So instead of having

version :thumb do
    process :resize_to_limit => [50, 50]
  end

version :normal do
   process :resize_to_limit => [300,300]
end

I removed :normal and added this

process :resize_to_limit => [300, 300]

Now the original is saved in size I need and I don't have a third unused image on the server

查看更多
趁早两清
4楼-- · 2020-02-09 12:12

everyone! Selected solution does not work for me. My solution:

  after :store, :remove_original_file

  def remove_original_file(p)
    if self.version_name.nil?
      self.file.delete if self.file.exists?
    end
  end
查看更多
再贱就再见
5楼-- · 2020-02-09 12:15
class FileUploader < CarrierWave::Uploader::Base

  after :store, :delete_original_file

  def delete_original_file(new_file)
    File.delete path if version_name.blank?
  end

  include CarrierWave::RMagick

  storage :file
  .
  . # other configurations

end
查看更多
我想做一个坏孩纸
6楼-- · 2020-02-09 12:25

It's a little bit hack but has performance advantage

my_uploader.send :store_versions!, open(my_file)
查看更多
登录 后发表回答