FIX: add support for version query parameter in InlineUploads
This commit is contained in:
parent
f0f03acb2c
commit
0cfe47471f
|
@ -6,7 +6,7 @@ class InlineUploads
|
||||||
PLACEHOLDER = "__replace__"
|
PLACEHOLDER = "__replace__"
|
||||||
PATH_PLACEHOLDER = "__replace_path__"
|
PATH_PLACEHOLDER = "__replace_path__"
|
||||||
|
|
||||||
UPLOAD_REGEXP_PATTERN = "/original/(\\dX/(?:[a-f0-9]/)*[a-f0-9]{40}[a-zA-Z0-9.]*)"
|
UPLOAD_REGEXP_PATTERN = "/original/(\\dX/(?:\\h/)*\\h{40}[a-zA-Z0-9.]*)(\\?v=\\d+)?"
|
||||||
private_constant :UPLOAD_REGEXP_PATTERN
|
private_constant :UPLOAD_REGEXP_PATTERN
|
||||||
|
|
||||||
def self.process(markdown, on_missing: nil)
|
def self.process(markdown, on_missing: nil)
|
||||||
|
@ -26,7 +26,6 @@ class InlineUploads
|
||||||
# Do nothing
|
# Do nothing
|
||||||
elsif !(node.children.count == 1 && (node.children[0].name != "img" && node.children[0].children.blank?)) &&
|
elsif !(node.children.count == 1 && (node.children[0].name != "img" && node.children[0].children.blank?)) &&
|
||||||
!(node.name == "a" && node.children.count > 1 && !node_children_names(node).include?("img"))
|
!(node.name == "a" && node.children.count > 1 && !node_children_names(node).include?("img"))
|
||||||
|
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,7 +63,7 @@ class InlineUploads
|
||||||
]
|
]
|
||||||
|
|
||||||
if Discourse.store.external?
|
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})/
|
regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -72,8 +71,8 @@ class InlineUploads
|
||||||
indexes = Set.new
|
indexes = Set.new
|
||||||
|
|
||||||
markdown.scan(/(\n{2,}|\A)#{regexp}$/) do |match|
|
markdown.scan(/(\n{2,}|\A)#{regexp}$/) do |match|
|
||||||
if match[1].present?
|
if match[1].present? && match[2].present?
|
||||||
extension = match[1].split(".")[-1].downcase
|
extension = match[2].split(".")[-1].downcase
|
||||||
index = $~.offset(2)[0]
|
index = $~.offset(2)[0]
|
||||||
indexes << index
|
indexes << index
|
||||||
if FileHelper.supported_images.include?(extension)
|
if FileHelper.supported_images.include?(extension)
|
||||||
|
@ -87,36 +86,20 @@ class InlineUploads
|
||||||
markdown.scan(/^#{regexp}(\s)/) do |match|
|
markdown.scan(/^#{regexp}(\s)/) do |match|
|
||||||
if match[0].present?
|
if match[0].present?
|
||||||
index = $~.offset(0)[0]
|
index = $~.offset(0)[0]
|
||||||
next if indexes.include?(index)
|
next if !indexes.add?(index)
|
||||||
indexes << index
|
raw_matches << [match[0], match[0], +"#{Discourse.base_url}#{PATH_PLACEHOLDER}", index]
|
||||||
|
|
||||||
raw_matches << [
|
|
||||||
match[0],
|
|
||||||
match[0],
|
|
||||||
+"#{Discourse.base_url}#{PATH_PLACEHOLDER}",
|
|
||||||
$~.offset(0)[0]
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
markdown.scan(/\[[^\[\]]*\]: #{regexp}/) do |match|
|
markdown.scan(/\[[^\[\]]*\]: #{regexp}/) do |match|
|
||||||
if match[0].present?
|
indexes.add($~.offset(1)[0]) if match[0].present?
|
||||||
index = $~.offset(1)[0]
|
|
||||||
next if indexes.include?(index)
|
|
||||||
indexes << index
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
markdown.scan(/(([\n\s\)\]\<])+)#{regexp}/) do |match|
|
markdown.scan(/(([\n\s\)\]\<])+)#{regexp}/) do |match|
|
||||||
if matched_uploads(match[2]).present?
|
if matched_uploads(match[2]).present?
|
||||||
next if indexes.include?($~.offset(3)[0])
|
next if !indexes.add?($~.offset(3)[0])
|
||||||
|
index = $~.offset(0)[0]
|
||||||
raw_matches << [
|
raw_matches << [match[2], match[2], +"#{Discourse.base_url}#{PATH_PLACEHOLDER}", index]
|
||||||
match[2],
|
|
||||||
match[2],
|
|
||||||
+"#{Discourse.base_url}#{PATH_PLACEHOLDER}",
|
|
||||||
$~.offset(0)[0]
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -160,7 +143,7 @@ class InlineUploads
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
markdown.scan(/(__([a-f0-9]{40})__)/) do |match|
|
markdown.scan(/(__(\h{40})__)/) do |match|
|
||||||
upload = Upload.find_by(sha1: match[1])
|
upload = Upload.find_by(sha1: match[1])
|
||||||
markdown = markdown.sub(match[0], upload.short_path)
|
markdown = markdown.sub(match[0], upload.short_path)
|
||||||
end
|
end
|
||||||
|
@ -238,15 +221,8 @@ class InlineUploads
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.matched_uploads(node)
|
def self.matched_uploads(node)
|
||||||
matches = []
|
|
||||||
|
|
||||||
base_url = Discourse.base_url.sub(/https?:\/\//, "(https?://)")
|
|
||||||
|
|
||||||
if GlobalSetting.cdn_url
|
|
||||||
cdn_url = GlobalSetting.cdn_url.sub(/https?:\/\//, "(https?://)")
|
|
||||||
end
|
|
||||||
|
|
||||||
db = RailsMultisite::ConnectionManagement.current_db
|
db = RailsMultisite::ConnectionManagement.current_db
|
||||||
|
base_url = Discourse.base_url.sub(/https?:\/\//, "(https?://)")
|
||||||
|
|
||||||
regexps = [
|
regexps = [
|
||||||
/(upload:\/\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
|
/(upload:\/\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
|
||||||
|
@ -256,11 +232,10 @@ class InlineUploads
|
||||||
/(#{base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/,
|
/(#{base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/,
|
||||||
]
|
]
|
||||||
|
|
||||||
if cdn_url
|
if GlobalSetting.cdn_url && (cdn_url = GlobalSetting.cdn_url.sub(/https?:\/\//, "(https?://)"))
|
||||||
regexps << /(#{cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
regexps << /(#{cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
||||||
if GlobalSetting.relative_url_root.present?
|
if GlobalSetting.relative_url_root.present?
|
||||||
regexps << /(#{cdn_url}#{GlobalSetting.relative_url_root}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
regexps << /(#{cdn_url}#{GlobalSetting.relative_url_root}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -274,6 +249,7 @@ class InlineUploads
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
matches = []
|
||||||
node = node.to_s
|
node = node.to_s
|
||||||
|
|
||||||
regexps.each do |regexp|
|
regexps.each do |regexp|
|
||||||
|
|
|
@ -220,6 +220,32 @@ RSpec.describe InlineUploads do
|
||||||
MD
|
MD
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should correct image URLs with v parameters" do
|
||||||
|
md = <<~MD
|
||||||
|
<img src="#{upload.url}?v=1">
|
||||||
|
|
||||||
|
<img src="#{Discourse.base_url}#{upload.url}?v=2">
|
||||||
|
|
||||||
|
<img src="#{GlobalSetting.cdn_url}#{upload.url}?v=3">
|
||||||
|
|
||||||
|
#{Discourse.base_url}#{upload.url}?v=45
|
||||||
|
|
||||||
|
#{GlobalSetting.cdn_url}#{upload.url}?v=999
|
||||||
|
MD
|
||||||
|
|
||||||
|
expect(InlineUploads.process(md)).to eq(<<~MD)
|
||||||
|
![](#{upload.short_url})
|
||||||
|
|
||||||
|
![](#{upload.short_url})
|
||||||
|
|
||||||
|
![](#{upload.short_url})
|
||||||
|
|
||||||
|
![](#{upload.short_url})
|
||||||
|
|
||||||
|
![](#{upload.short_url})
|
||||||
|
MD
|
||||||
|
end
|
||||||
|
|
||||||
context "subfolder" do
|
context "subfolder" do
|
||||||
before do
|
before do
|
||||||
global_setting :relative_url_root, "/community"
|
global_setting :relative_url_root, "/community"
|
||||||
|
|
Loading…
Reference in New Issue