Does Paperclip automatically clean up filenames?

前提是你 提交于 2020-01-12 13:55:54

问题


I'm using Thoughtbot's Paperclip gem to handle file uploads.

I'm finding that when I upload a file with spaces in the filename, it gets stored with the spaces replaced with underscores.

That's good.

I also tried uploading a file with special characters like ~ and so on and they all got replaced with underscores.

Great. Exactly what I want.

But why is it happening?

All I'm doing in my model is...

has_attached_file(
    file_somefile,
    :path => ":rails_root/public/system/other/path/elements/:basename.:extension"
)

Is this Paperclip's default behavior?


回答1:


To add a little more information, this happens in Paperclip::Attachment#cleanup_filename in which the default restricted_characters /[&$+,/:;=?@<>[]{}\|\\^~%# ]/ are replaced with underscores.

It's not documented, but you can specify the :restricted_characters option to paperclip to change what gets replaced, e.g.

class User < ActiveRecord::Base
  attr_accessible :avatar
  has_attached_file :avatar, :restricted_characters => /@/ # only replaces '@'
end



回答2:


OK, after a little more searching, I found this blog post that says, down at the bottom, that Paperclip actually does some minimal processing of filenames.



来源:https://stackoverflow.com/questions/7328423/does-paperclip-automatically-clean-up-filenames

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!