A platform for community discussion. Free, open, simple.
FIX: add support for version query parameter in InlineUploads
Régis Hanol0cfe47471f30
diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rbindex f16f4e5110a..66fead79211 100644--- a/app/services/inline_uploads.rb+++ b/app/services/inline_uploads.rb@@ -6,7 +6,7 @@ class InlineUploadsPLACEHOLDER = "__replace__"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_PATTERNdef self.process(markdown, on_missing: nil)@@ -26,7 +26,6 @@ class InlineUploads# Do nothingelsif !(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"))-nextend@@ -64,7 +63,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@@ -72,8 +71,8 @@ class InlineUploadsindexes = Set.newmarkdown.scan(/(\n{2,}|\A)#{regexp}$/) do |match|- if match[1].present?- extension = match[1].split(".")[-1].downcase+ if match[1].present? && match[2].present?+ extension = match[2].split(".")[-1].downcaseindex = $~.offset(2)[0]indexes << indexif FileHelper.supported_images.include?(extension)@@ -87,36 +86,20 @@ class InlineUploadsmarkdown.scan(/^#{regexp}(\s)/) do |match|if match[0].present?index = $~.offset(0)[0]- next if indexes.include?(index)- indexes << index-- raw_matches << [- match[0],- match[0],- +"#{Discourse.base_url}#{PATH_PLACEHOLDER}",- $~.offset(0)[0]- ]+ next if !indexes.add?(index)+ raw_matches << [match[0], match[0], +"#{Discourse.base_url}#{PATH_PLACEHOLDER}", index]endendmarkdown.scan(/\[[^\[\]]*\]: #{regexp}/) do |match|- if match[0].present?- index = $~.offset(1)[0]- next if indexes.include?(index)- indexes << index- end+ indexes.add($~.offset(1)[0]) if match[0].present?endmarkdown.scan(/(([\n\s\)\]\<])+)#{regexp}/) do |match|if matched_uploads(match[2]).present?- next if indexes.include?($~.offset(3)[0])-- raw_matches << [- match[2],- match[2],- +"#{Discourse.base_url}#{PATH_PLACEHOLDER}",- $~.offset(0)[0]- ]+ next if !indexes.add?($~.offset(3)[0])+ index = $~.offset(0)[0]+ raw_matches << [match[2], match[2], +"#{Discourse.base_url}#{PATH_PLACEHOLDER}", index]endendend@@ -160,7 +143,7 @@ class InlineUploadsendend- markdown.scan(/(__([a-f0-9]{40})__)/) do |match|+ markdown.scan(/(__(\h{40})__)/) do |match|upload = Upload.find_by(sha1: match[1])markdown = markdown.sub(match[0], upload.short_path)end@@ -238,15 +221,8 @@ class InlineUploadsenddef 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+ base_url = Discourse.base_url.sub(/https?:\/\//, "(https?://)")regexps = [/(upload:\/\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,@@ -256,11 +232,10 @@ class InlineUploads/(#{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})/if GlobalSetting.relative_url_root.present?regexps << /(#{cdn_url}#{GlobalSetting.relative_url_root}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/-endend@@ -274,6 +249,7 @@ class InlineUploadsendend+ matches = []node = node.to_sregexps.each do |regexp|diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rbindex ea55a82352b..61700733af2 100644--- a/spec/services/inline_uploads_spec.rb+++ b/spec/services/inline_uploads_spec.rb@@ -220,6 +220,32 @@ RSpec.describe InlineUploads doMDend+ 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)+ ++ ++ ++ ++ + MD+ end+context "subfolder" dobefore doglobal_setting :relative_url_root, "/community"