From c8d5c049eb0c59cd9c274ad1f891f09680b24273 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Fri, 1 Oct 2021 12:25:17 +0530 Subject: [PATCH] DEV: skip S3 CDN urls with different path in prefix. (#14488) Previously, while retrieving each upload urls in a post S3 CDN urls with different path in prefix (external urls technically) are considered as uploaded url. It created issue while checking missing uploads. --- lib/file_store/s3_store.rb | 7 +++++-- spec/models/post_spec.rb | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb index 1c9f7e56ab..2e63681821 100644 --- a/lib/file_store/s3_store.rb +++ b/lib/file_store/s3_store.rb @@ -158,8 +158,11 @@ module FileStore end return false if SiteSetting.Upload.s3_cdn_url.blank? - cdn_hostname = URI.parse(SiteSetting.Upload.s3_cdn_url || "").hostname - return true if cdn_hostname.presence && url[cdn_hostname] + + s3_cdn_url = URI.parse(SiteSetting.Upload.s3_cdn_url || "") + cdn_hostname = s3_cdn_url.hostname + + return true if cdn_hostname.presence && url[cdn_hostname] && (s3_cdn_url.path.blank? || parsed_url.path.starts_with?(s3_cdn_url.path)) false end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 5c5eb12a2d..879eca29e1 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -1715,6 +1715,17 @@ describe Post do post.each_upload_url { |src, _, _| urls << src } expect(urls).to be_empty end + + it "skip S3 cdn urls with different path" do + setup_s3 + SiteSetting.Upload.stubs(:s3_cdn_url).returns("https://cdn.example.com/site1") + + urls = [] + raw = "" + post = Fabricate(:post, raw: raw) + post.each_upload_url { |src, _, _| urls << src } + expect(urls).to contain_exactly("https://cdn.example.com/site1/original/1X/bc68acbc8c022726e69f980e00d6811212r.jpg") + end end describe "#publish_changes_to_client!" do