2011-07-26 55 views
1

我有一个应用程序,必须接受几乎每个文件类型,除了那些已知是很烂(即exe,dll,蝙蝠等)。我正在使用回形针,并想知道是否有办法做到这一点。在github上提交之后,https://github.com/thoughtbot/paperclip/commit/020625921adae884534608d76c11f65692e4bbec看起来好像有可能。但我不确定。有没有办法在Paperclip中黑名单扩展?

更新:我找不到做事情的回形针的方式,但是我没有添加这个自定义的验证:

def extension_not_blacklisted? 
#An attempt to make a blacklist command when saving... 
forbiden_types = Array.new() 
forbiden_types << "jpg" << "exe" <<"dll" 
path_array = attachment.to_s.split(".") 
extension = path_array.pop 
extension_with_extras = extension.to_s.split("?") 
extension = extension_with_extras[0] 

forbiden_types.each do |f| 
    if f == extension 
    errors.add(:attachment,'FORBIDEN FILE EXTENSION: ' + extension) 
    end 
end 

回答

1

你自定义的验证方法可能是唯一的出路。至少现在,Paperclip只能验证内容类型,类似于:

validates_attachment_content_type :attachment, :content_type => ['image/png', 'application/pdf'], :message => 'should be a valid type' 

它验证包含,而不是排除。

0

使用before_post_process过滤器和返回false如果扩展名是在你的黑名单 - 返回假将阻止执行其余的处理链。

有关检查有效文件大小的示例,请参阅此页底部。

https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation

+0

的before_post_process似乎只适用于制作缩略图的形象,因为我返回false,它仍然上传大图像,但不生成缩略图。 –

0

您可以使用使用负向前查找正则表达式:

validates_attachment_content_type :attachment, :content_type => /\/(?!(php|pl|exe|pm|cfm|asp)$)/ 
相关问题