Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tofuseng/ce8cc43ba5cdac7741b2 to your computer and use it in GitHub Desktop.
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
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