media_type_spoof_detector_extensions.rb 852 B

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