You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.3 KiB

  1. # frozen_string_literal: true
  2. require 'mime/types/columnar'
  3. module Attachmentable
  4. extend ActiveSupport::Concern
  5. MAX_MATRIX_LIMIT = 16_777_216 # 4096x4096px or approx. 16MB
  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. video/ogg
  15. video/webm
  16. ).freeze
  17. included do
  18. before_post_process :obfuscate_file_name
  19. before_post_process :set_file_extensions
  20. before_post_process :check_image_dimensions
  21. before_post_process :set_file_content_type
  22. end
  23. private
  24. def set_file_content_type
  25. self.class.attachment_definitions.each_key do |attachment_name|
  26. attachment = send(attachment_name)
  27. next if attachment.blank? || attachment.queued_for_write[:original].blank? || !INCORRECT_CONTENT_TYPES.include?(attachment.instance_read(:content_type))
  28. attachment.instance_write :content_type, calculated_content_type(attachment)
  29. end
  30. end
  31. def set_file_extensions
  32. self.class.attachment_definitions.each_key do |attachment_name|
  33. attachment = send(attachment_name)
  34. next if attachment.blank?
  35. attachment.instance_write :file_name, [Paperclip::Interpolations.basename(attachment, :original), appropriate_extension(attachment)].delete_if(&:blank?).join('.')
  36. end
  37. end
  38. def check_image_dimensions
  39. self.class.attachment_definitions.each_key do |attachment_name|
  40. attachment = send(attachment_name)
  41. next if attachment.blank? || !/image.*/.match?(attachment.content_type) || attachment.queued_for_write[:original].blank?
  42. width, height = FastImage.size(attachment.queued_for_write[:original].path)
  43. matrix_limit = attachment.content_type == 'image/gif' ? GIF_MATRIX_LIMIT : MAX_MATRIX_LIMIT
  44. raise Mastodon::DimensionsValidationError, "#{width}x#{height} images are not supported" if width.present? && height.present? && (width * height > matrix_limit)
  45. end
  46. end
  47. def appropriate_extension(attachment)
  48. mime_type = MIME::Types[attachment.content_type]
  49. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  50. original_extension = Paperclip::Interpolations.extension(attachment, :original)
  51. proper_extension = extensions_for_mime_type.first.to_s
  52. extension = extensions_for_mime_type.include?(original_extension) ? original_extension : proper_extension
  53. extension = 'jpeg' if extension == 'jpe'
  54. extension
  55. end
  56. def calculated_content_type(attachment)
  57. Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp
  58. rescue Terrapin::CommandLineError
  59. ''
  60. end
  61. def obfuscate_file_name
  62. self.class.attachment_definitions.each_key do |attachment_name|
  63. attachment = send(attachment_name)
  64. next 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
  68. end