闭社主体 forked from https://github.com/tootsuite/mastodon
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.

104 lines
3.0 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: preview_cards
  5. #
  6. # id :bigint(8) not null, primary key
  7. # url :string default(""), not null
  8. # title :string default(""), not null
  9. # description :string default(""), not null
  10. # image_file_name :string
  11. # image_content_type :string
  12. # image_file_size :integer
  13. # image_updated_at :datetime
  14. # type :integer default("link"), not null
  15. # html :text default(""), not null
  16. # author_name :string default(""), not null
  17. # author_url :string default(""), not null
  18. # provider_name :string default(""), not null
  19. # provider_url :string default(""), not null
  20. # width :integer default(0), not null
  21. # height :integer default(0), not null
  22. # created_at :datetime not null
  23. # updated_at :datetime not null
  24. # embed_url :string default(""), not null
  25. # image_storage_schema_version :integer
  26. # blurhash :string
  27. #
  28. class PreviewCard < ApplicationRecord
  29. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  30. LIMIT = 1.megabytes
  31. BLURHASH_OPTIONS = {
  32. x_comp: 4,
  33. y_comp: 4,
  34. }.freeze
  35. self.inheritance_column = false
  36. enum type: [:link, :photo, :video, :rich]
  37. has_and_belongs_to_many :statuses
  38. has_attached_file :image, processors: [:thumbnail, :blurhash_transcoder], styles: ->(f) { image_styles(f) }, convert_options: { all: '-quality 80 -strip' }
  39. include Attachmentable
  40. validates :url, presence: true, uniqueness: true
  41. validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
  42. validates_attachment_size :image, less_than: LIMIT
  43. remotable_attachment :image, LIMIT
  44. scope :cached, -> { where.not(image_file_name: [nil, '']) }
  45. before_save :extract_dimensions, if: :link?
  46. def local?
  47. false
  48. end
  49. def missing_image?
  50. width.present? && height.present? && image_file_name.blank?
  51. end
  52. def save_with_optional_image!
  53. save!
  54. rescue ActiveRecord::RecordInvalid
  55. self.image = nil
  56. save!
  57. end
  58. class << self
  59. private
  60. def image_styles(f)
  61. styles = {
  62. original: {
  63. geometry: '400x400>',
  64. file_geometry_parser: FastGeometryParser,
  65. convert_options: '-coalesce -strip',
  66. blurhash: BLURHASH_OPTIONS,
  67. },
  68. }
  69. styles[:original][:format] = 'jpg' if f.instance.image_content_type == 'image/gif'
  70. styles
  71. end
  72. end
  73. private
  74. def extract_dimensions
  75. file = image.queued_for_write[:original]
  76. return if file.nil?
  77. width, height = FastImage.size(file.path)
  78. return nil if width.nil?
  79. self.width = width
  80. self.height = height
  81. end
  82. end