From 2eefe6d5d61a5da68d280b340679e3c13adc1369 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 19 Dec 2018 11:20:48 +0200 Subject: [PATCH] FIX: Use CDN for logos and icons. (#6698) --- app/helpers/user_notifications_helper.rb | 4 +-- app/models/site_setting.rb | 26 +++++++++++++------ lib/global_path.rb | 6 +++++ spec/components/wizard/wizard_builder_spec.rb | 13 +++++++--- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/app/helpers/user_notifications_helper.rb b/app/helpers/user_notifications_helper.rb index 176dab9cb94..e3aed779556 100644 --- a/app/helpers/user_notifications_helper.rb +++ b/app/helpers/user_notifications_helper.rb @@ -23,9 +23,7 @@ module UserNotificationsHelper logo_url = SiteSetting.site_logo_url if logo_url.blank? || logo_url =~ /\.svg$/i return nil if logo_url.blank? || logo_url =~ /\.svg$/i - uri = URI.parse(UrlHelper.absolute(upload_cdn_path(logo_url))) - uri.scheme = SiteSetting.scheme if uri.scheme.blank? - uri.to_s + full_cdn_url(logo_url) end def html_site_link(color) diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 687f000f0a6..92252160a10 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -1,7 +1,9 @@ require 'site_setting_extension' +require_dependency 'global_path' require_dependency 'site_settings/yaml_loader' class SiteSetting < ActiveRecord::Base + extend GlobalPath extend SiteSettingExtension validates_presence_of :name @@ -176,35 +178,43 @@ class SiteSetting < ActiveRecord::Base }.each { |client_setting| client_settings << client_setting } def self.site_logo_url - self.logo&.url || self.logo_url(warn: false) + upload = self.logo + upload ? full_cdn_url(upload.url) : self.logo_url(warn: false) end def self.site_logo_small_url - self.logo_small&.url || self.logo_small_url(warn: false) + upload = self.logo_small + upload ? full_cdn_url(upload.url) : self.logo_small_url(warn: false) end def self.site_digest_logo_url - self.digest_logo&.url || self.digest_logo_url(warn: false) + upload = self.digest_logo + upload ? full_cdn_url(upload.url) : self.digest_logo_url(warn: false) end def self.site_mobile_logo_url - self.mobile_logo&.url || self.mobile_logo_url(warn: false) + upload = self.mobile_logo + upload ? full_cdn_url(upload.url) : self.mobile_logo_url(warn: false) end def self.site_large_icon_url - self.large_icon&.url || self.large_icon_url(warn: false) + upload = self.large_icon + upload ? full_cdn_url(upload.url) : self.large_icon_url(warn: false) end def self.site_favicon_url - self.favicon&.url || self.favicon_url(warn: false) + upload = self.favicon + upload ? full_cdn_url(upload.url) : self.favicon_url(warn: false) end def self.site_apple_touch_icon_url - self.apple_touch_icon&.url || self.apple_touch_icon_url(warn: false) + upload = self.apple_touch_icon + upload ? full_cdn_url(upload.url) : self.apple_touch_icon_url(warn: false) end def self.opengraph_image_url - self.opengraph_image&.url || self.default_opengraph_image_url(warn: false) + upload = self.opengraph_image + upload ? full_cdn_url(upload.url) : self.default_opengraph_image_url(warn: false) end def self.site_twitter_summary_large_image_url diff --git a/lib/global_path.rb b/lib/global_path.rb index cea5985079b..10d1fbdda56 100644 --- a/lib/global_path.rb +++ b/lib/global_path.rb @@ -22,4 +22,10 @@ module GlobalPath end end + def full_cdn_url(url) + uri = URI.parse(UrlHelper.absolute(upload_cdn_path(url))) + uri.scheme = SiteSetting.scheme if uri.scheme.blank? + uri.to_s + end + end diff --git a/spec/components/wizard/wizard_builder_spec.rb b/spec/components/wizard/wizard_builder_spec.rb index 78dc44aa4d5..bcdf257983b 100644 --- a/spec/components/wizard/wizard_builder_spec.rb +++ b/spec/components/wizard/wizard_builder_spec.rb @@ -1,6 +1,11 @@ require 'rails_helper' require 'wizard' require 'wizard/builder' +require 'global_path' + +class GlobalPathInstance + extend GlobalPath +end describe Wizard::Builder do let(:moderator) { Fabricate.build(:moderator) } @@ -49,9 +54,9 @@ describe Wizard::Builder do logo_small_field = fields.last expect(logo_field.id).to eq('logo') - expect(logo_field.value).to eq(upload.url) + expect(logo_field.value).to eq(GlobalPathInstance.full_cdn_url(upload.url)) expect(logo_small_field.id).to eq('logo_small') - expect(logo_small_field.value).to eq(upload2.url) + expect(logo_small_field.value).to eq(GlobalPathInstance.full_cdn_url(upload2.url)) end end @@ -70,9 +75,9 @@ describe Wizard::Builder do apple_touch_icon_field = fields.last expect(favicon_field.id).to eq('favicon') - expect(favicon_field.value).to eq(upload.url) + expect(favicon_field.value).to eq(GlobalPathInstance.full_cdn_url(upload.url)) expect(apple_touch_icon_field.id).to eq('apple_touch_icon') - expect(apple_touch_icon_field.value).to eq(upload2.url) + expect(apple_touch_icon_field.value).to eq(GlobalPathInstance.full_cdn_url(upload2.url)) end end