Patch #37600 ยป cache-attachments-and-thumbnails.patch
app/controllers/attachments_controller.rb | ||
---|---|---|
20 | 20 |
class AttachmentsController < ApplicationController |
21 | 21 |
include ActionView::Helpers::NumberHelper |
22 | 22 | |
23 |
BROWSER_CACHE_LIFETIME = 1.day |
|
24 | ||
23 | 25 |
before_action :find_attachment, :only => [:show, :download, :thumbnail, :update, :destroy] |
24 | 26 |
before_action :find_container, :only => [:edit_all, :update_all, :download_all] |
25 | 27 |
before_action :find_downloadable_attachments, :only => :download_all |
... | ... | |
74 | 76 |
@attachment.increment_download |
75 | 77 |
end |
76 | 78 | |
79 |
expires_in BROWSER_CACHE_LIFETIME |
|
77 | 80 |
if stale?(:etag => @attachment.digest, :template => false) |
78 | 81 |
# images are sent inline |
79 | 82 |
send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
... | ... | |
84 | 87 | |
85 | 88 |
def thumbnail |
86 | 89 |
if @attachment.thumbnailable? && tbnail = @attachment.thumbnail(:size => params[:size]) |
90 |
# Should not be cached if params[:size] is not explicitly set |
|
91 |
# because the content varies depending on Setting.thumbnails_size |
|
92 |
expires_in BROWSER_CACHE_LIFETIME if params[:size].present? |
|
87 | 93 |
if stale?(:etag => tbnail, :template => false) |
88 | 94 |
send_file( |
89 | 95 |
tbnail, |
test/functional/attachments_controller_test.rb | ||
---|---|---|
304 | 304 |
assert_equal 'application/x-ruby', @response.media_type |
305 | 305 |
etag = @response.etag |
306 | 306 |
assert_not_nil etag |
307 |
assert_equal ['max-age=86400', 'private'], response.headers['Cache-Control'].split(', ').sort |
|
307 | 308 | |
308 | 309 |
@request.env["HTTP_IF_NONE_MATCH"] = etag |
309 | 310 |
get(:download, :params => {:id => 4}) |
... | ... | |
442 | 443 |
) |
443 | 444 |
assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fthumbnail%2F16' |
444 | 445 |
end |
446 | ||
447 |
def test_thumbnail_should_set_cache_control_header |
|
448 |
Attachment.clear_thumbnails |
|
449 |
@request.session[:user_id] = 2 |
|
450 |
get( |
|
451 |
:thumbnail, |
|
452 |
:params => { |
|
453 |
:id => 16, |
|
454 |
:size => 200 |
|
455 |
} |
|
456 |
) |
|
457 |
assert_response :success |
|
458 |
assert_equal ['max-age=86400', 'private'], response.headers['Cache-Control'].split(', ').sort |
|
459 | ||
460 |
get( |
|
461 |
:thumbnail, |
|
462 |
:params => { |
|
463 |
:id => 16 |
|
464 |
} |
|
465 |
) |
|
466 |
assert_response :success |
|
467 |
# Should not be cached if params[:size] is not explicitly set |
|
468 |
# because the content varies depending on Setting.thumbnails_size |
|
469 |
assert_equal ['private'], response.headers['Cache-Control'].split(', ').sort |
|
470 |
end |
|
445 | 471 |
else |
446 | 472 |
puts '(ImageMagick convert not available)' |
447 | 473 |
end |