我使用的是Rails3和Paperclip + Rails:
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:对处理这个错误有什么想法/建议吗?
发布于 2014-06-15 02:05:47
mongoid的回形针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)
发布于 2014-06-14 13:23:46
看一下你得到的错误,看起来文件名是正在修复。/正在被:取代。
在周围做了一些搜索,看起来最常见的错误原因是回形针无法找到ImageMagick。设置Paperclip.options[:command_path]应该可以解决这个问题。它也可能是由各种gem版本不匹配引起的。
看看rails paperclip and passenger is not recognized by the 'identify' command,有一堆(希望是)有用的东西可以尝试。
发布于 2014-06-19 23:39:27
我不得不做一些类似的事情,并用before_create和before_update处理它,但这是用普通的回形针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
endhttps://stackoverflow.com/questions/24150670
复制相似问题