FIX: store the topic links using the cooked upload url

This commit is contained in:
Régis Hanol
2018-08-14 12:23:32 +02:00
parent ad5f502332
commit de92913bf4
31 changed files with 116 additions and 110 deletions
+5 -19
View File
@@ -257,14 +257,14 @@ class CookedPostProcessor
return unless SiteSetting.crawl_images? || Discourse.store.has_been_uploaded?(url)
@size_cache[url] = FastImage.size(absolute_url)
rescue Zlib::BufError, URI::InvalidURIError, URI::InvalidComponentError, OpenSSL::SSL::SSLError
rescue Zlib::BufError, URI::Error, OpenSSL::SSL::SSLError
# FastImage.size raises BufError for some gifs, leave it.
end
def is_valid_image_url?(url)
uri = URI.parse(url)
%w(http https).include? uri.scheme
rescue URI::InvalidURIError
rescue URI::Error
end
def convert_to_link!(img)
@@ -460,28 +460,14 @@ class CookedPostProcessor
end
def optimize_urls
# attachments can't be on the CDN when either setting is enabled
if SiteSetting.login_required || SiteSetting.prevent_anons_from_downloading_files
@doc.css("a.attachment[href]").each do |a|
href = a["href"].to_s
a["href"] = UrlHelper.schemaless UrlHelper.absolute_without_cdn(href) if UrlHelper.is_local(href)
end
end
use_s3_cdn = SiteSetting.Upload.enable_s3_uploads && SiteSetting.Upload.s3_cdn_url.present?
%w{href data-download-href}.each do |selector|
@doc.css("a[#{selector}]").each do |a|
href = a[selector].to_s
a[selector] = UrlHelper.schemaless UrlHelper.absolute(href) if UrlHelper.is_local(href)
a[selector] = Discourse.store.cdn_url(a[selector]) if use_s3_cdn
a[selector] = UrlHelper.cook_url(a[selector].to_s)
end
end
@doc.css("img[src]").each do |img|
src = img["src"].to_s
img["src"] = UrlHelper.schemaless UrlHelper.absolute(src) if UrlHelper.is_local(src)
img["src"] = Discourse.store.cdn_url(img["src"]) if use_s3_cdn
img["src"] = UrlHelper.cook_url(img["src"].to_s)
end
end
@@ -544,7 +530,7 @@ class CookedPostProcessor
path =
begin
URI(img["src"]).path
rescue URI::InvalidURIError, URI::InvalidComponentError
rescue URI::Error
nil
end
+1 -1
View File
@@ -270,7 +270,7 @@ module Discourse
unless uri.is_a?(URI)
uri = begin
URI(uri)
rescue URI::InvalidURIError
rescue URI::Error
end
end
+1 -1
View File
@@ -218,7 +218,7 @@ module Email
begin
uri = URI.parse(base_url)
host = uri.host.downcase if uri.host.present?
rescue URI::InvalidURIError
rescue URI::Error
end
end
host
+2 -2
View File
@@ -153,7 +153,7 @@ module Email
# If an iframe is protocol relative, use SSL when displaying it
display_src = "#{src_uri.scheme || 'https'}://#{src_uri.host}#{src_uri.path}#{src_uri.query.nil? ? '' : '?' + src_uri.query}#{src_uri.fragment.nil? ? '' : '#' + src_uri.fragment}"
i.replace "<p><a href='#{src_uri.to_s}'>#{CGI.escapeHTML(display_src)}</a><p>"
rescue URI::InvalidURIError
rescue URI::Error
# If the URL is weird, remove the iframe
i.remove
end
@@ -215,7 +215,7 @@ module Email
@fragment.css("a").each do |link|
begin
link["href"] = "#{site_uri}#{link['href']}" unless URI(link["href"].to_s).host.present?
rescue URI::InvalidURIError, URI::InvalidComponentError
rescue URI::Error
# leave it
end
end
+2 -2
View File
@@ -378,8 +378,8 @@ class FinalDestination
def uri(location)
begin
URI(location)
rescue URI::InvalidURIError, ArgumentError
URI.parse(location)
rescue URI::Error
end
end
+1 -1
View File
@@ -46,7 +46,7 @@ class InlineOneboxer
if always_allow || domains
uri = begin
URI(url)
rescue URI::InvalidURIError
rescue URI::Error
end
if uri.present? &&
+2 -2
View File
@@ -293,7 +293,7 @@ module PrettyText
else
l["rel"] = "nofollow noopener"
end
rescue URI::InvalidURIError, URI::InvalidComponentError
rescue URI::Error
# add a nofollow anyway
l["rel"] = "nofollow noopener"
end
@@ -363,7 +363,7 @@ module PrettyText
unless uri.host.present? || href.start_with?('mailto')
link["href"] = "#{site_uri}#{link['href']}"
end
rescue URI::InvalidURIError, URI::InvalidComponentError
rescue URI::Error
# leave it
end
end
+2 -2
View File
@@ -426,13 +426,13 @@ module SiteSettingExtension
host = begin
URI.parse(url)&.host
rescue URI::InvalidURIError
rescue URI::Error
nil
end
host ||= begin
URI.parse("http://#{url}")&.host
rescue URI::InvalidURIError
rescue URI::Error
nil
end
+17
View File
@@ -31,4 +31,21 @@ class UrlHelper
encoded
end
def self.cook_url(url)
return url unless is_local(url)
uri = URI.parse(url)
filename = File.basename(uri.path)
is_attachment = !FileHelper.is_image?(filename)
no_cdn = SiteSetting.login_required || SiteSetting.prevent_anons_from_downloading_files
url = absolute_without_cdn(url)
url = Discourse.store.cdn_url(url) unless is_attachment && no_cdn
schemaless(url)
rescue URI::Error
url
end
end
+1 -1
View File
@@ -4,7 +4,7 @@ class UploadUrlValidator < ActiveModel::EachValidator
uri =
begin
URI.parse(value)
rescue URI::InvalidURIError
rescue URI::Error
end
unless uri && Upload.exists?(url: value)
+1 -1
View File
@@ -5,7 +5,7 @@ class UrlValidator < ActiveModel::EachValidator
begin
uri = URI.parse(value)
uri.is_a?(URI::HTTP) && !uri.host.nil? && uri.host.include?(".")
rescue URI::InvalidURIError => e
rescue URI::Error => e
if (e.message =~ /URI must be ascii only/)
value = URI.encode(value)
retry