【发布时间】: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