attachmentable.rb 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. 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)].delete_if(&: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. matrix_limit = attachment.content_type == 'image/gif' ? GIF_MATRIX_LIMIT : MAX_MATRIX_LIMIT
  43. raise Mastodon::DimensionsValidationError, "#{width}x#{height} images are not supported" if width.present? && height.present? && (width * height > matrix_limit)
  44. end
  45. def appropriate_extension(attachment)
  46. mime_type = MIME::Types[attachment.content_type]
  47. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  48. original_extension = Paperclip::Interpolations.extension(attachment, :original)
  49. proper_extension = extensions_for_mime_type.first.to_s
  50. extension = extensions_for_mime_type.include?(original_extension) ? original_extension : proper_extension
  51. extension = 'jpeg' if extension == 'jpe'
  52. extension
  53. end
  54. def calculated_content_type(attachment)
  55. Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp
  56. rescue Terrapin::CommandLineError
  57. ''
  58. end
  59. def obfuscate_file_name(attachment)
  60. return if attachment.blank? || attachment.queued_for_write[:original].blank? || attachment.options[:preserve_files]
  61. attachment.instance_write :file_name, SecureRandom.hex(8) + File.extname(attachment.instance_read(:file_name))
  62. end
  63. end