2019-05-30 02:38:46 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_dependency "pretty_text"
|
|
|
|
|
|
|
|
class InlineUploads
|
2019-06-03 03:41:26 -04:00
|
|
|
PLACEHOLDER = "__replace__"
|
2019-06-06 03:50:16 -04:00
|
|
|
PATH_PLACEHOLDER = "__replace_path__"
|
2019-06-03 03:41:26 -04:00
|
|
|
|
2019-06-12 23:18:49 -04:00
|
|
|
UPLOAD_REGEXP_PATTERN = "/original/(\\dX/(?:[a-f0-9]/)*[a-f0-9]{40}[a-zA-Z0-9.]*)"
|
2019-06-03 03:41:26 -04:00
|
|
|
private_constant :UPLOAD_REGEXP_PATTERN
|
|
|
|
|
2019-05-30 02:38:46 -04:00
|
|
|
def self.process(markdown, on_missing: nil)
|
|
|
|
markdown = markdown.dup
|
2019-06-13 00:08:01 -04:00
|
|
|
|
|
|
|
match_md_reference(markdown) do |match, src, replacement, index|
|
|
|
|
if upload = Upload.get_from_url(src)
|
|
|
|
markdown = markdown.sub(match, replacement.sub!(PATH_PLACEHOLDER, "__#{upload.sha1}__"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
cooked_fragment = Nokogiri::HTML::fragment(PrettyText.cook(markdown, disable_emojis: true))
|
2019-05-30 02:38:46 -04:00
|
|
|
link_occurences = []
|
|
|
|
|
|
|
|
cooked_fragment.traverse do |node|
|
|
|
|
if node.name == "img"
|
|
|
|
# Do nothing
|
2019-06-18 23:14:46 -04:00
|
|
|
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"))
|
|
|
|
|
2019-05-30 02:38:46 -04:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if seen_link = matched_uploads(node).first
|
2019-06-12 04:41:44 -04:00
|
|
|
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
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-03 03:41:26 -04:00
|
|
|
raw_matches = []
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
match_bbcode_img(markdown) do |match, src, replacement, index|
|
|
|
|
raw_matches << [match, src, replacement, index]
|
2019-06-03 03:41:26 -04:00
|
|
|
end
|
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
match_md_inline_img(markdown) do |match, src, replacement, index|
|
|
|
|
raw_matches << [match, src, replacement, index]
|
2019-06-03 03:41:26 -04:00
|
|
|
end
|
2019-05-30 02:38:46 -04:00
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
match_img(markdown) do |match, src, replacement, index|
|
|
|
|
raw_matches << [match, src, replacement, index]
|
|
|
|
end
|
2019-06-03 03:41:26 -04:00
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
match_anchor(markdown) do |match, href, replacement, index|
|
|
|
|
raw_matches << [match, href, replacement, index]
|
2019-06-03 03:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
regexps = [
|
2019-06-06 23:46:52 -04:00
|
|
|
/(https?:\/\/[a-zA-Z0-9\.\/-]+\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/,
|
2019-06-03 03:41:26 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
if Discourse.store.external?
|
2019-06-12 04:41:44 -04:00
|
|
|
regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
|
2019-06-06 23:46:52 -04:00
|
|
|
regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
|
2019-06-03 03:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
regexps.each do |regexp|
|
2019-06-06 23:46:52 -04:00
|
|
|
indexes = Set.new
|
|
|
|
|
|
|
|
markdown.scan(/(\n{2,}|\A)#{regexp}$/) do |match|
|
|
|
|
if match[1].present?
|
2019-07-16 08:35:17 -04:00
|
|
|
extension = match[1].split(".")[-1].downcase
|
2019-06-06 23:46:52 -04:00
|
|
|
index = $~.offset(2)[0]
|
|
|
|
indexes << index
|
2019-07-17 01:43:50 -04:00
|
|
|
if FileHelper.supported_images.include?(extension)
|
|
|
|
raw_matches << [match[1], match[1], +"![](#{PLACEHOLDER})", index]
|
|
|
|
else
|
|
|
|
raw_matches << [match[1], match[1], ++"#{Discourse.base_url}#{PATH_PLACEHOLDER}", index]
|
|
|
|
end
|
2019-06-06 23:46:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
markdown.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]
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
markdown.scan(/\[[^\[\]]*\]: #{regexp}/) do |match|
|
|
|
|
if match[0].present?
|
|
|
|
index = $~.offset(1)[0]
|
|
|
|
next if indexes.include?(index)
|
|
|
|
indexes << index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-14 01:47:44 -04:00
|
|
|
markdown.scan(/(([\n\s\)\]\<])+)#{regexp}/) do |match|
|
2019-06-06 23:46:52 -04:00
|
|
|
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]
|
|
|
|
]
|
2019-06-03 03:41:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
raw_matches
|
|
|
|
.sort { |a, b| a[3] <=> b[3] }
|
|
|
|
.each do |match, link, replace_with, _index|
|
|
|
|
|
|
|
|
node_info = link_occurences.shift
|
|
|
|
next unless node_info&.dig(:is_valid)
|
|
|
|
|
|
|
|
if link.include?(node_info[:link])
|
|
|
|
begin
|
|
|
|
uri = URI(link)
|
|
|
|
rescue URI::Error
|
|
|
|
end
|
|
|
|
|
|
|
|
if !Discourse.store.external?
|
2019-06-14 01:11:53 -04:00
|
|
|
host = uri&.host
|
|
|
|
|
2019-06-14 01:56:35 -04:00
|
|
|
hosts = [Discourse.current_hostname]
|
|
|
|
|
|
|
|
if cdn_url = GlobalSetting.cdn_url
|
|
|
|
hosts << URI(GlobalSetting.cdn_url).hostname
|
|
|
|
end
|
|
|
|
|
|
|
|
if host && !hosts.include?(host)
|
2019-06-14 01:11:53 -04:00
|
|
|
next
|
|
|
|
end
|
2019-06-03 03:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
upload = Upload.get_from_url(link)
|
|
|
|
|
|
|
|
if upload
|
2019-06-06 03:50:16 -04:00
|
|
|
replace_with.sub!(PLACEHOLDER, upload.short_url)
|
|
|
|
replace_with.sub!(PATH_PLACEHOLDER, upload.short_path)
|
|
|
|
markdown.sub!(match, replace_with)
|
2019-06-03 03:41:26 -04:00
|
|
|
else
|
|
|
|
on_missing.call(link) if on_missing
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-13 00:08:01 -04:00
|
|
|
markdown.scan(/(__([a-f0-9]{40})__)/) do |match|
|
|
|
|
upload = Upload.find_by(sha1: match[1])
|
|
|
|
markdown = markdown.sub(match[0], upload.short_path)
|
|
|
|
end
|
|
|
|
|
2019-05-30 02:38:46 -04:00
|
|
|
markdown
|
|
|
|
end
|
|
|
|
|
2019-06-06 03:50:16 -04:00
|
|
|
def self.match_md_inline_img(markdown, external_src: false)
|
|
|
|
markdown.scan(/(!?\[([^\[\]]*)\]\(([a-zA-z0-9\.\/:-]+)([ ]*['"]{1}[^\)]*['"]{1}[ ]*)?\))/) do |match|
|
|
|
|
if (matched_uploads(match[2]).present? || external_src) && block_given?
|
|
|
|
yield(
|
|
|
|
match[0],
|
|
|
|
match[2],
|
|
|
|
+"#{match[0].start_with?("!") ? "!" : ""}[#{match[1]}](#{PLACEHOLDER}#{match[3]})",
|
|
|
|
$~.offset(0)[0]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-13 01:53:43 -04:00
|
|
|
def self.match_bbcode_img(markdown, external_src: false)
|
2019-06-19 23:47:32 -04:00
|
|
|
markdown.scan(/(\[img\]\s*([^\[\]\s]+)\s*\[\/img\])/) do |match|
|
2019-06-13 01:53:43 -04:00
|
|
|
if (matched_uploads(match[1]).present? && block_given?) || external_src
|
2019-06-13 01:47:18 -04:00
|
|
|
yield(match[0], match[1], +"![](#{PLACEHOLDER})", $~.offset(0)[0])
|
|
|
|
end
|
2019-06-06 03:50:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.match_md_reference(markdown)
|
|
|
|
markdown.scan(/(\[([^\]]+)\]:([ ]+)(\S+))/) do |match|
|
2019-06-06 23:46:52 -04:00
|
|
|
if match[3] && matched_uploads(match[3]).present? && block_given?
|
2019-06-06 03:50:16 -04:00
|
|
|
yield(
|
|
|
|
match[0],
|
|
|
|
match[3],
|
|
|
|
+"[#{match[1]}]:#{match[2]}#{Discourse.base_url}#{PATH_PLACEHOLDER}",
|
|
|
|
$~.offset(0)[0]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.match_anchor(markdown, external_href: false)
|
|
|
|
markdown.scan(/((<a[^<]+>)([^<\a>]*?)<\/a>)/) do |match|
|
|
|
|
node = Nokogiri::HTML::fragment(match[0]).children[0]
|
|
|
|
href = node.attributes["href"]&.value
|
|
|
|
|
|
|
|
if href && (matched_uploads(href).present? || external_href)
|
|
|
|
has_attachment = node.attributes["class"]&.value
|
|
|
|
index = $~.offset(0)[0]
|
|
|
|
text = match[2].strip.gsub("\n", "").gsub(/ +/, " ")
|
|
|
|
text = "#{text}|attachment" if has_attachment
|
|
|
|
|
|
|
|
yield(match[0], href, +"[#{text}](#{PLACEHOLDER})", index) if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.match_img(markdown, external_src: false)
|
2019-06-21 02:07:06 -04:00
|
|
|
markdown.scan(/(([ ]*)<(?!img)[^<>]+\/?>)?([\r\n]*)(([ ]*)<img ([^>\n]+)>([ ]*))([\r\n]*)/) do |match|
|
2019-06-21 00:32:02 -04:00
|
|
|
node = Nokogiri::HTML::fragment(match[3].strip).children[0]
|
2019-06-11 22:31:00 -04:00
|
|
|
src = node.attributes["src"]&.value
|
2019-06-06 03:50:16 -04:00
|
|
|
|
2019-06-11 22:31:00 -04:00
|
|
|
if src && (matched_uploads(src).present? || external_src)
|
2019-06-06 03:50:16 -04:00
|
|
|
text = node.attributes["alt"]&.value
|
2019-07-05 14:34:11 -04:00
|
|
|
width = node.attributes["width"]&.value.to_i
|
|
|
|
height = node.attributes["height"]&.value.to_i
|
2019-06-06 03:50:16 -04:00
|
|
|
title = node.attributes["title"]&.value
|
2019-07-05 14:34:11 -04:00
|
|
|
text = "#{text}|#{width}x#{height}" if width > 0 && height > 0
|
2019-06-06 03:50:16 -04:00
|
|
|
after_html_tag = match[0].present?
|
|
|
|
|
|
|
|
spaces_before =
|
|
|
|
if after_html_tag && !match[0].end_with?("/>")
|
2019-06-21 00:32:02 -04:00
|
|
|
(match[4].length > 0 ? match[4] : " ")
|
2019-06-06 03:50:16 -04:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
replacement = +"#{spaces_before}![#{text}](#{PLACEHOLDER}#{title.present? ? " \"#{title}\"" : ""})"
|
|
|
|
|
2019-06-21 00:32:02 -04:00
|
|
|
if after_html_tag && (num_newlines = match[2].length) <= 1
|
2019-06-06 03:50:16 -04:00
|
|
|
replacement.prepend("\n" * (num_newlines == 0 ? 2 : 1))
|
|
|
|
end
|
|
|
|
|
2019-06-21 00:32:02 -04:00
|
|
|
if after_html_tag && !match[0].end_with?("/>") && (num_newlines = match[7].length) <= 1
|
2019-06-06 03:50:16 -04:00
|
|
|
replacement += ("\n" * (num_newlines == 0 ? 2 : 1))
|
|
|
|
end
|
|
|
|
|
2019-06-21 00:32:02 -04:00
|
|
|
match[3].strip! if !after_html_tag
|
2019-06-06 03:50:16 -04:00
|
|
|
|
2019-06-21 02:07:06 -04:00
|
|
|
if (match[1].nil? || match[1].length < 4)
|
|
|
|
if (match[4].nil? || match[4].length < 4)
|
|
|
|
yield(match[3], src, replacement, $~.offset(0)[0]) if block_given?
|
|
|
|
else
|
|
|
|
yield(match[3], src, match[3].sub(src, PATH_PLACEHOLDER), $~.offset(0)[0]) if block_given?
|
|
|
|
end
|
2019-06-21 00:32:02 -04:00
|
|
|
else
|
|
|
|
yield(match[3], src, match[3].sub(src, PATH_PLACEHOLDER), $~.offset(0)[0]) if block_given?
|
|
|
|
end
|
2019-06-06 03:50:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-30 02:38:46 -04:00
|
|
|
def self.matched_uploads(node)
|
|
|
|
matches = []
|
|
|
|
|
2019-06-14 00:48:31 -04:00
|
|
|
base_url = Discourse.base_url.sub(/https?:\/\//, "(https?://)")
|
2019-06-14 01:56:35 -04:00
|
|
|
|
|
|
|
if GlobalSetting.cdn_url
|
|
|
|
cdn_url = GlobalSetting.cdn_url.sub(/https?:\/\//, "(https?://)")
|
|
|
|
end
|
2019-06-14 00:48:31 -04:00
|
|
|
|
2019-05-30 02:38:46 -04:00
|
|
|
regexps = [
|
2019-06-12 23:18:49 -04:00
|
|
|
/(upload:\/\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
|
|
|
|
/(\/uploads\/short-url\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
|
2019-06-14 00:48:31 -04:00
|
|
|
/(#{base_url}\/uploads\/short-url\/([a-zA-Z0-9]+)[a-zA-Z0-9\.]*)/,
|
2019-05-30 02:38:46 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
db = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
if Discourse.store.external?
|
|
|
|
if Rails.configuration.multisite
|
2019-06-12 04:41:44 -04:00
|
|
|
regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
2019-06-03 03:41:26 -04:00
|
|
|
regexps << /(#{SiteSetting.Upload.s3_cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
2019-05-30 02:38:46 -04:00
|
|
|
else
|
2019-06-12 04:41:44 -04:00
|
|
|
regexps << /((https?:)?#{SiteSetting.Upload.s3_base_url}#{UPLOAD_REGEXP_PATTERN})/
|
2019-06-03 03:41:26 -04:00
|
|
|
regexps << /(#{SiteSetting.Upload.s3_cdn_url}#{UPLOAD_REGEXP_PATTERN})/
|
|
|
|
regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
2019-06-14 00:48:31 -04:00
|
|
|
regexps << /(#{base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
2019-06-14 01:56:35 -04:00
|
|
|
regexps << /(#{cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ if cdn_url
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|
|
|
|
else
|
2019-06-03 03:41:26 -04:00
|
|
|
regexps << /(\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
2019-06-14 00:48:31 -04:00
|
|
|
regexps << /(#{base_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/
|
2019-06-14 01:56:35 -04:00
|
|
|
regexps << /(#{cdn_url}\/uploads\/#{db}#{UPLOAD_REGEXP_PATTERN})/ if cdn_url
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
node = node.to_s
|
|
|
|
|
|
|
|
regexps.each do |regexp|
|
2019-06-12 04:41:44 -04:00
|
|
|
node.scan(/(^|[\n\s"'\(>])#{regexp}($|[\n\s"'\)<])/) do |matched|
|
|
|
|
matches << matched[1]
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
matches
|
|
|
|
end
|
|
|
|
private_class_method :matched_uploads
|
2019-06-18 23:14:46 -04:00
|
|
|
|
|
|
|
def self.node_children_names(node, names = Set.new)
|
|
|
|
if node.children.blank?
|
|
|
|
names << node.name
|
|
|
|
return names
|
|
|
|
end
|
|
|
|
|
|
|
|
node.children.each do |child|
|
|
|
|
names = node_children_names(child, names)
|
|
|
|
end
|
|
|
|
|
|
|
|
names
|
|
|
|
end
|
|
|
|
private_class_method :node_children_names
|
2019-05-30 02:38:46 -04:00
|
|
|
end
|