preview_card.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. # rubocop:disable Naming/MethodParameterName
  61. def image_styles(f)
  62. styles = {
  63. original: {
  64. geometry: '400x400>',
  65. file_geometry_parser: FastGeometryParser,
  66. convert_options: '-coalesce -strip',
  67. blurhash: BLURHASH_OPTIONS,
  68. },
  69. }
  70. styles[:original][:format] = 'jpg' if f.instance.image_content_type == 'image/gif'
  71. styles
  72. end
  73. # rubocop:enable Naming/MethodParameterName
  74. end
  75. private
  76. def extract_dimensions
  77. file = image.queued_for_write[:original]
  78. return if file.nil?
  79. width, height = FastImage.size(file.path)
  80. return nil if width.nil?
  81. self.width = width
  82. self.height = height
  83. end
  84. end