media_attachment.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: media_attachments
  5. #
  6. # id :bigint(8) not null, primary key
  7. # status_id :bigint(8)
  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 :bigint(8)
  19. # description :text
  20. # scheduled_status_id :bigint(8)
  21. # blurhash :string
  22. # processing :integer
  23. # file_storage_schema_version :integer
  24. #
  25. class MediaAttachment < ApplicationRecord
  26. self.inheritance_column = nil
  27. enum type: [:image, :gifv, :video, :unknown, :audio]
  28. enum processing: [:queued, :in_progress, :complete, :failed], _prefix: true
  29. MAX_DESCRIPTION_LENGTH = 1_500
  30. IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif).freeze
  31. VIDEO_FILE_EXTENSIONS = %w(.webm .mp4 .m4v .mov).freeze
  32. AUDIO_FILE_EXTENSIONS = %w(.ogg .oga .mp3 .wav .flac .opus .aac .m4a .3gp .wma).freeze
  33. IMAGE_MIME_TYPES = %w(image/jpeg image/png image/gif).freeze
  34. VIDEO_MIME_TYPES = %w(video/webm video/mp4 video/quicktime video/ogg).freeze
  35. VIDEO_CONVERTIBLE_MIME_TYPES = %w(video/webm video/quicktime).freeze
  36. AUDIO_MIME_TYPES = %w(audio/wave audio/wav audio/x-wav audio/x-pn-wave audio/ogg audio/mpeg audio/mp3 audio/webm audio/flac audio/aac audio/m4a audio/x-m4a audio/mp4 audio/3gpp video/x-ms-asf).freeze
  37. BLURHASH_OPTIONS = {
  38. x_comp: 4,
  39. y_comp: 4,
  40. }.freeze
  41. IMAGE_STYLES = {
  42. original: {
  43. pixels: 1_638_400, # 1280x1280px
  44. file_geometry_parser: FastGeometryParser,
  45. },
  46. small: {
  47. pixels: 160_000, # 400x400px
  48. file_geometry_parser: FastGeometryParser,
  49. blurhash: BLURHASH_OPTIONS,
  50. },
  51. }.freeze
  52. VIDEO_FORMAT = {
  53. format: 'mp4',
  54. content_type: 'video/mp4',
  55. convert_options: {
  56. output: {
  57. 'loglevel' => 'fatal',
  58. 'movflags' => 'faststart',
  59. 'pix_fmt' => 'yuv420p',
  60. 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
  61. 'vsync' => 'cfr',
  62. 'c:v' => 'h264',
  63. 'maxrate' => '1300K',
  64. 'bufsize' => '1300K',
  65. 'frames:v' => 60 * 60 * 3,
  66. 'crf' => 18,
  67. 'map_metadata' => '-1',
  68. },
  69. },
  70. }.freeze
  71. VIDEO_PASSTHROUGH_OPTIONS = {
  72. video_codecs: ['h264'],
  73. audio_codecs: ['aac', nil],
  74. colorspaces: ['yuv420p'],
  75. options: {
  76. format: 'mp4',
  77. convert_options: {
  78. output: {
  79. 'loglevel' => 'fatal',
  80. 'map_metadata' => '-1',
  81. 'c:v' => 'copy',
  82. 'c:a' => 'copy',
  83. },
  84. },
  85. },
  86. }.freeze
  87. VIDEO_STYLES = {
  88. small: {
  89. convert_options: {
  90. output: {
  91. 'loglevel' => 'fatal',
  92. vf: 'scale=\'min(400\, iw):min(400\, ih)\':force_original_aspect_ratio=decrease',
  93. },
  94. },
  95. format: 'png',
  96. time: 0,
  97. file_geometry_parser: FastGeometryParser,
  98. blurhash: BLURHASH_OPTIONS,
  99. },
  100. original: VIDEO_FORMAT.merge(passthrough_options: VIDEO_PASSTHROUGH_OPTIONS),
  101. }.freeze
  102. AUDIO_STYLES = {
  103. original: {
  104. format: 'mp3',
  105. content_type: 'audio/mpeg',
  106. convert_options: {
  107. output: {
  108. 'loglevel' => 'fatal',
  109. 'map_metadata' => '-1',
  110. 'q:a' => 2,
  111. },
  112. },
  113. },
  114. }.freeze
  115. VIDEO_CONVERTED_STYLES = {
  116. small: VIDEO_STYLES[:small],
  117. original: VIDEO_FORMAT,
  118. }.freeze
  119. IMAGE_LIMIT = 10.megabytes
  120. VIDEO_LIMIT = 40.megabytes
  121. MAX_VIDEO_MATRIX_LIMIT = 2_304_000 # 1920x1200px
  122. MAX_VIDEO_FRAME_RATE = 60
  123. belongs_to :account, inverse_of: :media_attachments, optional: true
  124. belongs_to :status, inverse_of: :media_attachments, optional: true
  125. belongs_to :scheduled_status, inverse_of: :media_attachments, optional: true
  126. has_attached_file :file,
  127. styles: ->(f) { file_styles f },
  128. processors: ->(f) { file_processors f },
  129. convert_options: { all: '-quality 90 -strip +set modify-date +set create-date' }
  130. validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
  131. validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :larger_media_format?
  132. validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :larger_media_format?
  133. remotable_attachment :file, VIDEO_LIMIT, suppress_errors: false
  134. include Attachmentable
  135. validates :account, presence: true
  136. validates :description, length: { maximum: MAX_DESCRIPTION_LENGTH }, if: :local?
  137. validates :file, presence: true, if: :local?
  138. scope :attached, -> { where.not(status_id: nil).or(where.not(scheduled_status_id: nil)) }
  139. scope :unattached, -> { where(status_id: nil, scheduled_status_id: nil) }
  140. scope :local, -> { where(remote_url: '') }
  141. scope :remote, -> { where.not(remote_url: '') }
  142. scope :cached, -> { remote.where.not(file_file_name: nil) }
  143. default_scope { order(id: :asc) }
  144. def local?
  145. remote_url.blank?
  146. end
  147. def not_processed?
  148. processing.present? && !processing_complete?
  149. end
  150. def needs_redownload?
  151. file.blank? && remote_url.present?
  152. end
  153. def larger_media_format?
  154. video? || gifv? || audio?
  155. end
  156. def audio_or_video?
  157. audio? || video?
  158. end
  159. def to_param
  160. shortcode
  161. end
  162. def focus=(point)
  163. return if point.blank?
  164. x, y = (point.is_a?(Enumerable) ? point : point.split(',')).map(&:to_f)
  165. meta = file.instance_read(:meta) || {}
  166. meta['focus'] = { 'x' => x, 'y' => y }
  167. file.instance_write(:meta, meta)
  168. end
  169. def focus
  170. x = file.meta['focus']['x']
  171. y = file.meta['focus']['y']
  172. "#{x},#{y}"
  173. end
  174. attr_writer :delay_processing
  175. def delay_processing?
  176. @delay_processing
  177. end
  178. after_commit :enqueue_processing, on: :create
  179. after_commit :reset_parent_cache, on: :update
  180. before_create :prepare_description, unless: :local?
  181. before_create :set_shortcode
  182. before_create :set_processing
  183. before_post_process :set_type_and_extension
  184. before_post_process :check_video_dimensions
  185. before_save :set_meta
  186. class << self
  187. def supported_mime_types
  188. IMAGE_MIME_TYPES + VIDEO_MIME_TYPES + AUDIO_MIME_TYPES
  189. end
  190. def supported_file_extensions
  191. IMAGE_FILE_EXTENSIONS + VIDEO_FILE_EXTENSIONS + AUDIO_FILE_EXTENSIONS
  192. end
  193. private
  194. def file_styles(f)
  195. if f.instance.file_content_type == 'image/gif' || VIDEO_CONVERTIBLE_MIME_TYPES.include?(f.instance.file_content_type)
  196. VIDEO_CONVERTED_STYLES
  197. elsif IMAGE_MIME_TYPES.include?(f.instance.file_content_type)
  198. IMAGE_STYLES
  199. elsif VIDEO_MIME_TYPES.include?(f.instance.file_content_type)
  200. VIDEO_STYLES
  201. else
  202. AUDIO_STYLES
  203. end
  204. end
  205. def file_processors(f)
  206. if f.file_content_type == 'image/gif'
  207. [:gif_transcoder, :blurhash_transcoder]
  208. elsif VIDEO_MIME_TYPES.include?(f.file_content_type)
  209. [:video_transcoder, :blurhash_transcoder, :type_corrector]
  210. elsif AUDIO_MIME_TYPES.include?(f.file_content_type)
  211. [:transcoder, :type_corrector]
  212. else
  213. [:lazy_thumbnail, :blurhash_transcoder, :type_corrector]
  214. end
  215. end
  216. end
  217. private
  218. def set_shortcode
  219. self.type = :unknown if file.blank? && !type_changed?
  220. return unless local?
  221. loop do
  222. self.shortcode = SecureRandom.urlsafe_base64(14)
  223. break if MediaAttachment.find_by(shortcode: shortcode).nil?
  224. end
  225. end
  226. def prepare_description
  227. self.description = description.strip[0...MAX_DESCRIPTION_LENGTH] unless description.nil?
  228. end
  229. def set_type_and_extension
  230. self.type = begin
  231. if VIDEO_MIME_TYPES.include?(file_content_type)
  232. :video
  233. elsif AUDIO_MIME_TYPES.include?(file_content_type)
  234. :audio
  235. else
  236. :image
  237. end
  238. end
  239. end
  240. def set_processing
  241. self.processing = delay_processing? ? :queued : :complete
  242. end
  243. def check_video_dimensions
  244. return unless (video? || gifv?) && file.queued_for_write[:original].present?
  245. movie = FFMPEG::Movie.new(file.queued_for_write[:original].path)
  246. return unless movie.valid?
  247. raise Mastodon::DimensionsValidationError, "#{movie.width}x#{movie.height} videos are not supported" if movie.width * movie.height > MAX_VIDEO_MATRIX_LIMIT
  248. raise Mastodon::DimensionsValidationError, "#{movie.frame_rate.to_i}fps videos are not supported" if movie.frame_rate > MAX_VIDEO_FRAME_RATE
  249. end
  250. def set_meta
  251. meta = populate_meta
  252. return if meta == {}
  253. file.instance_write :meta, meta
  254. end
  255. def populate_meta
  256. meta = file.instance_read(:meta) || {}
  257. file.queued_for_write.each do |style, file|
  258. meta[style] = style == :small || image? ? image_geometry(file) : video_metadata(file)
  259. end
  260. meta
  261. end
  262. def image_geometry(file)
  263. width, height = FastImage.size(file.path)
  264. return {} if width.nil?
  265. {
  266. width: width,
  267. height: height,
  268. size: "#{width}x#{height}",
  269. aspect: width.to_f / height,
  270. }
  271. end
  272. def video_metadata(file)
  273. movie = FFMPEG::Movie.new(file.path)
  274. return {} unless movie.valid?
  275. {
  276. width: movie.width,
  277. height: movie.height,
  278. frame_rate: movie.frame_rate,
  279. duration: movie.duration,
  280. bitrate: movie.bitrate,
  281. }.compact
  282. end
  283. def enqueue_processing
  284. PostProcessMediaWorker.perform_async(id) if delay_processing?
  285. end
  286. def reset_parent_cache
  287. Rails.cache.delete("statuses/#{status_id}") if status_id.present?
  288. end
  289. end