From a797f7c664c5a52f1e414840bee9861328ffea4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 26 May 2015 11:47:33 +0200 Subject: [PATCH] FIX: properly handle images when using 's3_cdn_url' --- app/jobs/regular/pull_hotlinked_images.rb | 1 + app/models/upload.rb | 4 +++- lib/file_store/s3_store.rb | 5 ++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index e9a35d8fc..ae1f8516f 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -111,6 +111,7 @@ module Jobs end # we don't want to pull images hosted on the CDN (if we use one) return false if Discourse.asset_host.present? && URI.parse(Discourse.asset_host).hostname == uri.hostname + return false if Discourse.s3_cdn_url.present? && URI.parse(Discourse.s3_cdn_url).hostname == uri.hostname # we don't want to pull images hosted on the main domain return false if URI.parse(Discourse.base_url_no_prefix).hostname == uri.hostname # check the domains blacklist diff --git a/app/models/upload.rb b/app/models/upload.rb index d604dd0b8..eb967cf9d 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -128,7 +128,9 @@ class Upload < ActiveRecord::Base def self.get_from_url(url) return if url.blank? # we store relative urls, so we need to remove any host/cdn - url = url.gsub(/^#{Discourse.asset_host}/i, "") if Discourse.asset_host.present? + url = url.sub(/^#{Discourse.asset_host}/i, "") if Discourse.asset_host.present? + # when using s3, we need to replace with the absolute base url + url = url.sub(/^#{SiteSetting.s3_cdn_url}/i, Discourse.store.absolute_base_url) if SiteSetting.s3_cdn_url.present? Upload.find_by(url: url) if Discourse.store.has_been_uploaded?(url) end diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb index 4dd92d739..29e2445f9 100644 --- a/lib/file_store/s3_store.rb +++ b/lib/file_store/s3_store.rb @@ -32,7 +32,10 @@ module FileStore end def has_been_uploaded?(url) - url.present? && url.start_with?(absolute_base_url) + return false if url.blank? + return true if url.start_with?(absolute_base_url) + return true if SiteSetting.s3_cdn_url.present? && url.start_with?(SiteSetting.s3_cdn_url) + false end def absolute_base_url