From 9919f16041da1d389ab7efbfcf571d920666603f Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Tue, 8 Jan 2019 11:17:05 +0530 Subject: [PATCH] FIX: use absolute URL for twitter:image tag --- app/helpers/application_helper.rb | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cd3ed044537..82a5d5fbae6 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -210,17 +210,10 @@ module ApplicationHelper opts[:image] = SiteSetting.site_apple_touch_icon_url end - # Use the correct scheme for open graph image - if opts[:image].present? - if opts[:image].start_with?("//") - uri = URI(Discourse.base_url) - opts[:image] = "#{uri.scheme}:#{opts[:image]}" - elsif opts[:image].start_with?("/uploads/") - opts[:image] = "#{Discourse.base_url}#{opts[:image]}" - elsif GlobalSetting.relative_url_root && opts[:image].start_with?(GlobalSetting.relative_url_root) - opts[:image] = "#{Discourse.base_url_no_prefix}#{opts[:image]}" - end - end + # Use the correct scheme for opengraph/twitter image + opts[:image] = get_absolute_image_url(opts[:image]) if opts[:image].present? + opts[:twitter_summary_large_image] = + get_absolute_image_url(opts[:twitter_summary_large_image]) if opts[:twitter_summary_large_image].present? # Add opengraph & twitter tags result = [] @@ -452,4 +445,17 @@ module ApplicationHelper setup_data end + + def get_absolute_image_url(link) + absolute_url = link + if link.start_with?("//") + uri = URI(Discourse.base_url) + absolute_url = "#{uri.scheme}:#{link}" + elsif link.start_with?("/uploads/") + absolute_url = "#{Discourse.base_url}#{link}" + elsif GlobalSetting.relative_url_root && link.start_with?(GlobalSetting.relative_url_root) + absolute_url = "#{Discourse.base_url_no_prefix}#{link}" + end + absolute_url + end end