Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails how to delete a file without failing on error

I'm using JPEGCAM to allow users to take a profile pic with their web cam. This uploads a temporary file as so:

def ajax_photo_upload       File.open(upload_path, 'w:ASCII-8BIT') do |f|     f.write request.raw_post   end   # @user.photo = File.open(upload_path)   @user.assign_attributes(     :photo => File.open(upload_path),     :orig_filename => "#{current_user.full_name}.jpg"   )   if @user.save   respond_to do |format|   ..... private    def upload_path # is used in upload and create     file_name = session[:session_id].to_s + '.jpg'     File.join(::Rails.root.to_s, 'public', 'temp', file_name)   end 

What's the best way to go about deleting this temporary file safely? Thanks

like image 317
AnApprentice Avatar asked Oct 09 '12 21:10

AnApprentice


People also ask

How do I delete a rails file?

Rails delete operation using destroy method By using destroy, you can delete the record from rails as well as its other existing dependencies. So in the context of our rails application, if we delete a book record using the destroy function, the authors associated with the book will also be deleted.

How do you delete a file if it exists in Ruby?

Removing a single file in Ruby is simple and straightforward, with File. delete . This method Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error.

How do you delete a row in Ruby on rails?

If you are using Rails 5 and above, the following solution will work. Show activity on this post. Deleting a row from a particular table or a record set from a table is pretty simple, from your console all you need to do is grab the record set by its Id then delete or destroy it.


2 Answers

When you know that you are done with the file:

File.delete(path_to_file) if File.exist?(path_to_file) 

Another thing: make sure that you always close files that you have opened, an operating system can only handle a certain number of open files/file descriptors and you'll may run into strange bugs when you pass that limit... So when you want to open files in Ruby always either use the block form:

File.open(path) do |f|   # ... end 

and Ruby will close the file automatically for you. If the block form is not usable, you have to close files by yourself:

f = File.open(path) # ... f.close 

So make sure to close the file that you pass to @user.assign_attributes(...)...

like image 147
severin Avatar answered Sep 25 '22 13:09

severin


If you are sure you are done with it, why not just use FileUtils.rm or FileUtils.rm_f?

FileUtils.rm_f(upload_path)

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/fileutils/rdoc/FileUtils.html#method-c-rm_f

You could also ignore this in Rails, and have a cron that wakes up and deletes files older than a day from the temp directory that match these temp files. That has the benefit of some margin for error if a file fails to be reprocessed - you don't rm it immediately - and the file operation is not done on the request/response loop for Rails, which will then respond a bit faster.

like image 20
Andrew Kuklewicz Avatar answered Sep 24 '22 13:09

Andrew Kuklewicz