验证失败:上传文件的扩展名与其内容不匹配

Can*_*Can 12 ruby validation ruby-on-rails paperclip

我正在使用paperclip gem上传文件.我的paperclip gem版本是paperclip-4.1.1.在上传文件时抛出

Validation failed: Upload file has an extension that does not match its contents.
Run Code Online (Sandbox Code Playgroud)

我正在尝试上传xlsx文件.而且我已经在模型中提到了content_type.

 validates_attachment_content_type :upload_file, :content_type => %w(application/msword application/vnd.ms-office application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet),
                                               :message => ', Only XML,EXCEL files are allowed. '
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会发生这种错误.如果您对此错误有任何疑问,请分享.

摘自日志以显示验证失败:

Command :: file -b --mime-type '/tmp/5249540099071db4e41e119388e9dd6220140513-24023-1jlg4zy' [paperclip] Content Type Spoof: Filename file_for_bulk_upload1.xlsx (["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]), content type discovered from file command: . See documentation to allow this combination. 
Command :: file -b --mime-type '/tmp/6f19a4f96154ef7ce65db1d585abdb2820140513-24023-tt4u1e' [paperclip] Content Type Spoof: Filename file_for_bulk_upload1.xlsx (["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]), content type discovered from file command:
Run Code Online (Sandbox Code Playgroud)

小智 16

Paperclip欺骗验证检查失败,因为该file命令无法准确确定文件类型.

在您的日志中content type discovered from file command: .- 期间之前的空白是输出的结果 - 即空白.然而,比较的另一面纯粹使用文件扩展名,它正确地被选为excel文件.因此您的验证失败.

Paperclip的当前版本file -b --mime-type用于确定文件,但--mime-type并非所有实现都支持.有一个改变使用,--mime但它还没有在一个里程碑.

我想你有一些选择.你选择哪个取决于你对上传的一些狡猾的文件和被称为excel文件的关注程度.如果您对此感到担心,请尝试选项1; 如果你不担心选择2或3选项.

1)覆盖欺骗检查以--mime代替使用--mime-type.

覆盖type_from_file_command初始化程序:

module Paperclip
  class MediaTypeSpoofDetector
    private

    def type_from_file_command
      # -- original code removed --
      # begin
      #   Paperclip.run("file", "-b --mime-type :file", :file => @file.path)
      # rescue Cocaine::CommandLineError
      #   ""
      # end

      # -- new code follows --
      begin
         Paperclip.run("file", "-b --mime :file", :file => @file.path)
      rescue Cocaine::CommandLineError
        ""
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

2)file通过从文件扩展名完全设置文件类型来绕过检查.

将此Paperclip选项设置为在应用程序初始化期间读取的某个位置(例如config/application.rb,config/environments/<environment>.rbconfig/initializers/paperclip.rb):

Paperclip.options[:content_type_mappings] = { xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }
Run Code Online (Sandbox Code Playgroud)

3)完全禁用欺骗.

通过在初始化程序中创建类似的内容来覆盖欺骗检查:

module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

更新:

您在模型中进行的验证不是导致此问题的原因.这将验证您可以加载哪些类型的文件; 你看到的是Paperclip计算文件的类型是有效但其内容与文件的类型不匹配.

假设您可以使用欺骗验证,您的内容验证会出现一个异常现象.您输出的错误消息显示"只允许XML,EXCEL文件",但是您的实际验证是检查MS word和excel文件,而不是xml.

如果您的消息正确并且您确实只想允许xml和excel文件,则应将content_type验证更改为:

validates_attachment_content_type :upload_file, :content_type => %w(application/xml application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet),
                                                :message => ', Only XML,EXCEL files are allowed. '
Run Code Online (Sandbox Code Playgroud)