mirror of
https://github.com/mastodon/mastodon.git
synced 2024-10-22 10:35:40 +09:00
28 lines
691 B
Ruby
28 lines
691 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Paperclip
|
|
module MediaTypeSpoofDetectorExtensions
|
|
def calculated_content_type
|
|
@calculated_content_type ||= type_from_mime_magic || type_from_file_command
|
|
end
|
|
|
|
def type_from_mime_magic
|
|
@type_from_mime_magic ||= begin
|
|
begin
|
|
File.open(@file.path) do |file|
|
|
MimeMagic.by_magic(file)&.type
|
|
end
|
|
rescue Errno::ENOENT
|
|
''
|
|
end
|
|
end
|
|
end
|
|
|
|
def type_from_file_command
|
|
@type_from_file_command ||= FileCommandContentTypeDetector.new(@file.path).detect
|
|
end
|
|
end
|
|
end
|
|
|
|
Paperclip::MediaTypeSpoofDetector.prepend(Paperclip::MediaTypeSpoofDetectorExtensions)
|