How can I make carrierwave not save the original file after versions are processed?

前端 未结 5 1156
广开言路
广开言路 2020-12-16 04:17

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 pro

相关标签:
5条回答
  • 2020-12-16 04:33

    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
    
    0 讨论(0)
  • 2020-12-16 04:35

    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

    0 讨论(0)
  • 2020-12-16 04:47

    It's a little bit hack but has performance advantage

    my_uploader.send :store_versions!, open(my_file)
    
    0 讨论(0)
  • 2020-12-16 04:48

    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
    
    0 讨论(0)
  • 2020-12-16 04:49
    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
    
    0 讨论(0)
提交回复
热议问题