You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. module Paperclip
  3. module MediaTypeSpoofDetectorExtensions
  4. def mapping_override_mismatch?
  5. !Array(mapped_content_type).include?(calculated_content_type) && !Array(mapped_content_type).include?(type_from_mime_magic)
  6. end
  7. def calculated_media_type_from_mime_magic
  8. @calculated_media_type_from_mime_magic ||= type_from_mime_magic.split('/').first
  9. end
  10. def calculated_type_mismatch?
  11. !media_types_from_name.include?(calculated_media_type) && !media_types_from_name.include?(calculated_media_type_from_mime_magic)
  12. end
  13. def type_from_mime_magic
  14. @type_from_mime_magic ||= begin
  15. begin
  16. File.open(@file.path) do |file|
  17. MimeMagic.by_magic(file)&.type || ''
  18. end
  19. rescue Errno::ENOENT
  20. ''
  21. end
  22. end
  23. end
  24. def type_from_file_command
  25. @type_from_file_command ||= FileCommandContentTypeDetector.new(@file.path).detect
  26. end
  27. end
  28. end
  29. Paperclip::MediaTypeSpoofDetector.prepend(Paperclip::MediaTypeSpoofDetectorExtensions)