Ruby-on-rails Rails 如何在不出错的情况下删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12808988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:51:20  来源:igfitidea点击:

Rails how to delete a file without failing on error

ruby-on-railsrubyruby-on-rails-3

提问by AnApprentice

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

我正在使用 JPEGCAM 来允许用户使用他们的网络摄像头拍摄个人资料照片。这会上传一个临时文件,如下所示:

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

安全删除此临时文件的最佳方法是什么?谢谢

回答by severin

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:

另一件事:确保你总是关闭你打开的文件,操作系统只能处理一定数量的打开文件/文件描述符,当你超过这个限制时,你可能会遇到奇怪的错误......所以当你想要在 Ruby 中打开文件总是使用块形式:

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:

Ruby 会自动为你关闭文件。如果块表单不可用,则必须自己关闭文件:

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

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

所以一定要关闭你传递给的文件@user.assign_attributes(...)......

回答by Andrew Kuklewicz

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

如果你确定你已经完成了,为什么不直接使用FileUtils.rmorFileUtils.rm_f呢?

FileUtils.rm_f(upload_path)

FileUtils.rm_f(upload_path)

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

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.

您也可以在 Rails 中忽略这一点,并使用一个 cron 唤醒并从与这些临时文件匹配的临时目录中删除超过一天的文件。如果文件无法重新处理 - 您不会立即 rm 它 - 并且文件操作不会在 Rails 的请求/响应循环上完成,那么它的好处是有一定的错误余量,然后会更快地响应。