Search code examples
ruby-on-railspaperclipspoofing

How do I upload a csv file on Windows using Paperclip paperclip 4.3.2


There is this well known issue with Paperclip.

https://github.com/thoughtbot/paperclip/issues/1924

How do I configure my model so that this stupid spoofing validation will work?

Before the problem was discovered I was using:

validates_attachment_content_type :csv_import, :content_type => 'text/csv'

But that would not work on some versions of Windows. On Windows 7 Professional I get this error:

[paperclip] Content Type Spoof: Filename delivery_detail.csv (application/octet-stream from Headers, [#<MIME::Type:0x00000005077f38 @content_type="text/csv", @raw_media_type="text", @raw_sub_type="csv", @simplified="text/csv", @media_type="text", @sub_type="csv", @extensions=["csv"], @encoding="8bit", @system=nil, @registered=true, @url=["IANA", "RFC4180"], @obsolete=nil, @docs=nil>, #<MIME::Type:0x000000050c7f60 @content_type="text/comma-separated-values", @raw_media_type="text", @raw_sub_type="comma-separated-values", @simplified="text/comma-separated-values", @media_type="text", @sub_type="comma-separated-values", @extensions=["csv"], @encoding="8bit", @system=nil, @registered=false, @url=nil, @obsolete="!", @docs="use-instead:text/csv", @use_instead=["text/csv"]>] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination.

Has anyone ever succeded in making paperclip upload csv files?

I tried every possible workaround from Github issue reports and nothing has worked. I need to see working example solution.

update 1

sonianand11 commented on 2 Oct 2014

https://github.com/thoughtbot/paperclip/issues/1470

This works, but it involves switching off content validation, Is there a better way to do it?.


Solution

  • I came up with the following solution:

    add to the model:

    validates_attachment_content_type :my_csv_uploaded_file, content_type: ['text/plain', 'text/csv', 'application/vnd.ms-excel']
    

    and to the initializer:

    Paperclip.options[:content_type_mappings] = { csv: 'application/vnd.ms-excel' }
    

    It worked for me. It was tested using Windows 7 Professional