diff --git a/lib/url_helper.rb b/lib/url_helper.rb index 0c69d287820..b156013295e 100644 --- a/lib/url_helper.rb +++ b/lib/url_helper.rb @@ -71,7 +71,11 @@ class UrlHelper def self.local_cdn_url(url) return url if Discourse.asset_host.blank? - url.sub(Discourse.base_url_no_prefix, Discourse.asset_host) + if url.start_with?("/#{Discourse.store.upload_path}/") + "#{Discourse.asset_host}#{url}" + else + url.sub(Discourse.base_url_no_prefix, Discourse.asset_host) + end end end diff --git a/spec/components/url_helper_spec.rb b/spec/components/url_helper_spec.rb index 53da4a688db..77ae9442499 100644 --- a/spec/components/url_helper_spec.rb +++ b/spec/components/url_helper_spec.rb @@ -119,4 +119,15 @@ describe UrlHelper do end end + describe "#local_cdn_url" do + let(:url) { "/uploads/default/1X/575bcc2886bf7a39684b57ca90be85f7d399bbc7.png" } + let(:asset_host) { "//my.awesome.cdn" } + + it "should return correct cdn url for local relative urls" do + Discourse.stubs(:asset_host).returns(asset_host) + cdn_url = UrlHelper.local_cdn_url(url) + expect(cdn_url).to eq("#{asset_host}#{url}") + end + end + end