cleanup tmp directory with carrierwave

2019-02-10 22:19发布

问题:

I use carrierwave for my images upload, in my form i added a hidden field for caching like is described in documentation.

= form_for @user, html: {multipart: true} do |f|
%p
  = f.label :image, "your image"
  = f.file_field :image, id: "img"
  = f.hidden_field :image_cache

but the problem is after uploading images and saving record, tmp directory still having all temporary/caching files.

there is a way to clean up the tmp directory ?

i found this post here but can't understand it as well, and there is no simple example explained

Edit

I tried to run this command with console

CarrierWave.clean_cached_files!

it outputs me an array of all files in tmp directory like this:

["/home/medBo/projects/my_project/public/uploads/tmp/1380732930-5006-6671","/hom‌​e/medBo/projects/my_project/public/uploads/tmp/1380754280-4623-3698" ....

but when i go to see what happens, i find that all files still exists in /tmp (not removed)

i tried to read more in the link above, i found a special considerations about CarrierWave.clean_cached_files! :

Special Considerations

This method breaks uploaders that have more than one version defined. Your first version will be saved, but afterwards the cleanup code will run and remove the tmp file that is used to generate additional versions. In that case, you are better off creating a rake task that cleans out the tmp folders periodically.

what means : "This method breaks uploaders that have more than one version" ? (because i use two versions in my uploader class "thumb and large versions") :

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  ...
  ...
  version :large do
    resize_to_limit(600, 600)
  end

  version :thumb do
    process :crop_image
    resize_to_fill(100, 100)
  end
...
...
end

I also try to run a task to see if folders inside tmp/ directory will be removed but the task doesn't work :

task :delete_tmp_files do
   FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*")
end

回答1:

CarrierWave will take care of tidying most of the tmp files and folders for you when everything is working properly. To catch the anomalies create a custom rake task to clean up the garbage and then use the Whenever gem to schedule this task to run every day, every hour etc.

my_custom_task.rake

task :delete_tmp_files do
  FileUtils.rm_rf Dir.glob("#{Rails.root}/where/you/store/your/tmp_images/*") #public/tmp/screenshots etc
  #note the asterisk which deletes folders and files whilst retaining the parent folder
end

call with rake delete_tmp_files

Ryan Bates has done a great railscast on setting up whenever in rails. http://railscasts.com/episodes/164-cron-in-ruby-revised



回答2:

Have you tried calling

CarrierWave.clean_cached_files!

in your code or manually from a rails console? Did it work? If so, you can put it in a daily task. You can use something like the whenever gem. It will look something like:

every 1.day, :at => '4:30 am' do
  runner "CarrierWave.clean_cached_files!"
end


回答3:

Referring to the CarrierWave API Docs:

clean_cached_files!(seconds = 60*60*24)

Object Removes cached files which are older than one day.

If you're doing it on the console, just pass a lower value to the method, e.g.

CarrierWave.clean_cached_files! 1

Read more here: http://www.rubydoc.info/github/jnicklas/carrierwave/CarrierWave/Uploader/Cache/ClassMethods