attachmentable.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # frozen_string_literal: true
  2. require 'mime/types/columnar'
  3. module Attachmentable
  4. extend ActiveSupport::Concern
  5. MAX_MATRIX_LIMIT = 33_177_600 # 7680x4320px or approx. 847MB in RAM
  6. GIF_MATRIX_LIMIT = 921_600 # 1280x720px
  7. # For some file extensions, there exist different content
  8. # type variants, and browsers often send the wrong one,
  9. # for example, sending an audio .ogg file as video/ogg,
  10. # likewise, MimeMagic also misreports them as such. For
  11. # those files, it is necessary to use the output of the
  12. # `file` utility instead
  13. INCORRECT_CONTENT_TYPES = %w(
  14. audio/vorbis
  15. video/ogg
  16. video/webm
  17. ).freeze
  18. included do
  19. def self.has_attached_file(name, options = {}) # rubocop:disable Naming/PredicateName
  20. super(name, options)
  21. send(:"before_#{name}_validate", prepend: true) do
  22. attachment = send(name)
  23. check_image_dimension(attachment)
  24. set_file_content_type(attachment)
  25. obfuscate_file_name(attachment)
  26. set_file_extension(attachment)
  27. end
  28. end
  29. end
  30. private
  31. def set_file_content_type(attachment) # rubocop:disable Naming/AccessorMethodName
  32. return if attachment.blank? || attachment.queued_for_write[:original].blank? || !INCORRECT_CONTENT_TYPES.include?(attachment.instance_read(:content_type))
  33. attachment.instance_write :content_type, calculated_content_type(attachment)
  34. end
  35. def set_file_extension(attachment) # rubocop:disable Naming/AccessorMethodName
  36. return if attachment.blank?
  37. attachment.instance_write :file_name, [Paperclip::Interpolations.basename(attachment, :original), appropriate_extension(attachment)].compact_blank!.join('.')
  38. end
  39. def check_image_dimension(attachment)
  40. return if attachment.blank? || !/image.*/.match?(attachment.content_type) || attachment.queued_for_write[:original].blank?
  41. width, height = FastImage.size(attachment.queued_for_write[:original].path)
  42. return unless width.present? && height.present?
  43. if attachment.content_type == 'image/gif' && width * height > GIF_MATRIX_LIMIT
  44. raise Mastodon::DimensionsValidationError, "#{width}x#{height} GIF files are not supported"
  45. elsif width * height > MAX_MATRIX_LIMIT
  46. raise Mastodon::DimensionsValidationError, "#{width}x#{height} images are not supported"
  47. end
  48. end
  49. def appropriate_extension(attachment)
  50. mime_type = MIME::Types[attachment.content_type]
  51. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  52. original_extension = Paperclip::Interpolations.extension(attachment, :original)
  53. proper_extension = extensions_for_mime_type.first.to_s
  54. extension = extensions_for_mime_type.include?(original_extension) ? original_extension : proper_extension
  55. extension = 'jpeg' if extension == 'jpe'
  56. extension
  57. end
  58. def calculated_content_type(attachment)
  59. Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp
  60. rescue Terrapin::CommandLineError
  61. ''
  62. end
  63. def obfuscate_file_name(attachment)
  64. return if attachment.blank? || attachment.queued_for_write[:original].blank? || attachment.options[:preserve_files]
  65. attachment.instance_write :file_name, SecureRandom.hex(8) + File.extname(attachment.instance_read(:file_name))
  66. end
  67. end