media_attachment.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. # created_at :datetime not null
  14. # updated_at :datetime not null
  15. # shortcode :string
  16. # type :integer default("image"), not null
  17. # file_meta :json
  18. # account_id :integer
  19. # description :text
  20. #
  21. class MediaAttachment < ApplicationRecord
  22. self.inheritance_column = nil
  23. enum type: [:image, :gifv, :video, :unknown]
  24. IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif'].freeze
  25. VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v'].freeze
  26. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  27. VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
  28. IMAGE_STYLES = {
  29. original: {
  30. geometry: '1280x1280>',
  31. file_geometry_parser: FastGeometryParser,
  32. },
  33. small: {
  34. geometry: '400x400>',
  35. file_geometry_parser: FastGeometryParser,
  36. },
  37. }.freeze
  38. VIDEO_STYLES = {
  39. small: {
  40. convert_options: {
  41. output: {
  42. vf: 'scale=\'min(400\, iw):min(400\, ih)\':force_original_aspect_ratio=decrease',
  43. },
  44. },
  45. format: 'png',
  46. time: 0,
  47. },
  48. }.freeze
  49. LIMIT = 8.megabytes
  50. belongs_to :account, inverse_of: :media_attachments, optional: true
  51. belongs_to :status, inverse_of: :media_attachments, optional: true
  52. has_attached_file :file,
  53. styles: ->(f) { file_styles f },
  54. processors: ->(f) { file_processors f },
  55. convert_options: { all: '-quality 90 -strip' }
  56. validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
  57. validates_attachment_size :file, less_than: LIMIT
  58. remotable_attachment :file, LIMIT
  59. include Attachmentable
  60. validates :account, presence: true
  61. validates :description, length: { maximum: 420 }, if: :local?
  62. scope :attached, -> { where.not(status_id: nil) }
  63. scope :unattached, -> { where(status_id: nil) }
  64. scope :local, -> { where(remote_url: '') }
  65. scope :remote, -> { where.not(remote_url: '') }
  66. default_scope { order(id: :asc) }
  67. def local?
  68. remote_url.blank?
  69. end
  70. def needs_redownload?
  71. file.blank? && remote_url.present?
  72. end
  73. def to_param
  74. shortcode
  75. end
  76. def focus=(point)
  77. return if point.blank?
  78. x, y = (point.is_a?(Enumerable) ? point : point.split(',')).map(&:to_f)
  79. meta = file.instance_read(:meta) || {}
  80. meta['focus'] = { 'x' => x, 'y' => y }
  81. file.instance_write(:meta, meta)
  82. end
  83. def focus
  84. x = file.meta['focus']['x']
  85. y = file.meta['focus']['y']
  86. "#{x},#{y}"
  87. end
  88. before_create :prepare_description, unless: :local?
  89. before_create :set_shortcode
  90. before_post_process :set_type_and_extension
  91. before_save :set_meta
  92. class << self
  93. private
  94. def file_styles(f)
  95. if f.instance.file_content_type == 'image/gif'
  96. {
  97. small: IMAGE_STYLES[:small],
  98. original: {
  99. format: 'mp4',
  100. convert_options: {
  101. output: {
  102. 'movflags' => 'faststart',
  103. 'pix_fmt' => 'yuv420p',
  104. 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
  105. 'vsync' => 'cfr',
  106. 'c:v' => 'h264',
  107. 'b:v' => '500K',
  108. 'maxrate' => '1300K',
  109. 'bufsize' => '1300K',
  110. 'crf' => 18,
  111. },
  112. },
  113. },
  114. }
  115. elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
  116. IMAGE_STYLES
  117. else
  118. VIDEO_STYLES
  119. end
  120. end
  121. def file_processors(f)
  122. if f.file_content_type == 'image/gif'
  123. [:gif_transcoder]
  124. elsif VIDEO_MIME_TYPES.include? f.file_content_type
  125. [:video_transcoder]
  126. else
  127. [:thumbnail]
  128. end
  129. end
  130. end
  131. private
  132. def set_shortcode
  133. self.type = :unknown if file.blank? && !type_changed?
  134. return unless local?
  135. loop do
  136. self.shortcode = SecureRandom.urlsafe_base64(14)
  137. break if MediaAttachment.find_by(shortcode: shortcode).nil?
  138. end
  139. end
  140. def prepare_description
  141. self.description = description.strip[0...420] unless description.nil?
  142. end
  143. def set_type_and_extension
  144. self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
  145. end
  146. def set_meta
  147. meta = populate_meta
  148. return if meta == {}
  149. file.instance_write :meta, meta
  150. end
  151. def populate_meta
  152. meta = file.instance_read(:meta) || {}
  153. file.queued_for_write.each do |style, file|
  154. meta[style] = style == :small || image? ? image_geometry(file) : video_metadata(file)
  155. end
  156. meta
  157. end
  158. def image_geometry(file)
  159. width, height = FastImage.size(file.path)
  160. return {} if width.nil?
  161. {
  162. width: width,
  163. height: height,
  164. size: "#{width}x#{height}",
  165. aspect: width.to_f / height.to_f,
  166. }
  167. end
  168. def video_metadata(file)
  169. movie = FFMPEG::Movie.new(file.path)
  170. return {} unless movie.valid?
  171. {
  172. width: movie.width,
  173. height: movie.height,
  174. frame_rate: movie.frame_rate,
  175. duration: movie.duration,
  176. bitrate: movie.bitrate,
  177. }
  178. end
  179. end