Empty folders when deleting a file using CarrierWave

When I delete the downloaded file using CarrierWave and Mongoid, it deletes the file but leaves the emty folders.

/files/:user_id/images/:file_id/ ['image.png', 'content_image.png', 'thumb_image.png'] 

I want the :file_id folder to also be deleted when the file is deleted. Is there any way to do this?

+4
source share
3 answers

I solved this with this:

  before_destroy :remember_id after_destroy :remove_id_directory protected def remember_id @id = id end def remove_id_directory FileUtils.remove_dir("#{Rails.root}/path/to/folder/#{@id}", :force => true) end 
+4
source

You can run the daily cron job to destroy directories:

 cd /your/uploads/dir && find . -type d -empty -exec rmdir {} \; 
+4
source

You can also do the following:

 after_destroy :remove_file_directory def remove_file_directory path = File.expand_path(mount.store_path, mount.root) FileUtils.remove_dir(path, force: false) end 

Where mount is what you installed_s For example: if you have mount_uploader :avatar, AvatarUploader , then

  def remove_file_directory path = File.expand_path(avatar.store_path, avatar.root) FileUtils.remove_dir(path, force: false) end 
+4
source

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


All Articles