FIX: ensure local images use local CDN when uploads are stored on S3

When the S3 store was enabled, we were only applying the S3 CDN.
So all images stored locally, like the emojis, were never put on the local CDN.

Fixed a bunch of CookedPostProcessor test by adding a call to 'optimize_urls'
in order to get final URLs.

I also removed the unnecessary PrettyText.add_s3_cdn method since this is already
handled in the CookedPostProcessor.
This commit is contained in:
Régis Hanol
2019-02-20 19:24:38 +01:00
parent 964e7edcaf
commit 664e90bd17
6 changed files with 108 additions and 112 deletions

View File

@@ -389,7 +389,7 @@ class CookedPostProcessor
if upload
thumbnail = upload.thumbnail(w, h)
if thumbnail && thumbnail.filesize.to_i < upload.filesize
img["src"] = UrlHelper.cook_url(thumbnail.url)
img["src"] = thumbnail.url
srcset = +""
@@ -408,11 +408,11 @@ class CookedPostProcessor
img["srcset"] = "#{UrlHelper.cook_url(img["src"])}#{srcset}" if srcset.present?
end
else
img["src"] = UrlHelper.cook_url(upload.url)
img["src"] = upload.url
end
if small_upload = loading_image(upload)
img["data-small-upload"] = UrlHelper.cook_url(small_upload.url)
img["data-small-upload"] = small_upload.url
end
end
@@ -588,8 +588,10 @@ class CookedPostProcessor
end
end
@doc.css("img[src]").each do |img|
img["src"] = UrlHelper.cook_url(img["src"].to_s)
%w{src data-small-upload}.each do |selector|
@doc.css("img[#{selector}]").each do |img|
img[selector] = UrlHelper.cook_url(img[selector].to_s)
end
end
end

View File

@@ -46,8 +46,7 @@ module FileStore
end
def cdn_url(url)
return url if Discourse.asset_host.blank?
url.sub(Discourse.base_url_no_prefix, Discourse.asset_host)
UrlHelper.local_cdn_url(url)
end
def path_for(upload)

View File

@@ -254,10 +254,6 @@ module PrettyText
add_rel_nofollow_to_user_content(doc)
end
if SiteSetting.Upload.enable_s3_uploads && SiteSetting.Upload.s3_cdn_url.present?
add_s3_cdn(doc)
end
if SiteSetting.enable_mentions
add_mentions(doc, user_id: opts[:user_id])
end
@@ -265,13 +261,6 @@ module PrettyText
doc.to_html
end
def self.add_s3_cdn(doc)
doc.css("img").each do |img|
next unless img["src"]
img["src"] = Discourse.store.cdn_url(img["src"])
end
end
def self.add_rel_nofollow_to_user_content(doc)
whitelist = []

View File

@@ -56,11 +56,20 @@ class UrlHelper
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
unless is_attachment && no_cdn
url = Discourse.store.cdn_url(url)
url = local_cdn_url(url) if Discourse.store.external?
end
schemaless(url)
rescue URI::Error
url
end
def self.local_cdn_url(url)
return url if Discourse.asset_host.blank?
url.sub(Discourse.base_url_no_prefix, Discourse.asset_host)
end
end