A platform for community discussion. Free, open, simple.
FIX: improve last_modified date returned for avatars
Sam29315b73c255
diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rbindex cf91d95db93..271cd412571 100644--- a/app/controllers/user_avatars_controller.rb+++ b/app/controllers/user_avatars_controller.rb@@ -124,7 +124,7 @@ class UserAvatarsController < ApplicationControlleroptimized_path = Discourse.store.path_for(optimized)image = optimized_path if File.exists?(optimized_path)else- return proxy_avatar(Discourse.store.cdn_url(optimized.url))+ return proxy_avatar(Discourse.store.cdn_url(optimized.url), upload.created_at)endend@@ -141,7 +141,7 @@ class UserAvatarsController < ApplicationControllerendPROXY_PATH = Rails.root + "tmp/avatar_proxy"- def proxy_avatar(url)+ def proxy_avatar(url, last_modified)if url[0..1] == "//"url = (SiteSetting.force_https ? "https:" : "http:") + url@@ -163,8 +163,7 @@ class UserAvatarsController < ApplicationControllerFileUtils.mv tmp.path, pathend- # putting a bogus date cause download is not retaining the data- response.headers["Last-Modified"] = DateTime.parse("1-1-2000").httpdate+ response.headers["Last-Modified"] = last_modified.httpdateresponse.headers["Content-Length"] = File.size(path).to_simmutable_for(1.year)send_file path, disposition: nil@@ -174,7 +173,7 @@ class UserAvatarsController < ApplicationControllerdef render_blankpath = Rails.root + "public/images/avatar.png"expires_in 10.minutes, public: true- response.headers["Last-Modified"] = DateTime.parse("1-1-2000").httpdate+ response.headers["Last-Modified"] = 10.minutes.ago.httpdateresponse.headers["Content-Length"] = File.size(path).to_ssend_file path, disposition: nilenddiff --git a/spec/requests/user_avatars_controller_spec.rb b/spec/requests/user_avatars_controller_spec.rbindex d8e5e0b0e34..50cc68eca0a 100644--- a/spec/requests/user_avatars_controller_spec.rb+++ b/spec/requests/user_avatars_controller_spec.rb@@ -79,7 +79,7 @@ describe UserAvatarsController doupload = Fabricate(:upload, url: "//test.s3.amazonaws.com/something")- Fabricate(:optimized_image,+ optimized_image = Fabricate(:optimized_image,sha1: SecureRandom.hex << "A" * 8,upload: upload,width: 98,@@ -98,6 +98,16 @@ describe UserAvatarsController doexpect(response.body).to eq("image")expect(response.headers["Cache-Control"]).to eq('max-age=31556952, public, immutable')+ expect(response.headers["Last-Modified"]).to eq(optimized_image.upload.created_at.httpdate)+ end++ it 'serves a correct last modified for render blank' do+ freeze_time++ get "/user_avatar/default/xxx/51/777.png"++ expect(response.status).to eq(200)+ expect(response.headers["Last-Modified"]).to eq(10.minutes.ago.httpdate)endit 'serves image even if size missing and its in local mode' do