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.

27 lines
691 B

  1. # frozen_string_literal: true
  2. module Paperclip
  3. module MediaTypeSpoofDetectorExtensions
  4. def calculated_content_type
  5. @calculated_content_type ||= type_from_mime_magic || type_from_file_command
  6. end
  7. def type_from_mime_magic
  8. @type_from_mime_magic ||= begin
  9. begin
  10. File.open(@file.path) do |file|
  11. MimeMagic.by_magic(file)&.type
  12. end
  13. rescue Errno::ENOENT
  14. ''
  15. end
  16. end
  17. end
  18. def type_from_file_command
  19. @type_from_file_command ||= FileCommandContentTypeDetector.new(@file.path).detect
  20. end
  21. end
  22. end
  23. Paperclip::MediaTypeSpoofDetector.prepend(Paperclip::MediaTypeSpoofDetectorExtensions)