Created
May 6, 2015 11:59
-
-
Save tofuseng/ce8cc43ba5cdac7741b2 to your computer and use it in GitHub Desktop.
create an initializer to override Paperclip's classes file_command_content_type_detector.rb and media_type_spoof_detector.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Paperclip | |
class FileCommandContentTypeDetector | |
SENSIBLE_DEFAULT = "application/octet-stream" | |
def initialize(filename) | |
@filename = filename | |
end | |
def detect | |
type_from_file_command | |
end | |
private | |
def type_from_file_command | |
type = begin | |
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist. | |
Paperclip.log("Check mime_type of #{@filename}") | |
MIME::Types.type_for(@filename).first.nil? ? nil : MIME::Types.type_for(@filename).first.content_type | |
rescue nil | |
end | |
if type.nil? || type.match(/\(.*?\)/) | |
type = SENSIBLE_DEFAULT | |
end | |
type.split(/[:;\s]+/)[0] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment