开发者

Rails 3 + paperclip + windows + file name encoding problem

开发者 https://www.devze.com 2023-03-11 01:17 出处:网络
I use rails 3 and paperclip plugin to attach files. I\'ve got two models: Order; OrderAttachment: belongs_to :order;

I use rails 3 and paperclip plugin to attach files.

I've got two models:

  • Order;
  • OrderAttachment:
    • belongs_to :order;
    • has_attached_file :doc;

And I try to attach files to order via paperclip;

When I’m attaching files with English or numeric file name everything works great:

  SQL (0.0ms)  INSERT INTO "order_attachments" ("order_id", "created_at", "updated_at",  "doc_file_name", "doc_content_type", "doc_file_size", "doc_updated_at") VALUES (1, '2011-06-08 11:07:22.108523', '2011-06-08 11:07:22.108523', 'Example.txt', 'text/plain', 22, '2011-06-08 11:07:22.105523') RETURNING "id"
[paperclip] Saving attachments.
[paperclip] saving D:/my_project_path/public/system/docs/93/original/Example.txt
  SQL (1.0ms)  COMMIT
Completed 200 OK in 229ms (Views: 21.0ms | ActiveRecord: 7开发者_如何转开发.0ms)

But when I’m attaching files with Russian file name error occurs:

SQL (1.0ms)  INSERT INTO "order_attachments" ("order_id", "created_at", "updated_at", "doc_file_name", "doc_content_type", "doc_file_size", "doc_updated_at") VALUES (1, '2011-06-08 11:26:43.040925', '2011-06-08 11:26:43.040925', 'Пример.txt', 'text/plain', 26, '2011-06-08 11:26:43.035924') RETURNING "id"
[paperclip] Saving attachments.
[paperclip] saving D:/my_project_path/public/system/docs/94/original/Пример.txt
  SQL (0.0ms)  ROLLBACK
Completed   in 161ms

Errno::ENOENT (No such file or directory - D:/my_project_path/public/system/docs/94/original/╨а╤Я╨б╨В╨а╤С╨а╤Ш╨а┬╡╨б╨В.txt):
  app/controllers/orders_controller.rb:138:in `attachment'

Paperclip saves file to D:/my_project_path/public/system/docs/94/original/Пример.txt (i can open it via explorer) but no record in database created. Maybe something wrong with encoding.

I use:

  • Windows 7 professional x64;
  • PostgreSQL 9.0 (UTF-8 database encoding);
  • Ruby 1.9.2;
  • Ruby on Rails 3.0.7;
  • Paperclip 2.3.11;

Thanks for any help.


As i said paperclip saves attached file and rails creates valid request, but something inside paperclip source goes frong under Windows.

Errno::ENOENT exception was caught in C:\Ruby192\lib\ruby\gems\1.9.1\gems\paperclip-2.3.11\lib\paperclip\storage\filesystem.rb source file in line 42

def flush_writes #:nodoc:
  @queued_for_write.each do |style_name, file|
    file.close
    FileUtils.mkdir_p(File.dirname(path(style_name)))
    log("saving #{path(style_name)}")
    FileUtils.mv(file.path, path(style_name))
    FileUtils.chmod(0644, path(style_name))
  end
  @queued_for_write = {}
end

I changed this function to:

def flush_writes #:nodoc:
  @queued_for_write.each do |style_name, file|
    file.close
    FileUtils.mkdir_p(File.dirname(path(style_name)))
    log("saving #{path(style_name)}")
    FileUtils.mv(file.path, path(style_name))
    begin
      FileUtils.chmod(0644, path(style_name))
    rescue Errno::ENOENT
      log("Errno::ENOENT caught on #{ENV['OS']}")
    end
  end
  @queued_for_write = {}
end

This solution works for me. Now sever log output is:

  SQL (1.0ms)  INSERT INTO "order_attachments" ("order_id", "created_at", "updated_at", "doc_file_name", "doc_content_type", "doc_file_size", "doc_updated_at") VALUES (14, '2011-06-08 18:44:25.853559', '2011-06-08 18:44:25.853559', 'Пример.doc', 'application/msword', 292352, '2011-06-08 18:44:25.727552') RETURNING "id"
[paperclip] Saving attachments.
[paperclip] saving D:/my_project_path/public/system/docs/199/original/Пример.doc
[paperclip] Errno::ENOENT caught on Windows_NT    --> !!! here it is - our exception !!!
  SQL (3.0ms)  COMMIT
Completed 200 OK in 287ms (Views: 13.0ms | ActiveRecord: 15.0ms)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号