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.

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