问题
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
回答1:
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
回答2:
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
回答3:
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
回答4:
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
回答5:
It's a little bit hack but has performance advantage
my_uploader.send :store_versions!, open(my_file)
来源:https://stackoverflow.com/questions/8579425/how-can-i-make-carrierwave-not-save-the-original-file-after-versions-are-process