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.

181 lines
4.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. # account_id :integer
  14. # created_at :datetime not null
  15. # updated_at :datetime not null
  16. # shortcode :string
  17. # type :integer default("image"), not null
  18. # file_meta :json
  19. #
  20. require 'mime/types'
  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 = { original: '1280x1280>', small: '400x400>' }.freeze
  29. VIDEO_STYLES = {
  30. small: {
  31. convert_options: {
  32. output: {
  33. vf: 'scale=\'min(400\, iw):min(400\, ih)\':force_original_aspect_ratio=decrease',
  34. },
  35. },
  36. format: 'png',
  37. time: 0,
  38. },
  39. }.freeze
  40. belongs_to :account, inverse_of: :media_attachments
  41. belongs_to :status, inverse_of: :media_attachments
  42. has_attached_file :file,
  43. styles: ->(f) { file_styles f },
  44. processors: ->(f) { file_processors f },
  45. convert_options: { all: '-quality 90 -strip' }
  46. include Remotable
  47. validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
  48. validates_attachment_size :file, less_than: 8.megabytes
  49. validates :account, presence: true
  50. scope :attached, -> { where.not(status_id: nil) }
  51. scope :unattached, -> { where(status_id: nil) }
  52. scope :local, -> { where(remote_url: '') }
  53. scope :remote, -> { where.not(remote_url: '') }
  54. default_scope { order(id: :asc) }
  55. def local?
  56. remote_url.blank?
  57. end
  58. def needs_redownload?
  59. file.blank? && remote_url.present?
  60. end
  61. def to_param
  62. shortcode
  63. end
  64. before_create :set_shortcode
  65. before_post_process :set_type_and_extension
  66. before_save :set_meta
  67. class << self
  68. private
  69. def file_styles(f)
  70. if f.instance.file_content_type == 'image/gif'
  71. {
  72. small: IMAGE_STYLES[:small],
  73. original: {
  74. format: 'mp4',
  75. convert_options: {
  76. output: {
  77. 'movflags' => 'faststart',
  78. 'pix_fmt' => 'yuv420p',
  79. 'vf' => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
  80. 'vsync' => 'cfr',
  81. 'b:v' => '1300K',
  82. 'maxrate' => '500K',
  83. 'bufsize' => '1300K',
  84. 'crf' => 18,
  85. },
  86. },
  87. },
  88. }
  89. elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
  90. IMAGE_STYLES
  91. else
  92. VIDEO_STYLES
  93. end
  94. end
  95. def file_processors(f)
  96. if f.file_content_type == 'image/gif'
  97. [:gif_transcoder]
  98. elsif VIDEO_MIME_TYPES.include? f.file_content_type
  99. [:video_transcoder]
  100. else
  101. [:thumbnail]
  102. end
  103. end
  104. end
  105. private
  106. def set_shortcode
  107. self.type = :unknown if file.blank? && !type_changed?
  108. return unless local?
  109. loop do
  110. self.shortcode = SecureRandom.urlsafe_base64(14)
  111. break if MediaAttachment.find_by(shortcode: shortcode).nil?
  112. end
  113. end
  114. def set_type_and_extension
  115. self.type = VIDEO_MIME_TYPES.include?(file_content_type) ? :video : :image
  116. extension = appropriate_extension
  117. basename = Paperclip::Interpolations.basename(file, :original)
  118. file.instance_write :file_name, [basename, extension].delete_if(&:blank?).join('.')
  119. end
  120. def set_meta
  121. meta = populate_meta
  122. return if meta == {}
  123. file.instance_write :meta, meta
  124. end
  125. def populate_meta
  126. meta = {}
  127. file.queued_for_write.each do |style, file|
  128. begin
  129. geo = Paperclip::Geometry.from_file file
  130. meta[style] = {
  131. width: geo.width.to_i,
  132. height: geo.height.to_i,
  133. size: "#{geo.width.to_i}x#{geo.height.to_i}",
  134. aspect: geo.width.to_f / geo.height.to_f,
  135. }
  136. rescue Paperclip::Errors::NotIdentifiedByImageMagickError
  137. meta[style] = {}
  138. end
  139. end
  140. meta
  141. end
  142. def appropriate_extension
  143. mime_type = MIME::Types[file.content_type]
  144. extensions_for_mime_type = mime_type.empty? ? [] : mime_type.first.extensions
  145. original_extension = Paperclip::Interpolations.extension(file, :original)
  146. extensions_for_mime_type.include?(original_extension) ? original_extension : extensions_for_mime_type.first
  147. end
  148. end