2023-07-06 13:05:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Paperclip
|
|
|
|
module MediaTypeSpoofDetectorExtensions
|
2023-08-01 17:34:11 +00:00
|
|
|
MARCEL_MIME_TYPES = %w(audio/mpeg image/avif).freeze
|
|
|
|
|
2023-07-06 13:05:05 +00:00
|
|
|
def calculated_content_type
|
|
|
|
return @calculated_content_type if defined?(@calculated_content_type)
|
|
|
|
|
|
|
|
@calculated_content_type = type_from_file_command.chomp
|
|
|
|
|
|
|
|
# The `file` command fails to recognize some MP3 files as such
|
2023-08-01 17:34:11 +00:00
|
|
|
@calculated_content_type = type_from_marcel if @calculated_content_type == 'application/octet-stream' && type_from_marcel.in?(MARCEL_MIME_TYPES)
|
2023-07-06 13:05:05 +00:00
|
|
|
@calculated_content_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def type_from_marcel
|
|
|
|
@type_from_marcel ||= Marcel::MimeType.for Pathname.new(@file.path),
|
|
|
|
name: @file.path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Paperclip::MediaTypeSpoofDetector.prepend(Paperclip::MediaTypeSpoofDetectorExtensions)
|