Paperclip does not support .doc file

In rails 4.0.2, I use paperclip gem to upload files. But it does not support the .doc file. An error message is displayed below the file upload field because "it has an extension that does not match its contents"

In the model, validation for checking the type of content is shown below:

validates_attachment_content_type :document, :content_type => ['application/txt', 'text/plain', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.oasis.opendocument.text', 'application/x-vnd.oasis.opendocument.text', 'application/rtf', 'application/x-rtf', 'text/rtf', 'text/richtext', 'application/doc', 'application/docx', 'application/x-soffice', 'application/octet-stream'] 

Gemstones that are in use right now

 rails (4.0.2, 4.0.0, 3.2.13, 3.2.8, 3.0.4, 3.0.3) paperclip (3.5.2, 2.3.11, 2.3.8) 

How can I solve this problem?

+3
ruby file validation ruby-on-rails paperclip
source share
5 answers

add this to the initializer to disable spoofing protection:

 require 'paperclip/media_type_spoof_detector' module Paperclip class MediaTypeSpoofDetector def spoofed? false end end end 

For centOS

  module Paperclip class MediaTypeSpoofDetector def type_from_file_command begin Paperclip.run("file", "-b --mime :file", :file => @file.path) rescue Cocaine::CommandLineError "" end end end end 

from https://github.com/thoughtbot/paperclip/issues/1429

+7
source share

It’s a good idea to skip spoofing checks. Because Paperclip adds it for security reasons. See this article for more details: http://robots.thoughtbot.com/prevent-spoofing-with-paperclip

Password check checks if the file extension matches the mime type. For example, the mime type of the txt file is text/plain , when you load it into the Paperclip folder, everything goes well. But if you change the extension to jpg , then download it, the check will not be performed because the jpg mime file type must be image/jpeg .

Please note that this check is performed to check the security, so there is no usual way to skip it. Even when you use do_not_validate_attachment_file_type , it is not skipped. But for some files, Paperclip cannot correctly recognize the display of files -> mime.

In this case, the correct way is to add a content type mapping to the binding configuration. Like this:

 # Add it to initializer Paperclip.options[:content_type_mappings] = { pem: 'text/plain' } 

Thus, it works without violating the substitution check. If you do not know what the mime type is, you can use the file command:

 file -b --mime-type some_file.pdf # -> application/pdf 
+4
source share

You can enable all types of content with do_not_validate_attachment_file_type :file

You can enable spoofing with has_attached_file :file,

 class Upload #validate_media_type == false means "authorize spoofing" has_attached_file :file, validate_media_type: false #authorize all content types do_not_validate_attachment_file_type :file_if_content_type_missing end 
+1
source share

An error in the server log means that your OS file command cannot get the MIME type for the .doc file. This happens to me with ubuntu 12.04.

To get around this, I slightly modified the MediaTypeSpoofDetector to use mimetype if file --mime did not work.

 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 -- file_type = '' begin file_type = Paperclip.run('file', '-b --mime-type :file', file: @file.path) rescue Cocaine::CommandLineError file_type = '' end if file_type == '' begin file_type = Paperclip.run('mimetype', '-b :file', file: @file.path) rescue Cocaine::CommandLineError file_type = '' end end file_type end end end 
0
source share

Try setting do_not_validate_attachment_file_type :document validation in the model.

0
source share

All Articles