Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cleanup tmp directory with carrierwave

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
like image 300
medBouzid Avatar asked Oct 01 '13 21:10

medBouzid


2 Answers

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
like image 193
jcm Avatar answered Sep 29 '22 06:09

jcm


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

like image 37
dodgerogers747 Avatar answered Sep 29 '22 06:09

dodgerogers747