media_type_spoof_detector_extensions.rb 787 B

12345678910111213141516171819202122
  1. # frozen_string_literal: true
  2. module Paperclip
  3. module MediaTypeSpoofDetectorExtensions
  4. def calculated_content_type
  5. return @calculated_content_type if defined?(@calculated_content_type)
  6. @calculated_content_type = type_from_file_command.chomp
  7. # The `file` command fails to recognize some MP3 files as such
  8. @calculated_content_type = type_from_marcel if @calculated_content_type == 'application/octet-stream' && type_from_marcel == 'audio/mpeg'
  9. @calculated_content_type
  10. end
  11. def type_from_marcel
  12. @type_from_marcel ||= Marcel::MimeType.for Pathname.new(@file.path),
  13. name: @file.path
  14. end
  15. end
  16. end
  17. Paperclip::MediaTypeSpoofDetector.prepend(Paperclip::MediaTypeSpoofDetectorExtensions)