docbrown/app/view_objects/articles/social_image.rb
Ben Halpern ec7adf56fe
Allow admins to set cover image height configs (#19936)
* Initial basic work

* Bulk of related work, including find/replace on the inputs

* Adjust some tests

* Adjust some specs

* Fix a few more tests

* Clean up tests

* Adjust tests

* Test fiddle

* Adjust crop back to be a param

* Update tests

* Set proper defaults

* Fix some styling

* Adjust enrichment logic and tests

* Adjust form JS

* Update test snapshot

* Clean up formatting

* Fix spec name

* Adjust some css and defaults

* Adjust translation for image provider options

* Switch from fill to fill-down

* Proper fallback image

* Fix tests

* Update app/services/images/optimizer.rb

Co-authored-by: Mac Siri <mac@forem.com>

---------

Co-authored-by: Mac Siri <mac@forem.com>
2023-08-21 13:25:16 -04:00

49 lines
1.5 KiB
Ruby

module Articles
class SocialImage
include Rails.application.routes.url_helpers
SOCIAL_PREVIEW_MIGRATION_DATETIME = Time.zone.parse("2019-04-22T00:00:00Z")
def initialize(article, height: 500, width: 1000)
@article = article
@height = height
@width = width
end
def url
image = user_defined_image
if image.present?
image = image.split("w_1000/").last if image.include?("w_1000/https://")
return Images::Optimizer.call(image, width: width, height: height, crop: "crop")
end
return legacy_article_social_image unless use_new_social_url?
article_social_preview_url(article, format: :png, host: Settings::General.app_domain)
end
private
attr_reader :article, :height, :width
def legacy_article_social_image
cache_key = "article-social-img-#{article}-#{article.updated_at.rfc3339}-#{article.comments_count}"
Rails.cache.fetch(cache_key, expires_in: 1.hour) do
src = Images::GenerateSocialImage.call(article)
return src if src.start_with? "https://res.cloudinary.com/"
Images::Optimizer.call(src, width: "1000", height: "500", crop: "crop")
end
end
def use_new_social_url?
article.updated_at > SOCIAL_PREVIEW_MIGRATION_DATETIME
end
def user_defined_image
return article.main_image if article.main_image.present?
return article.social_image if article.social_image.present?
return article.video_thumbnail_url if article.video_thumbnail_url.present?
end
end
end