Updated PreviewCard, FetchLinkCardService, FetchOEmbedService

• Updated:
- PreviewCard, FetchLinkCardService, FetchOEmbedService to work better
This commit is contained in:
mgabdev 2020-07-22 13:51:08 -05:00
parent 8f4be991a0
commit 7aa7b66374
3 changed files with 8 additions and 4 deletions

View File

@ -26,7 +26,7 @@
class PreviewCard < ApplicationRecord
IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
LIMIT = 1.megabytes
LIMIT = 4.megabytes
self.inheritance_column = false
@ -45,6 +45,10 @@ class PreviewCard < ApplicationRecord
before_save :extract_dimensions, if: :link?
def missing_image?
width.present? && height.present? && image_file_name.blank?
end
def save_with_optional_image!
save!
rescue ActiveRecord::RecordInvalid

View File

@ -30,7 +30,7 @@ class FetchLinkCardService < BaseService
RedisLock.acquire(lock_options) do |lock|
if lock.acquired?
@card = PreviewCard.find_by(url: @url)
process_url if @card.nil? || @card.updated_at <= 2.weeks.ago
process_url if @card.nil? || @card.updated_at <= 1.weeks.ago || @card.missing_image?
else
raise GabSocial::RaceConditionError
end
@ -47,7 +47,7 @@ class FetchLinkCardService < BaseService
def process_url
@card ||= PreviewCard.new(url: @url)
Request.new(:get, @url).perform do |res|
Request.new(:get, @url).add_headers('Accept' => 'text/html').perform do |res|
if res.code == 200 && res.mime_type == 'text/html'
@html = res.body_with_limit
@html_charset = res.charset

View File

@ -64,7 +64,7 @@ class FetchOEmbedService
def html
return @html if defined?(@html)
@html = @options[:html] || Request.new(:get, @url).perform do |res|
@html = @options[:html] || Request.new(:get, @url).add_headers('Accept' => 'text/html').perform do |res|
res.code != 200 || res.mime_type != 'text/html' ? nil : res.body_with_limit
end
end