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.

234 lines
5.9 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. LIMIT = 8.megabytes
  51. belongs_to :account, inverse_of: :media_attachments, optional: true
  52. belongs_to :status, inverse_of: :media_attachments, optional: true
  53. has_attached_file :file,
  54. styles: ->(f) { file_styles f },
  55. processors: ->(f) { file_processors f },
  56. convert_options: { all: '-quality 90 -strip' }
  57. validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
  58. validates_attachment_size :file, less_than: LIMIT
  59. remotable_attachment :file, LIMIT
  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. 'b:v' => '1300K',
  107. 'maxrate' => '500K',
  108. 'bufsize' => '1300K',
  109. 'crf' => 18,
  110. },
  111. },
  112. },
  113. }
  114. elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
  115. IMAGE_STYLES
  116. else
  117. VIDEO_STYLES
  118. end
  119. end
  120. def file_processors(f)
  121. if f.file_content_type == 'image/gif'
  122. [:gif_transcoder]
  123. elsif VIDEO_MIME_TYPES.include? f.file_content_type
  124. [:video_transcoder]
  125. else
  126. [:thumbnail]
  127. end
  128. end
  129. end
  130. private
  131. def set_shortcode
  132. self.type = :unknown if file.blank? && !type_changed?
  133. return unless local?
  134. loop do
  135. self.shortcode = SecureRandom.urlsafe_base64(14)
  136. break if MediaAttachment.find_by(shortcode: shortcode).nil?
  137. end
  138. end
  139. def prepare_description
  140. self.description = description.strip[0...420] unless description.nil?
  141. end
  142. def set_type_and_extension
  143. self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
  144. extension = appropriate_extension
  145. basename = Paperclip::Interpolations.basename(file, :original)
  146. file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
  147. end
  148. def set_meta
  149. meta = populate_meta
  150. return if meta == {}
  151. file.instance_write :meta, meta
  152. end
  153. def populate_meta
  154. meta = file.instance_read(:meta) || {}
  155. file.queued_for_write.each do |style, file|
  156. meta[style] = style == :small || image? ? image_geometry(file) : video_metadata(file)
  157. end
  158. meta
  159. end
  160. def image_geometry(file)
  161. width, height = FastImage.size(file.path)
  162. return {} if width.nil?
  163. {
  164. width: width,
  165. height: height,
  166. size: "#{width}x#{height}",
  167. aspect: width.to_f / height.to_f,
  168. }
  169. end
  170. def video_metadata(file)
  171. movie = FFMPEG::Movie.new(file.path)
  172. return {} unless movie.valid?
  173. {
  174. width: movie.width,
  175. height: movie.height,
  176. frame_rate: movie.frame_rate,
  177. duration: movie.duration,
  178. bitrate: movie.bitrate,
  179. }
  180. end
  181. def appropriate_extension
  182. mime_type = MIME::Types[file.content_type]
  183. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  184. original_extension = Paperclip::Interpolations.extension(file, :original)
  185. extensions_for_mime_type.include?(original_extension) ? original_extension : extensions_for_mime_type.first
  186. end
  187. end