diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rb index a44dd289c12..c2be05ce8d7 100644 --- a/app/controllers/user_avatars_controller.rb +++ b/app/controllers/user_avatars_controller.rb @@ -74,7 +74,7 @@ class UserAvatarsController < ApplicationController if optimized = get_optimized_image(upload, size) unless optimized.local? expires_in 1.day, public: true - return redirect_to optimized.url + return redirect_to Discourse.store.cdn_url(optimized.url) end image = Discourse.store.path_for(optimized) end diff --git a/lib/file_store/base_store.rb b/lib/file_store/base_store.rb index 74df43e1815..6fc9e688573 100644 --- a/lib/file_store/base_store.rb +++ b/lib/file_store/base_store.rb @@ -44,6 +44,10 @@ module FileStore def purge_tombstone(grace_period) end + def cdn_url(url) + url + end + end end diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb index 89e2deec803..4ee0da7f2bc 100644 --- a/lib/file_store/s3_store.rb +++ b/lib/file_store/s3_store.rb @@ -79,6 +79,14 @@ module FileStore end end + def cdn_url(url) + if SiteSetting.s3_cdn_url.present? + url.sub(absolute_base_url, SiteSetting.s3_cdn_url) + else + url + end + end + private def get_path_for_upload(file, upload) diff --git a/spec/controllers/user_avatars_controller_spec.rb b/spec/controllers/user_avatars_controller_spec.rb index f25f0d3e46e..124580ee034 100644 --- a/spec/controllers/user_avatars_controller_spec.rb +++ b/spec/controllers/user_avatars_controller_spec.rb @@ -3,23 +3,34 @@ require 'spec_helper' describe UserAvatarsController do context 'show' do - it 'redirects to correct size for non local' do + it 'handles non local content correctly' do SiteSetting.avatar_sizes = "100|49" SiteSetting.enable_s3_uploads = true SiteSetting.s3_access_key_id = "XXX" SiteSetting.s3_secret_access_key = "XXX" SiteSetting.s3_upload_bucket = "test" + SiteSetting.s3_cdn_url = "http://cdn.com" GlobalSetting.expects(:cdn_url).returns("http://awesome.com/boom") - upload = Fabricate(:upload) + upload = Fabricate(:upload, url: "//test.s3-#{SiteSetting.s3_region}.amazonaws.com/something") + Fabricate(:optimized_image, + sha1: SecureRandom.hex << "A"*8, + upload: upload, + width: 98, + height: 98, + url: "//test.s3-#{SiteSetting.s3_region}.amazonaws.com/something/else") + user = Fabricate(:user, uploaded_avatar_id: upload.id) get :show, size: 97, username: user.username, version: upload.id, hostname: 'default' # 98 is closese which is 49 * 2 for retina expect(response).to redirect_to("http://awesome.com/boom/user_avatar/default/#{user.username_lower}/98/#{upload.id}.png") + + get :show, size: 98, username: user.username, version: upload.id, hostname: 'default' + expect(response).to redirect_to("http://cdn.com/something/else") end it 'serves image even if size missing and its in local mode' do