Rake interrupted! Operation Not Allowed - Carrierwave Delete tmp files that failed validation

I use Carrierwave, s3 and fog to download the video. I added the file file_size_validator so that the video does not exceed 5 MB.

My understanding of how Carrierwave works (please let me know if this is incorrect). Carrierwave saves a copy of the file to the tmp file folder, and then checks on it, so if it passes, the file is uploaded to S3, and the code below deletes the tmp file from the local file system:

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 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 does not pass the verification, the before_store and after_store calls are not called, and the tmp file remains in the tmp folder and is not deleted.

enter image description here

So we have to deal with deleting these files (again, please let me know if there is a general way to do this). I created a cron rake task that removes screenshots associated with videos that work very well, and then used a similar format to delete these temporary files and folders. When I try to start a 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 rake with sudo (for debugging only) causes 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. 

A running package installation did not achieve anything.

Does anyone know why this is happening? Is it a permission error when trying to delete these folders? Is there a better way to deal with tmp files that fail validation?

+1
source share
1 answer

Using rm_rf instead of rm overcomes permission errors and successfully deletes folders.

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

+2
source

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


All Articles