Paperclip会自动清理文件名吗?

Eth*_*han 12 ruby-on-rails paperclip

我正在使用Thoughtbot的Paperclip gem来处理文件上传.

我发现当我上传文件名中包含空格的文件时,它会被替换为下划线的空格存储.

非常好.

我也尝试上传一个带有特殊字符等文件的文件,~它们都被下划线取代.

大.正是我想要的.

但为什么会这样呢?

我在模特中所做的一切都是......

has_attached_file(
    file_somefile,
    :path => ":rails_root/public/system/other/path/elements/:basename.:extension"
)
Run Code Online (Sandbox Code Playgroud)

这是Paperclip的默认行为吗?

Jar*_*red 13

要添加更多信息,会发生在Paperclip :: Attachment#cleanup_filename中,其中默认的restricted_characters/[&$ +,/ :; =?@ <> [] {}\| \\ ^〜%#] /是用下划线代替.

它没有记录,但您可以指定paperclip的:restricted_characters选项来更改被替换的内容,例如

class User < ActiveRecord::Base
  attr_accessible :avatar
  has_attached_file :avatar, :restricted_characters => /@/ # only replaces '@'
end
Run Code Online (Sandbox Code Playgroud)


Eth*_*han 6

好的,经过一番搜索,我发现这篇博文在底部说,Paperclip实际上做了一些文件名的最小处理.