복붙노트

[RUBY-ON-RAILS] 검증 실패 : 업로드 파일의 내용과 일치하지 않는 확장자가

RUBY-ON-RAILS

검증 실패 : 업로드 파일의 내용과 일치하지 않는 확장자가

나는 파일을 업로드 할 클립 보석을 사용하고 있습니다. 내 클립 보석 버전은 클립-4.1.1입니다. 파일을 그 던지기를 업로드하는 동안

Validation failed: Upload file has an extension that does not match its contents.

나는 XLSX 파일을 업로드하는 것을 시도하고있다. 또한 전 모델 콘텐츠 _에 그 언급했다.

 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. '

이 오류가 발생하는 이유를 모르겠어요. 이 오류에 대한 어떤 생각이 있으면 공유하시기 바랍니다.

쇼 검증 실패 로그에서 발췌 :

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:

해결법

  1. ==============================

    1.파일 명령이 정확하게 파일 형식을 결정할 수 없기 때문에 종이 클립 스푸핑 유효성 검사가 실패하고 있습니다.

    파일 명령이 정확하게 파일 형식을 결정할 수 없기 때문에 종이 클립 스푸핑 유효성 검사가 실패하고 있습니다.

    로그에서 콘텐츠 형식은 파일 명령에서 발견했다. - 즉 빈 - 기간 전에 빈 공간은 출력 결과이다. 그러나, 비교의 타측 순수 올바르게 엑셀 파일로 촬상되는 파일 확장을 사용한다. 따라서 유효성 검사 오류가 발생했습니다.

    종이 클립의 현재 버전은 그러나 --mime 형은 모든 구현에서 지원하지 않는 파일을 결정하기 위해 파일 -b --mime 형을 사용하고 있습니다. 이 대신 --mime를 사용하는 변화이지만 아직 이정표 아니다.

    난 당신이 몇 가지 옵션이 있습니다 생각합니다. 어떤 당신이 몇 가지 사기 파일에 대한 있습니다 우려 업로드되는 및 엑셀 파일을 호출되는 방식에 따라 선택합니다. 당신이 걱정하는 경우 다음 옵션 1을 시도; 당신은 옵션 2 또는 3에 갈 걱정하지 않는 경우.

    1) --mime 형 대신 --mime를 사용하여 스푸핑 검사를 무시.

    이니셜 라이저에서 type_from_file_command를 오버라이드 (override) :

    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
    

    2)의 파일 확장자에서 완전히 파일 형식을 설정하여 파일 검사를 바이 패스.

    응용 프로그램의 초기화시 읽기이 종이 클립 옵션 어딘가에 설정 (예를 들어, 설정 / application.rb, 설정 / 환경 / <환경> .rb 또는 설정 / 초기화 / paperclip.rb) :

    Paperclip.options[:content_type_mappings] = { xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }
    

    3) 사용 안 함은 모두 스푸핑.

    이니셜 라이저에서 이런 식으로 뭔가를 만들어 위장 검사를 오버라이드 (override) :

    module Paperclip
      class MediaTypeSpoofDetector
        def spoofed?
          false
        end
      end
    end
    

    최신 정보:

    당신은 당신의 모델이 검증이 문제의 원인이 아닙니다. 파일 형식을 사용하면 부하에 허용이 유효성을 검사; 당신이보고있는 것은 종이 클립 파일의 종류가 유효하지만 그 내용이 파일의 형식과 일치하지 않는 것이 계산이다.

    당신을 가정하면 작업에 스푸핑 검증을 얻을 수 있습니다, 콘텐츠 유효성 검사 이상 하나있다. 당신 출력 오류 메시지는하지만 실제 검증은 MS 워드와 엑셀 파일이 아닌 XML을 확인하고있다 "단지 XML은, 엑셀 파일을 사용할 수 있습니다"라고.

    귀하의 메시지가 정확하고 당신은 단지 XML을 허용 할 않고 엑셀 당신이 일하기 위해 콘텐츠 _ 검증을 변경해야합니다 파일 경우 :

    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. '
    
  2. ==============================

    2.이 방법을 시도

    이 방법을 시도

    validates_attachment_content_type :upload_file, :content_type => ["application/msword", "application/vnd.ms-office application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"], :message => ', Only XML,EXCEL files are allowed. '
    
  3. from https://stackoverflow.com/questions/23629888/validation-failed-upload-file-has-an-extension-that-does-not-match-its-contents by cc-by-sa and MIT license