简体   繁体   English

通过Paperclip保存文件时经常“没有这样的文件或目录”

[英]Frequent “No such file or directory” while saving files via Paperclip

I feel like this will end in a facepalm moment but I've been banging on my head on it for too long. 我觉得这将以一个facepalm时刻结束,但我已经敲了很长时间。

I have a Rails seed.rb file that gets all the files from a specific directory creating a new object for each file and saving the file via Paperclip: 我有一个Rails seed.rb文件,它从特定目录获取所有文件,为每个文件创建一个新对象,并通过Paperclip保存文件:

Dir["./**/*.jpg"].each do |f|
  ...
  p = Picture.new
  File.open(f, 'r') { |photo_file| p.photo = photo_file }
  p.save!
  ....
end

where photo is the Paperclip-assigned attribute (picture.rb): 其中photo是Paperclip指定的属性(picture.rb):

has_attached_file :photo,
                  :styles => { :medium => "500x500>", :thumb => "100x100#" },
                  :processors => [:rotator]

My problem is after some number of files (some times 50, some times 2) the script exits with the following error: 我的问题是在经过一些文件(有时50次,有时是2次)后脚本退出时出现以下错误:

No such file or directory - /var/folders/oD/oDq1WD11EEaXmfi8VfNvfE+++TM/-Tmp-/stream,22423,0,22423,0
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1407:in `stat'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1407:in `block in fu_each_src_dest'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1423:in `fu_each_src_dest0'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1405:in `fu_each_src_dest'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:504:in `mv'
/Users/patgeorge/.rvm/gems/ruby-1.9.2-head@rails3/bundler/gems/paperclip-61f74de14812cabc026967a2b2c3ca8cbd2eed69-master/lib/paperclip/storage.rb:42:in `block in flush_writes'

I thought maybe I wasn't closing the file but according to the Ruby IO docs using the block from of open will close the file. 我想也许我没有关闭文件但是根据Ruby IO文档使用open的块将关闭文件。

Obviously I don't see myself having to run this often so it's not a huge problem. 显然我不认为自己经常这样做,所以这不是一个大问题。 It's just frustrating and confusing. 这令人沮丧和困惑。

I'm running Ruby 1.9.2 r28142, Rails 3.0.0.beta4, and Paperclip 2.3.3. 我正在运行Ruby 1.9.2 r28142,Rails 3.0.0.beta4和Paperclip 2.3.3。

Additional: 额外:

Attempting Winfield's suggestion my code block now looks like this: 尝试Winfield的建议我的代码块现在看起来像这样:

Dir["./**/*.jpg"].each do |f|
  ...
  File.open(f, 'r') do |photo_file|
    p = Picture.new
    p.photo = photo_file
    p.save!
  end
  ...
end

Still getting the error periodically, though. 但是,仍然会定期得到错误。

Still more info : 更多信息

I noticed that when I first run my script it's able to do a large amount of files (12 or so). 我注意到,当我第一次运行脚本时,它可以执行大量文件(大约12个)。 As I continue to run it the number decreases to where I can only do 2 at a time. 当我继续运行时,数字减少到我一次只能做2个。 I'm not sure what I'm doing to make it "reset" and process more. 我不知道我在做什么让它“重置”并处理更多。 But I imagine that's the key. 但我认为这是关键。

It sure looks to me like you're closing the File handle you opened before you read it out into paperclip. 在您将其读入回形针之前,我肯定会关闭您关闭的文件句柄。

File.open() with a block opens the file, passes it to the block, and closes it after block execution. 带有块的File.open()打开文件,将其传递给块,并在块执行后关闭它。 This means it's likely closed before you call p.save! 这意味着它可能会在你调用p.save之前关闭!

Try doing all of your Photo creation inside your file block: 尝试在文件块中创建所有照片:

File.open(f, 'r') {|photo_file| Picture.create!(:photo => photo_file) }

This monkey patch solved the issue: 这个猴子补丁解决了这个问题:

http://github.com/thoughtbot/paperclip/issues/issue/262/ http://github.com/thoughtbot/paperclip/issues/issue/262/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM