闭社主体 forked from https://github.com/tootsuite/mastodon
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.

165 lines
4.4 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: media_attachments
  5. #
  6. # id :integer not null, primary key
  7. # status_id :integer
  8. # file_file_name :string
  9. # file_content_type :string
  10. # file_file_size :integer
  11. # file_updated_at :datetime
  12. # remote_url :string default(""), not null
  13. # account_id :integer
  14. # created_at :datetime not null
  15. # updated_at :datetime not null
  16. # shortcode :string
  17. # type :integer default("image"), not null
  18. # file_meta :json
  19. #
  20. class MediaAttachment < ApplicationRecord
  21. self.inheritance_column = nil
  22. enum type: [:image, :gifv, :video, :unknown]
  23. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  24. VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
  25. IMAGE_STYLES = { original: '1280x1280>', small: '400x400>' }.freeze
  26. VIDEO_STYLES = {
  27. small: {
  28. convert_options: {
  29. output: {
  30. vf: 'scale=\'min(400\, iw):min(400\, ih)\':force_original_aspect_ratio=decrease',
  31. },
  32. },
  33. format: 'png',
  34. time: 0,
  35. },
  36. }.freeze
  37. belongs_to :account, inverse_of: :media_attachments
  38. belongs_to :status, inverse_of: :media_attachments
  39. has_attached_file :file,
  40. styles: ->(f) { file_styles f },
  41. processors: ->(f) { file_processors f },
  42. convert_options: { all: '-quality 90 -strip' }
  43. include Remotable
  44. validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
  45. validates_attachment_size :file, less_than: 8.megabytes
  46. validates :account, presence: true
  47. scope :attached, -> { where.not(status_id: nil) }
  48. scope :local, -> { where(remote_url: '') }
  49. default_scope { order(id: :asc) }
  50. def local?
  51. remote_url.blank?
  52. end
  53. def to_param
  54. shortcode
  55. end
  56. before_create :set_shortcode
  57. before_post_process :set_type_and_extension
  58. before_save :set_meta
  59. class << self
  60. private
  61. def file_styles(f)
  62. if f.instance.file_content_type == 'image/gif'
  63. {
  64. small: IMAGE_STYLES[:small],
  65. original: {
  66. format: 'mp4',
  67. convert_options: {
  68. output: {
  69. 'movflags' => 'faststart',
  70. 'pix_fmt' => 'yuv420p',
  71. 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
  72. 'vsync' => 'cfr',
  73. 'b:v' => '1300K',
  74. 'maxrate' => '500K',
  75. 'crf' => 6,
  76. },
  77. },
  78. },
  79. }
  80. elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
  81. IMAGE_STYLES
  82. else
  83. VIDEO_STYLES
  84. end
  85. end
  86. def file_processors(f)
  87. if f.file_content_type == 'image/gif'
  88. [:gif_transcoder]
  89. elsif VIDEO_MIME_TYPES.include? f.file_content_type
  90. [:video_transcoder]
  91. else
  92. [:thumbnail]
  93. end
  94. end
  95. end
  96. private
  97. def set_shortcode
  98. self.type = :unknown if file.blank? && !type_changed?
  99. return unless local?
  100. loop do
  101. self.shortcode = SecureRandom.urlsafe_base64(14)
  102. break if MediaAttachment.find_by(shortcode: shortcode).nil?
  103. end
  104. end
  105. def set_type_and_extension
  106. self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
  107. extension = appropriate_extension
  108. basename = Paperclip::Interpolations.basename(file, :original)
  109. file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
  110. end
  111. def set_meta
  112. meta = populate_meta
  113. return if meta == {}
  114. file.instance_write :meta, meta
  115. end
  116. def populate_meta
  117. meta = {}
  118. file.queued_for_write.each do |style, file|
  119. begin
  120. geo = Paperclip::Geometry.from_file file
  121. meta[style] = {
  122. width: geo.width.to_i,
  123. height: geo.height.to_i,
  124. size: "#{geo.width.to_i}x#{geo.height.to_i}",
  125. aspect: geo.width.to_f / geo.height.to_f,
  126. }
  127. rescue Paperclip::Errors::NotIdentifiedByImageMagickError
  128. meta[style] = {}
  129. end
  130. end
  131. meta
  132. end
  133. def appropriate_extension
  134. mime_type = MIME::Types[file.content_type]
  135. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  136. original_extension = Paperclip::Interpolations.extension(file, :original)
  137. extensions_for_mime_type.include?(original_extension) ? original_extension : extensions_for_mime_type.first
  138. end
  139. end