Skip to content

Commit

Permalink
DEV: use cdn url to download the external uploads to local.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Jun 6, 2019
1 parent 4be54d5 commit b783068
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/file_store/base_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def download(upload)

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,
Expand Down
18 changes: 16 additions & 2 deletions spec/components/file_store/base_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,33 @@
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

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

0 comments on commit b783068

Please sign in to comment.