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.

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