【问题标题】:How to handle filenames with non-alphanumeric characters with Rails Paperclip如何使用 Rails Paperclip 处理包含非字母数字字符的文件名
【发布时间】:2014-07-31 18:53:45
【问题描述】:

我正在使用带有 Paperclip + Rails 的 Rails 3:

Gemfile
  gem "paperclip"
  gem "mongoid-paperclip", require: 'mongoid_paperclip'

除非用户上传带有非字母数字字符的文件名的照片,否则一切正常:

thing 1/2/3/.PNG

我试过用 before_post_process before_validation 处理这个问题:

  def strip_strange_characters_from_attachments
    # Set the clean Attachment File Title
    self.attachment.instance.meta['file_name'] = "test.png"
  end

但是,Rails 事先出错并且文件没有上传。下面的错误。有什么想法吗?

[2014-06-10 13:54:47] INFO    Command :: identify -format %wx%h '/var/folders/g_/kythn1dx4fbbry5npb4jx1cr0000gn/T/thing 1:2:3:20140610-41978-ksy5e9.PNG[0]'
[2014-06-10 13:54:47] INFO    [paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: /var/folders/g_/kythn1dx4fbbry5npb4jx1cr0000gn/T/thing 1:2:3:20140610-41978-ksy5e9.PNG is not recognized by the 'identify' command.>
[2014-06-10 13:54:47] INFO    Command :: identify -format %wx%h '/var/folders/g_/kythn1dx4fbbry5npb4jx1cr0000gn/T/thing 1:2:3:20140610-41978-ksy5e9.PNG[0]'
[2014-06-10 13:54:47] INFO    [paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: /var/folders/g_/kythn1dx4fbbry5npb4jx1cr0000gn/T/thing 1:2:3:20140610-41978-ksy5e9.PNG is not recognized by the 'identify' command.>
[2014-06-10 13:54:47] INFO    Command :: identify -format %wx%h '/var/folders/g_/kythn1dx4fbbry5npb4jx1cr0000gn/T/thing 1:2:3:20140610-41978-ksy5e9.PNG[0]'
[2014-06-10 13:54:47] INFO    Completed 422 Unprocessable Entity in 120.0ms
[2014-06-10 13:54:48] INFO    Mongo: (1.5333ms) | Query count: 3
[2014-06-10 13:54:48] FATAL   
Mongoid::Errors::Validations - 
Problem:
  Validation of Mongo::Attachment failed.
Summary:
  The following errors were found: Attachment /var/folders/g_/kythn1dx4fbbry5npb4jx1cr0000gn/T/thing 1:2:3:20140610-41978-ksy5e9.PNG is not recognized by the 'identify' command., Attachment /var/folders/g_/kythn1dx4fbbry5npb4jx1cr0000gn/T/thing 1:2:3:20140610-41978-ksy5e9.PNG is not recognized by the 'identify' command.
Resolution:

关于处理此错误的任何想法/建议?

【问题讨论】:

  • 你的回形针是什么版本的?默认情况下,较新的有一个cleanup_filename 方法来处理这个问题。见here
  • 看来 mongo 回形针 gem 不支持 cleanup_filename - github.com/meskyanichi/mongoid-paperclip/…
  • 谢谢,但混淆文件名并不好,因为最终用户不会对下载他们不认识的文件名感到满意。
  • 但是原始文件名存储在数据库中,或者您没有为用户提供数据库中的信息?

标签: ruby-on-rails ruby-on-rails-3 mongodb paperclip


【解决方案1】:

我必须做一些类似的事情并使用 before_create 和 before_update 处理它,但这是使用常规的 Paperclip gem。没有显示 randomize_file_name 方法,但你明白了。

before_create :randomize_attachment_name
before_update :randomize_attachment_name

def randomize_attachment_name
  if document_file_name
    random_name = randomize_file_name(document_file_name)
    self.document.instance_write(:file_name, random_name)
  end
end

【讨论】:

    【解决方案2】:

    为什么不用这样的东西改变路径?

    path: '/:class/:attachment/:id_partition/:style/:id.:extension'
    

    【讨论】:

      【解决方案3】:

      mongoid-paperclip gem 只是将所有给定的选项传递给回形针(请参阅source),因此您需要清理文件名,就像使用普通回形针时一样。

      有两个选项可以做到这一点,这里是默认值:

       :restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, 
       :filename_cleaner => nil,
      

      通常情况下,文件名中的空格非常好,但您可以尝试将其添加到:restricted_characters。受限字符用于初始化PaperClip::FilenameCleaner

      has_mongoid_attached_file :image, restricted_characters: /[\s&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/ 
      

      您可以更明确地指定文件名清理器,如下所示(但不确定这是否相关)。

      has_mongoid_attached_file :image, filename_cleaner: Paperclip::FilenameCleaner.new(/[\s&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/)
      

      这与指定restricted_characters 选项完全相同。但是,您可以使用此选项并将您自己的FilenameClear 版本交给它。它应该有一个call 方法,并将接收文件名作为参数(参见source

      【讨论】:

        【解决方案4】:

        查看您遇到的错误,似乎文件名正在得到修复。 / 正在替换为 :

        进行一些搜索,看起来该错误的最常见原因是 Paperclip 无法找到 ImageMagick。设置 Paperclip.options[:command_path] 应该可以解决这个问题。也可能是各种gem版本不匹配造成的。

        看看rails paperclip and passenger `is not recognized by the 'identify' command`,有很多(希望是)有用的东西可以尝试。

        【讨论】:

          猜你喜欢
          • 2012-07-25
          • 2012-01-05
          • 2016-02-27
          • 1970-01-01
          • 1970-01-01
          • 2016-10-29
          • 1970-01-01
          • 2011-10-13
          • 2017-07-05
          相关资源
          最近更新 更多