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.

233 lines
5.8 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. # 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. def focus=(point)
  76. return if point.blank?
  77. x, y = (point.is_a?(Enumerable) ? point : point.split(',')).map(&:to_f)
  78. meta = file.instance_read(:meta) || {}
  79. meta['focus'] = { 'x' => x, 'y' => y }
  80. file.instance_write(:meta, meta)
  81. end
  82. def focus
  83. x = file.meta['focus']['x']
  84. y = file.meta['focus']['y']
  85. "#{x},#{y}"
  86. end
  87. before_create :prepare_description, unless: :local?
  88. before_create :set_shortcode
  89. before_post_process :set_type_and_extension
  90. before_save :set_meta
  91. class << self
  92. private
  93. def file_styles(f)
  94. if f.instance.file_content_type == 'image/gif'
  95. {
  96. small: IMAGE_STYLES[:small],
  97. original: {
  98. format: 'mp4',
  99. convert_options: {
  100. output: {
  101. 'movflags' => 'faststart',
  102. 'pix_fmt' => 'yuv420p',
  103. 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
  104. 'vsync' => 'cfr',
  105. 'b:v' => '1300K',
  106. 'maxrate' => '500K',
  107. 'bufsize' => '1300K',
  108. 'crf' => 18,
  109. },
  110. },
  111. },
  112. }
  113. elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
  114. IMAGE_STYLES
  115. else
  116. VIDEO_STYLES
  117. end
  118. end
  119. def file_processors(f)
  120. if f.file_content_type == 'image/gif'
  121. [:gif_transcoder]
  122. elsif VIDEO_MIME_TYPES.include? f.file_content_type
  123. [:video_transcoder]
  124. else
  125. [:thumbnail]
  126. end
  127. end
  128. end
  129. private
  130. def set_shortcode
  131. self.type = :unknown if file.blank? && !type_changed?
  132. return unless local?
  133. loop do
  134. self.shortcode = SecureRandom.urlsafe_base64(14)
  135. break if MediaAttachment.find_by(shortcode: shortcode).nil?
  136. end
  137. end
  138. def prepare_description
  139. self.description = description.strip[0...420] unless description.nil?
  140. end
  141. def set_type_and_extension
  142. self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
  143. extension = appropriate_extension
  144. basename = Paperclip::Interpolations.basename(file, :original)
  145. file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
  146. end
  147. def set_meta
  148. meta = populate_meta
  149. return if meta == {}
  150. file.instance_write :meta, meta
  151. end
  152. def populate_meta
  153. meta = file.instance_read(:meta) || {}
  154. file.queued_for_write.each do |style, file|
  155. meta[style] = style == :small || image? ? image_geometry(file) : video_metadata(file)
  156. end
  157. meta
  158. end
  159. def image_geometry(file)
  160. width, height = FastImage.size(file.path)
  161. return {} if width.nil?
  162. {
  163. width: width,
  164. height: height,
  165. size: "#{width}x#{height}",
  166. aspect: width.to_f / height.to_f,
  167. }
  168. end
  169. def video_metadata(file)
  170. movie = FFMPEG::Movie.new(file.path)
  171. return {} unless movie.valid?
  172. {
  173. width: movie.width,
  174. height: movie.height,
  175. frame_rate: movie.frame_rate,
  176. duration: movie.duration,
  177. bitrate: movie.bitrate,
  178. }
  179. end
  180. def appropriate_extension
  181. mime_type = MIME::Types[file.content_type]
  182. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  183. original_extension = Paperclip::Interpolations.extension(file, :original)
  184. extensions_for_mime_type.include?(original_extension) ? original_extension : extensions_for_mime_type.first
  185. end
  186. end