2016-10-27 3 views
0

확장명이 .txt 인 첨부 파일을 업로드해야하지만 file 명령으로 mime-type "application/octet-stream"으로 평가해야합니다. 파일은 장비에 의해 자동으로 생성되며 업로드하기 전에 파일 이름을 바꾸는 것은 불가능합니다.paperclip 5.1 내용 유형 유효성 검사가 너무 엄격함

class Book < ActiveRecord::Base 
    has_attached_file :excerpt 
    validates_attachment_content_type :excerpt, content_type: { content_typ: ["text/plain", "application/octet-stream"]} 
    validates_attachment_file_name :excerpt, matches: [/txt\z/] 
end 

하지만 난 항상 감지 된 콘텐츠 형식이 유추 콘텐츠 유형과 일치하지 않는 오류를 얻을 : 나는 시도 오류 메시지가 방법을 문서에보고 말한다

Command :: file -b --mime '/tmp/313a40bb0448477e051da1e2cba2c20120161027-19345-lrhf6t.txt' 
[paperclip] Content Type Spoof: Filename Sample.txt (text/plain from Headers, ["text/plain"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination. 

조합을 허용하지만 해결 방법처럼 보이는 것을 찾지 못했습니다. 이 discussion을 보았지만 v4를 사용했습니다.

답변

2

철자가 잘못 되었기 때문에 content_type 키입니까? (당신은 content_typ로 입력해야합니다.)

처음 제안이 작동하지 않는 경우, 당신의 경우에, 당신은 (README 파일의 Security Validations 섹션의 지침에 따라) config/initializers/paperclip.rb에서이 작업을 수행 할 거라고 생각 :

Paperclip.options[:content_type_mappings] = { 
    txt: %w(text/plain application/octet-stream) 
} 
1

포인터 크리스에게 감사드립니다. README 파일의 해당 섹션을 신중하게 읽지는 않았을 것입니다. (BTW, 오타를 수정하는 것은 어떤 차이를하지 않았다.)

그래서,이 솔루션은 다음과 같이

config/initializers/paperclip.rb에서 :

Paperclip.options[:content_type_mappings] = { 
    txt: %w(application/octet-stream) 
} 

모델에서 :

class Book < ActiveRecord::Base 
    has_attached_file :excerpt 
    validates_attachment_file_name :excerpt, matches: [/txt\z/] 
end 

이것은 실제 .txt 파일이 'text/plain'또는 'application/octet-stream'인지 여부에 관계없이 작동합니다.