闭社主体 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.

166 lines
4.5 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. validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
  44. validates_attachment_size :file, less_than: 8.megabytes
  45. validates :account, presence: true
  46. scope :attached, -> { where.not(status_id: nil) }
  47. scope :local, -> { where(remote_url: '') }
  48. default_scope { order('id asc') }
  49. def local?
  50. remote_url.blank?
  51. end
  52. def file_remote_url=(url)
  53. self.file = URI.parse(Addressable::URI.parse(url).normalize.to_s)
  54. end
  55. def to_param
  56. shortcode
  57. end
  58. before_create :set_shortcode
  59. before_post_process :set_type_and_extension
  60. before_save :set_meta
  61. class << self
  62. private
  63. def file_styles(f)
  64. if f.instance.file_content_type == 'image/gif'
  65. {
  66. small: IMAGE_STYLES[:small],
  67. original: {
  68. format: 'mp4',
  69. convert_options: {
  70. output: {
  71. 'movflags' => 'faststart',
  72. 'pix_fmt' => 'yuv420p',
  73. 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
  74. 'vsync' => 'cfr',
  75. 'b:v' => '1300K',
  76. 'maxrate' => '500K',
  77. 'crf' => 6,
  78. },
  79. },
  80. },
  81. }
  82. elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
  83. IMAGE_STYLES
  84. else
  85. VIDEO_STYLES
  86. end
  87. end
  88. def file_processors(f)
  89. if f.file_content_type == 'image/gif'
  90. [:gif_transcoder]
  91. elsif VIDEO_MIME_TYPES.include? f.file_content_type
  92. [:video_transcoder]
  93. else
  94. [:thumbnail]
  95. end
  96. end
  97. end
  98. private
  99. def set_shortcode
  100. self.type = :unknown if file.blank? && !type_changed?
  101. return unless local?
  102. loop do
  103. self.shortcode = SecureRandom.urlsafe_base64(14)
  104. break if MediaAttachment.find_by(shortcode: shortcode).nil?
  105. end
  106. end
  107. def set_type_and_extension
  108. self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
  109. extension = appropriate_extension
  110. basename = Paperclip::Interpolations.basename(file, :original)
  111. file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
  112. end
  113. def set_meta
  114. meta = populate_meta
  115. return if meta == {}
  116. file.instance_write :meta, meta
  117. end
  118. def populate_meta
  119. meta = {}
  120. file.queued_for_write.each do |style, file|
  121. begin
  122. geo = Paperclip::Geometry.from_file file
  123. meta[style] = {
  124. width: geo.width.to_i,
  125. height: geo.height.to_i,
  126. size: "#{geo.width.to_i}x#{geo.height.to_i}",
  127. aspect: geo.width.to_f / geo.height.to_f,
  128. }
  129. rescue Paperclip::Errors::NotIdentifiedByImageMagickError
  130. meta[style] = {}
  131. end
  132. end
  133. meta
  134. end
  135. def appropriate_extension
  136. mime_type = MIME::Types[file.content_type]
  137. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  138. original_extension = Paperclip::Interpolations.extension(file, :original)
  139. extensions_for_mime_type.include?(original_extension) ? original_extension : extensions_for_mime_type.first
  140. end
  141. end