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

This commit is contained in:
Guo Xiang Tan 2019-07-31 10:00:52 +08:00
parent 43365a2bf1
commit 8a6ee09008
2 changed files with 34 additions and 7 deletions

View File

@ -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

View File

@ -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