2017-05-01 16:20:57 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-20 10:52:36 +02:00
|
|
|
require 'mime/types/columnar'
|
2018-04-23 09:16:38 +02:00
|
|
|
|
2017-05-01 16:20:57 +02:00
|
|
|
module Attachmentable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2023-03-25 10:00:03 +01:00
|
|
|
MAX_MATRIX_LIMIT = 33_177_600 # 7680x4320px or approx. 847MB in RAM
|
2019-10-03 01:09:12 +02:00
|
|
|
GIF_MATRIX_LIMIT = 921_600 # 1280x720px
|
2018-04-23 09:16:38 +02:00
|
|
|
|
2020-06-30 23:58:02 +02:00
|
|
|
# For some file extensions, there exist different content
|
|
|
|
# type variants, and browsers often send the wrong one,
|
|
|
|
# for example, sending an audio .ogg file as video/ogg,
|
2024-01-03 20:02:53 +01:00
|
|
|
# likewise, kt-paperclip also misreports them as such. For
|
2020-06-30 23:58:02 +02:00
|
|
|
# those files, it is necessary to use the output of the
|
|
|
|
# `file` utility instead
|
|
|
|
INCORRECT_CONTENT_TYPES = %w(
|
2021-09-29 23:52:36 +02:00
|
|
|
audio/vorbis
|
2024-01-03 20:02:53 +01:00
|
|
|
audio/opus
|
2020-06-30 23:58:02 +02:00
|
|
|
video/ogg
|
|
|
|
video/webm
|
|
|
|
).freeze
|
|
|
|
|
2017-05-01 16:20:57 +02:00
|
|
|
included do
|
2021-09-29 23:52:36 +02:00
|
|
|
def self.has_attached_file(name, options = {}) # rubocop:disable Naming/PredicateName
|
2024-05-24 10:36:21 +02:00
|
|
|
super
|
2023-07-06 15:05:05 +02:00
|
|
|
|
2023-07-07 13:35:22 +02:00
|
|
|
send(:"before_#{name}_validate", prepend: true) do
|
2021-09-29 23:52:36 +02:00
|
|
|
attachment = send(name)
|
|
|
|
check_image_dimension(attachment)
|
|
|
|
set_file_content_type(attachment)
|
|
|
|
obfuscate_file_name(attachment)
|
|
|
|
set_file_extension(attachment)
|
|
|
|
end
|
|
|
|
end
|
2017-05-01 16:20:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-09-29 23:52:36 +02:00
|
|
|
def set_file_content_type(attachment) # rubocop:disable Naming/AccessorMethodName
|
|
|
|
return if attachment.blank? || attachment.queued_for_write[:original].blank? || !INCORRECT_CONTENT_TYPES.include?(attachment.instance_read(:content_type))
|
2019-06-22 16:54:06 +02:00
|
|
|
|
2021-09-29 23:52:36 +02:00
|
|
|
attachment.instance_write :content_type, calculated_content_type(attachment)
|
2019-06-22 16:54:06 +02:00
|
|
|
end
|
|
|
|
|
2021-09-29 23:52:36 +02:00
|
|
|
def set_file_extension(attachment) # rubocop:disable Naming/AccessorMethodName
|
|
|
|
return if attachment.blank?
|
2018-04-23 09:16:38 +02:00
|
|
|
|
2023-04-30 14:07:21 +02:00
|
|
|
attachment.instance_write :file_name, [Paperclip::Interpolations.basename(attachment, :original), appropriate_extension(attachment)].compact_blank!.join('.')
|
2018-04-23 09:16:38 +02:00
|
|
|
end
|
|
|
|
|
2021-09-29 23:52:36 +02:00
|
|
|
def check_image_dimension(attachment)
|
|
|
|
return if attachment.blank? || !/image.*/.match?(attachment.content_type) || attachment.queued_for_write[:original].blank?
|
2018-04-23 09:16:38 +02:00
|
|
|
|
2021-09-29 23:52:36 +02:00
|
|
|
width, height = FastImage.size(attachment.queued_for_write[:original].path)
|
2023-11-24 10:31:28 +01:00
|
|
|
return unless width.present? && height.present?
|
2018-04-23 09:16:38 +02:00
|
|
|
|
2023-11-24 10:31:28 +01:00
|
|
|
if attachment.content_type == 'image/gif' && width * height > GIF_MATRIX_LIMIT
|
|
|
|
raise Mastodon::DimensionsValidationError, "#{width}x#{height} GIF files are not supported"
|
|
|
|
elsif width * height > MAX_MATRIX_LIMIT
|
|
|
|
raise Mastodon::DimensionsValidationError, "#{width}x#{height} images are not supported"
|
|
|
|
end
|
2017-05-01 16:20:57 +02:00
|
|
|
end
|
2018-04-23 09:16:38 +02:00
|
|
|
|
|
|
|
def appropriate_extension(attachment)
|
|
|
|
mime_type = MIME::Types[attachment.content_type]
|
|
|
|
|
|
|
|
extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
|
|
|
|
original_extension = Paperclip::Interpolations.extension(attachment, :original)
|
2018-06-18 17:27:05 +02:00
|
|
|
proper_extension = extensions_for_mime_type.first.to_s
|
2018-06-24 13:33:06 +02:00
|
|
|
extension = extensions_for_mime_type.include?(original_extension) ? original_extension : proper_extension
|
2024-06-05 21:15:39 +02:00
|
|
|
extension = 'jpeg' if ['jpe', 'jfif'].include?(extension)
|
2018-04-23 09:16:38 +02:00
|
|
|
|
2018-06-24 13:33:06 +02:00
|
|
|
extension
|
2018-04-23 09:16:38 +02:00
|
|
|
end
|
2019-06-22 16:54:06 +02:00
|
|
|
|
|
|
|
def calculated_content_type(attachment)
|
2020-06-30 23:58:02 +02:00
|
|
|
Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp
|
2019-06-22 16:54:06 +02:00
|
|
|
rescue Terrapin::CommandLineError
|
|
|
|
''
|
|
|
|
end
|
2020-01-04 01:54:07 +01:00
|
|
|
|
2021-09-29 23:52:36 +02:00
|
|
|
def obfuscate_file_name(attachment)
|
|
|
|
return if attachment.blank? || attachment.queued_for_write[:original].blank? || attachment.options[:preserve_files]
|
2020-01-04 01:54:07 +01:00
|
|
|
|
2021-09-29 23:52:36 +02:00
|
|
|
attachment.instance_write :file_name, SecureRandom.hex(8) + File.extname(attachment.instance_read(:file_name))
|
2020-01-04 01:54:07 +01:00
|
|
|
end
|
2017-05-01 16:20:57 +02:00
|
|
|
end
|