FIX: improve last_modified date returned for avatars

instead of hard coding a date:

1. For optimized images use the upload date when on s3
2. For not-found use 10 minutes ago to match the expiry
This commit is contained in:
Sam 2018-08-24 09:36:11 +10:00
parent c26de01399
commit 29315b73c2
2 changed files with 15 additions and 6 deletions

View File

@ -124,7 +124,7 @@ class UserAvatarsController < ApplicationController
optimized_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)
end
end
@ -141,7 +141,7 @@ class UserAvatarsController < ApplicationController
end
PROXY_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 < ApplicationController
FileUtils.mv tmp.path, path
end
# 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.httpdate
response.headers["Content-Length"] = File.size(path).to_s
immutable_for(1.year)
send_file path, disposition: nil
@ -174,7 +173,7 @@ class UserAvatarsController < ApplicationController
def render_blank
path = 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.httpdate
response.headers["Content-Length"] = File.size(path).to_s
send_file path, disposition: nil
end

View File

@ -79,7 +79,7 @@ describe UserAvatarsController do
upload = 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 do
expect(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)
end
it 'serves image even if size missing and its in local mode' do