FIX: `Post#each_upload_url` yields incorrect path to block when CDN is enabled.

FIX: Post#each_upload_url yields incorrect path to block when CDN is enabled.

diff --git a/app/models/post.rb b/app/models/post.rb
index c579d01..f9f565d 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -905,7 +905,7 @@ class Post < ActiveRecord::Base
       next unless Discourse.store.has_been_uploaded?(src) || (include_local_upload && src =~ /\A\/[^\/]/i)
 
       path = begin
-        URI(URI.unescape(src))&.path
+        URI(URI.unescape(GlobalSetting.cdn_url ? src.sub(GlobalSetting.cdn_url, "") : src))&.path
       rescue URI::Error
       end
 
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 77ce568..111f90e 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -1361,15 +1361,42 @@ describe Post do
   end
 
   describe '#each_upload_url' do
-    let(:upload) { Fabricate(:upload_s3) }
-
     it "correctly identifies all upload urls" do
-      urls = []
       upload1 = Fabricate(:upload)
       upload2 = Fabricate(:upload)
-      post = Fabricate(:post, raw: "A post with image and link upload.\n\n![](#{upload1.short_url})\n\n<a href='#{upload2.url}'>Link to upload</a>\n![](http://example.com/external.png)")
-      post.each_upload_url { |src, _, _| urls << src }
-      expect(urls).to eq([upload1.url, upload2.url])
+
+      set_cdn_url "https://awesome.com/somepath"
+
+      post = Fabricate(:post, raw: <<~RAW)
+      A post with image and link upload.
+
+      ![](#{upload1.short_url})
+
+      "#{GlobalSetting.cdn_url}#{upload1.url}"
+
+      <a href='#{Discourse.base_url}#{upload2.url}'>Link to upload</a>
+      ![](http://example.com/external.png)
+      RAW
+
+      urls = []
+      paths = []
+
+      post.each_upload_url do |src, path, _|
+        urls << src
+        paths << path
+      end
+
+      expect(urls).to contain_exactly(
+        upload1.url,
+        "#{GlobalSetting.cdn_url}#{upload1.url}",
+        "#{Discourse.base_url}#{upload2.url}"
+      )
+
+      expect(paths).to contain_exactly(
+        upload1.url,
+        upload1.url,
+        upload2.url
+      )
     end
 
     it "should skip external urls with upload url in query string" do

GitHub sha: 8a6ee090