DEV: skip S3 CDN urls with different path in prefix. (#14488)

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.

diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb
index 1c9f7e5..2e63681 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 5c5eb12..879eca2 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 = "<img src='https://cdn.example.com/site1/original/1X/bc68acbc8c022726e69f980e00d6811212r.jpg' /><img src='https://cdn.example.com/site2/original/1X/bc68acbc8c022726e69f980e00d68112128.jpg' />"
+      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

GitHub sha: c8d5c049eb0c59cd9c274ad1f891f09680b24273

This commit appears in #14488 which was approved by tgxworld. It was merged by vinothkannans.