rake aborted! Operation not permitted - Carrierwave Delete tmp files that failed validation

社会主义新天地 提交于 2019-12-11 07:22:27

问题


I am using Carrierwave, s3 and fog to upload videos. I added in a file_size_validator file so that videos can't be larger than 5mb.

My understanding of how Carrierwave works (please advise if incorrect). Carrierwave saves a copy of the file to the tmp file folder and then performs validations on it so if it passes, the file is uploaded to S3 and the code below removes the tmp file from the local filesystem:

video_uploader.rb

before :store, :remember_cache_id
  after :store, :delete_tmp_dir

  def cache_dir
    Rails.root.join('public/uploads/tmp/videos') 
  end

    # store! nil's the cache_id after it finishes so we need to remember it for deletion
  def remember_cache_id(new_file)
    @cache_id_was = cache_id
  end

  def delete_tmp_dir(new_file)
    # make sure we don't delete other things accidentally by checking the name pattern
    if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
      FileUtils.rm_rf(File.join(root, cache_dir, @cache_id_was))
    end
  end

If the file doesn't pass the validation then the before_store and after_store callbacks don't get called and the tmp file remains in the tmp folder and doesn't get deleted.

So we have to deal with deleting these files ourselves ( again please advise if there is a common way to do this ). I created a cron rake task that deletes the screenshots associated with the videos, which works just fine, and then used a similar format to delete these temporary files and folders. When trying to run the task i get the following errors:

video.rake

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

task :carrierwave_tmp do 
  CarrierWave.clean_cached_files!
end

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

rake delete_unsaved_videos
rake aborted!
Operation not permitted - /user/me/projects/teebox_network/public/uploads/tmp/videos/20130421-1853-8808-1646

running the rake with sudo (just for debugging) throws this.

sudo bundle exec rake delete_unsaved_videos
Could not find rake-10.0.4 in any of the sources
Run `bundle install` to install missing gems.

running bundle install doesn't achieve anything.

Does anyone know why this is happening? is it a permissions error with trying to delete those folders? is there a better way to deal with tmp files that failed validation?


回答1:


Using rm_rf instead of rm overcame the permissions errors and deletes the folders successfully.

FileUtils.rm_rf Dir.glob("#{Rails.root}/public/uploads/tmp/videos/*")



来源:https://stackoverflow.com/questions/16138617/rake-aborted-operation-not-permitted-carrierwave-delete-tmp-files-that-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!