FIX: Cover more edge cases in InlineUploads.

This commit is contained in:
Guo Xiang Tan 2019-06-12 16:41:44 +08:00
parent 739696fdf0
commit 641521896c
2 changed files with 58 additions and 25 deletions

View File

@ -22,12 +22,11 @@ class InlineUploads
end
if seen_link = matched_uploads(node).first
link_occurences <<
if (actual_link = (node.attributes["href"]&.value || node.attributes["src"]&.value))
{ link: actual_link, is_valid: true }
else
{ link: seen_link, is_valid: false }
end
if (actual_link = (node.attributes["href"]&.value || node.attributes["src"]&.value))
link_occurences << { link: actual_link, is_valid: true }
elsif node.name != "p"
link_occurences << { link: actual_link, is_valid: false }
end
end
end
@ -60,7 +59,7 @@ class InlineUploads
]
if Discourse.store.external?
regexps << /(https?:#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
end
@ -235,28 +234,31 @@ class InlineUploads
regexps = [
/(upload:\/\/([a-zA-Z0-9]+)[a-z0-9\.]*)/,
/(\/uploads\/short-url\/([a-zA-Z0-9]+)[a-z0-9\.]*)/,
/(#{Discourse.base_url}\/uploads\/short-url\/([a-zA-Z0-9]+)[a-z0-9\.]*)/,
]
db = RailsMultisite::ConnectionManagement.current_db
if Discourse.store.external?
if Rails.configuration.multisite
regexps << /(#{SiteSetting.Upload.s3_base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{SiteSetting.Upload.s3_cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
else
regexps << /(#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{Discourse.base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
end
else
regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
regexps << /(#{Discourse.base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
end
node = node.to_s
regexps.each do |regexp|
node.scan(regexp) do |matched|
matches << matched[0]
node.scan(/(^|[\n\s"'\(>])#{regexp}($|[\n\s"'\)<])/) do |matched|
matches << matched[1]
end
end

View File

@ -60,26 +60,55 @@ RSpec.describe InlineUploads do
end
it "should not correct code blocks" do
md = "`<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>`"
md = <<~MD
`<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>`
expect(InlineUploads.process(md)).to eq(md)
<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>
```
<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>
```
a [code]<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>[/code] b
[code]
<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>
[/code]
MD
md = " <a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>"
expect(InlineUploads.process(md)).to eq(md)
end
it "should not correct links in quotes" do
it "should not correct invalid links in quotes" do
post = Fabricate(:post)
user = Fabricate(:user)
md = <<~MD
[quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"]
<img src="#{upload.url}"
someothertext#{upload2.url}someothertext
<img src="#{upload.url}"
sometext#{upload2.url}sometext
#{upload3.url}
#{Discourse.base_url}#{upload3.url}
[/quote]
<img src="#{upload2.url}">
MD
expect(InlineUploads.process(md)).to eq(<<~MD)
[quote="#{user.username}, post:#{post.post_number}, topic:#{post.topic.id}"]
<img src="#{upload.url}"
someothertext#{upload2.url}someothertext
<img src="#{upload.url}"
sometext#{upload2.url}sometext
#{upload3.url}
![](#{upload3.short_url})
[/quote]
![](#{upload2.short_url})
MD
end
it "should correct links in quotes" do
post = Fabricate(:post)
user = Fabricate(:user)
@ -472,11 +501,13 @@ RSpec.describe InlineUploads do
it "should correct image URLs to the short version" do
md = <<~MD
#{upload.url}
<img src="#{upload.url}" alt="some image">
<img src="#{URI.join(SiteSetting.s3_cdn_url, URI.parse(upload2.url).path).to_s}" alt="some image">
MD
expect(InlineUploads.process(md)).to eq(<<~MD)
![](#{upload.short_url})
![some image](#{upload.short_url})
![some image](#{upload2.short_url})
MD