Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty folders when file is deleted using CarrierWave

When I delete an uploaded file using CarrierWave and Mongoid it remove the file but leave 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 removed when the file is deleted. Is there a way to do this?

like image 573
sandelius Avatar asked Dec 17 '22 07:12

sandelius


1 Answers

I've 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
like image 75
dft Avatar answered Feb 23 '23 01:02

dft