2020-06-30 21:58:02 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Paperclip
|
|
|
|
module MediaTypeSpoofDetectorExtensions
|
2020-07-07 14:32:14 +00:00
|
|
|
def mapping_override_mismatch?
|
|
|
|
!Array(mapped_content_type).include?(calculated_content_type) && !Array(mapped_content_type).include?(type_from_mime_magic)
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculated_media_type_from_mime_magic
|
|
|
|
@calculated_media_type_from_mime_magic ||= type_from_mime_magic.split('/').first
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculated_type_mismatch?
|
|
|
|
!media_types_from_name.include?(calculated_media_type) && !media_types_from_name.include?(calculated_media_type_from_mime_magic)
|
2020-06-30 21:58:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def type_from_mime_magic
|
|
|
|
@type_from_mime_magic ||= begin
|
|
|
|
begin
|
|
|
|
File.open(@file.path) do |file|
|
|
|
|
MimeMagic.by_magic(file)&.type
|
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def type_from_file_command
|
|
|
|
@type_from_file_command ||= FileCommandContentTypeDetector.new(@file.path).detect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Paperclip::MediaTypeSpoofDetector.prepend(Paperclip::MediaTypeSpoofDetectorExtensions)
|