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.

82 lines
2.9 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. included do
  8. before_post_process :obfuscate_file_name
  9. before_post_process :set_file_extensions
  10. before_post_process :check_image_dimensions
  11. before_post_process :set_file_content_type
  12. end
  13. private
  14. def set_file_content_type
  15. self.class.attachment_definitions.each_key do |attachment_name|
  16. attachment = send(attachment_name)
  17. next if attachment.blank? || attachment.queued_for_write[:original].blank?
  18. attachment.instance_write :content_type, calculated_content_type(attachment)
  19. end
  20. end
  21. def set_file_extensions
  22. self.class.attachment_definitions.each_key do |attachment_name|
  23. attachment = send(attachment_name)
  24. next if attachment.blank?
  25. attachment.instance_write :file_name, [Paperclip::Interpolations.basename(attachment, :original), appropriate_extension(attachment)].delete_if(&:blank?).join('.')
  26. end
  27. end
  28. def check_image_dimensions
  29. self.class.attachment_definitions.each_key do |attachment_name|
  30. attachment = send(attachment_name)
  31. next if attachment.blank? || !/image.*/.match?(attachment.content_type) || attachment.queued_for_write[:original].blank?
  32. width, height = FastImage.size(attachment.queued_for_write[:original].path)
  33. matrix_limit = attachment.content_type == 'image/gif' ? GIF_MATRIX_LIMIT : MAX_MATRIX_LIMIT
  34. raise Mastodon::DimensionsValidationError, "#{width}x#{height} images are not supported" if width.present? && height.present? && (width * height > matrix_limit)
  35. end
  36. end
  37. def appropriate_extension(attachment)
  38. mime_type = MIME::Types[attachment.content_type]
  39. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  40. original_extension = Paperclip::Interpolations.extension(attachment, :original)
  41. proper_extension = extensions_for_mime_type.first.to_s
  42. extension = extensions_for_mime_type.include?(original_extension) ? original_extension : proper_extension
  43. extension = 'jpeg' if extension == 'jpe'
  44. extension
  45. end
  46. def calculated_content_type(attachment)
  47. content_type = Paperclip.run('file', '-b --mime :file', file: attachment.queued_for_write[:original].path).split(/[:;\s]+/).first.chomp
  48. content_type = 'video/mp4' if content_type == 'video/x-m4v'
  49. content_type
  50. rescue Terrapin::CommandLineError
  51. ''
  52. end
  53. def obfuscate_file_name
  54. self.class.attachment_definitions.each_key do |attachment_name|
  55. attachment = send(attachment_name)
  56. next if attachment.blank? || attachment.queued_for_write[:original].blank? || attachment.options[:preserve_files]
  57. attachment.instance_write :file_name, SecureRandom.hex(8) + File.extname(attachment.instance_read(:file_name))
  58. end
  59. end
  60. end