From 6c9190357cce379a559ff1fd6080d2bb41674108 Mon Sep 17 00:00:00 2001 From: Maja Komel Date: Tue, 5 Jun 2018 15:19:06 +0200 Subject: [PATCH] fix get from url for external uploads without CDN --- app/models/upload.rb | 2 ++ spec/models/upload_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/app/models/upload.rb b/app/models/upload.rb index 754e5d7f69a..cb3b69e9e41 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -79,6 +79,8 @@ class Upload < ActiveRecord::Base return if url.blank? # we store relative urls, so we need to remove any host/cdn url = url.sub(Discourse.asset_host, "") if Discourse.asset_host.present? && Discourse.asset_host != SiteSetting.Upload.s3_cdn_url + # when using s3 without CDN + url = url.sub(/^https?\:/, "") if url.include?(Discourse.store.absolute_base_url) && Discourse.store.external? # when using s3, we need to replace with the absolute base url url = url.sub(SiteSetting.Upload.s3_cdn_url, Discourse.store.absolute_base_url) if SiteSetting.Upload.s3_cdn_url.present? diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 32b86bbc182..697cd266ee2 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -106,6 +106,13 @@ describe Upload do SiteSetting.enable_s3_uploads = false end + it "should return the right upload when using base url (not CDN) for s3" do + upload + url = "https://#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com#{path}" + + expect(Upload.get_from_url(url)).to eq(upload) + end + it "should return the right upload when using a CDN for s3" do upload s3_cdn_url = 'https://mycdn.slowly.net'