gpt4 book ai didi

ruby-on-rails - 如何使用 Rails Paperclip 处理包含非字母数字字符的文件名

转载 作者:可可西里 更新时间:2023-11-01 09:57:20 25 4
gpt4 key购买 nike

我正在使用带有 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:

关于处理这个错误有什么想法/建议吗?

最佳答案

mongoid-paperclip gem 只是将所有给定的选项传递给 paperclip(请参阅 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 )

关于ruby-on-rails - 如何使用 Rails Paperclip 处理包含非字母数字字符的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24150670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com