Clear carrier tmp directory

I use a carrier to upload images, in my form I added a hidden field for caching, as described in the 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 loading the images and saving the records, the tmp directory still has all the temporary / caching files.

is there a way to clear the tmp directory?

I found this post here , but I canโ€™t understand it, and there is no simple explained example.

Edit

I tried to run this command using the console

 CarrierWave.clean_cached_files! 

it displays me an array of all the files in the tmp directory as follows:

 ["/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 found that all the files still exist in / tmp (not deleted)

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

Special considerations

This method violates bootloaders that have more than one version. Your first version will be saved, but after that the cleanup code will run and delete the tmp file, which is used to create additional versions. In this case, you better create a rake task that periodically cleans the tmp folders.

What does it mean: "This method violates bootloaders with multiple versions"? (because I use two versions in my loader class of "large 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 the task to see if the folders inside tmp / directory will be deleted, but the task does not work:

 task :delete_tmp_files do FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/*") end 
+4
source share
3 answers

CarrierWave will take care of removing most of the tmp files and folders for you when everything is working correctly. To catch anomalies, create a custom rake task to clear the garbage, and then use the โ€œWhenever gemโ€ to schedule this task to be 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 made an excellent railscast when tuning every time on rails. http://railscasts.com/episodes/164-cron-in-ruby-revised

+4
source

Did you try to call

 CarrierWave.clean_cached_files! 

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

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

Referring to CarrierWave API Docs:

clean_cached_files! (seconds = 60 * 60 * 24)

Object Deletes cached files that are older than one day.

If you do this on the console, just pass a lower value to the method, for example.

 CarrierWave.clean_cached_files! 1 

More details here: http://www.rubydoc.info/github/jnicklas/carrierwave/CarrierWave/Uploader/Cache/ClassMethods

0
source

Source: https://habr.com/ru/post/1495101/


All Articles