From dcd0270d5211de2134dd3cc08b3f686f73b7e158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 6 Jan 2016 21:54:01 +0100 Subject: [PATCH] FIX: don't use the CDN for attachments when login is required --- lib/cooked_post_processor.rb | 8 ++++++++ spec/components/cooked_post_processor_spec.rb | 13 ++++++++++--- spec/fabricators/post_fabricator.rb | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 8b984b78fec..147824d829f 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -276,6 +276,14 @@ class CookedPostProcessor end def optimize_urls + # when login is required, attachments can't be on the CDN + if SiteSetting.login_required + @doc.css("a.attachment[href]").each do |a| + href = a["href"].to_s + a["href"] = UrlHelper.schemaless UrlHelper.absolute(href, nil) if UrlHelper.is_local(href) + end + end + %w{href data-download-href}.each do |selector| @doc.css("a[#{selector}]").each do |a| href = a["#{selector}"].to_s diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 2511927c37b..89d0c51e0a1 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -344,7 +344,7 @@ describe CookedPostProcessor do it "uses schemaless url for uploads" do cpp.optimize_urls - expect(cpp.html).to match_html '

Link

Google

' + expect(cpp.html).to match_html '

Link

Google

text.txt (20 Bytes)

' end context "when CDN is enabled" do @@ -352,13 +352,20 @@ describe CookedPostProcessor do it "does use schemaless CDN url for http uploads" do Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com") cpp.optimize_urls - expect(cpp.html).to match_html '

Link

Google

' + expect(cpp.html).to match_html '

Link

Google

text.txt (20 Bytes)

' end it "does not use schemaless CDN url for https uploads" do Rails.configuration.action_controller.stubs(:asset_host).returns("https://my.cdn.com") cpp.optimize_urls - expect(cpp.html).to match_html '

Link

Google

' + expect(cpp.html).to match_html '

Link

Google

text.txt (20 Bytes)

' + end + + it "does not use CDN when login is required" do + SiteSetting.login_required = true + Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com") + cpp.optimize_urls + expect(cpp.html).to match_html '

Link

Google

text.txt (20 Bytes)

' end end diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb index fcd53d92de8..43c479fabf4 100644 --- a/spec/fabricators/post_fabricator.rb +++ b/spec/fabricators/post_fabricator.rb @@ -96,6 +96,7 @@ Fabricator(:post_with_uploads_and_links, from: :post) do Google +text.txt (20 Bytes) ' end