From b7830680b67e44ba926df01f522831907f3a07c4 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Thu, 6 Jun 2019 19:17:19 +0530 Subject: [PATCH] DEV: use cdn url to download the external uploads to local. --- lib/file_store/base_store.rb | 3 ++- spec/components/file_store/base_store_spec.rb | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/file_store/base_store.rb b/lib/file_store/base_store.rb index 15b85cb9941..6e8c53442d3 100644 --- a/lib/file_store/base_store.rb +++ b/lib/file_store/base_store.rb @@ -77,7 +77,8 @@ module FileStore if !file max_file_size_kb = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes - url = SiteSetting.scheme + ":" + upload.url + url = Discourse.store.cdn_url(upload.url) + url = SiteSetting.scheme + ":" + url if url =~ /^\/\// file = FileHelper.download( url, max_file_size: max_file_size_kb, diff --git a/spec/components/file_store/base_store_spec.rb b/spec/components/file_store/base_store_spec.rb index 5ef2579747d..3c2e30eef90 100644 --- a/spec/components/file_store/base_store_spec.rb +++ b/spec/components/file_store/base_store_spec.rb @@ -59,13 +59,12 @@ RSpec.describe FileStore::BaseStore do end let(:upload_s3) { Fabricate(:upload_s3) } + let(:store) { FileStore::BaseStore.new } it "should return consistent encodings for fresh and cached downloads" do # Net::HTTP always returns binary ASCII-8BIT encoding. File.read auto-detects the encoding # Make sure we File.read after downloading a file for consistency - store = FileStore::BaseStore.new - first_encoding = store.download(upload_s3).read.encoding second_encoding = store.download(upload_s3).read.encoding @@ -73,5 +72,20 @@ RSpec.describe FileStore::BaseStore do expect(first_encoding).to eq(Encoding::UTF_8) expect(second_encoding).to eq(Encoding::UTF_8) end + + it "should return the file" do + file = store.download(upload_s3) + + expect(file.class).to eq(File) + end + + it "should return the file when s3 cdn enabled" do + SiteSetting.s3_cdn_url = "https://cdn.s3.amazonaws.com" + stub_request(:get, Discourse.store.cdn_url(upload_s3.url)).to_return(status: 200, body: "Hello world") + + file = store.download(upload_s3) + + expect(file.class).to eq(File) + end end end