media_attachment.rb 5.5 KB

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