From b9a343afe7b54b687cd5b26bdf4951021d83a01c Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Fri, 12 Jan 2018 01:08:15 -0200 Subject: [PATCH] FIX: Allow shared CDN for s3 and assets --- app/models/upload.rb | 2 +- spec/models/upload_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index 4d2e396a064..0e776d52676 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -77,7 +77,7 @@ class Upload < ActiveRecord::Base def self.get_from_url(url) return if url.blank? # we store relative urls, so we need to remove any host/cdn - url = url.sub(Discourse.asset_host, "") if Discourse.asset_host.present? + url = url.sub(Discourse.asset_host, "") if Discourse.asset_host.present? && Discourse.asset_host != SiteSetting.Upload.s3_cdn_url # when using s3, we need to replace with the absolute base url url = url.sub(SiteSetting.Upload.s3_cdn_url, Discourse.store.absolute_base_url) if SiteSetting.Upload.s3_cdn_url.present? diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index b8803fda54a..e7d9fdde7a8 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -111,6 +111,22 @@ describe Upload do expect(Upload.get_from_url(URI.join(s3_cdn_url, path).to_s)).to eq(upload) end + + it "should return the right upload when using one CDN for both s3 and assets" do + begin + original_asset_host = Rails.configuration.action_controller.asset_host + cdn_url = 'http://my.cdn.com' + Rails.configuration.action_controller.asset_host = cdn_url + SiteSetting.s3_cdn_url = cdn_url + upload + + expect(Upload.get_from_url( + URI.join(cdn_url, path).to_s + )).to eq(upload) + ensure + Rails.configuration.action_controller.asset_host = original_asset_host + end + end end end