Tell clients/proxies to cache raw blob requests

This commit is contained in:
Jacob Vosmaer 2016-03-03 17:59:47 +01:00
parent 00cb4a9714
commit fc90d9e589
5 changed files with 34 additions and 4 deletions

View file

@ -7,6 +7,8 @@ class Projects::AvatarsController < Projects::ApplicationController
@blob = @repository.blob_at_branch('master', @project.avatar_in_git)
if @blob
headers['X-Content-Type-Options'] = 'nosniff'
set_cache_headers
check_etag!
headers.store(*Gitlab::Workhorse.send_git_blob(@repository, @blob))
headers['Content-Disposition'] = 'inline'
headers['Content-Type'] = safe_content_type(@blob)

View file

@ -12,6 +12,8 @@ class Projects::RawController < Projects::ApplicationController
if @blob
headers['X-Content-Type-Options'] = 'nosniff'
check_etag!
set_cache_headers
if @blob.lfs_pointer?
send_lfs_object

View file

@ -152,4 +152,29 @@ module BlobHelper
'application/octet-stream'
end
end
def set_cache_headers
if @project.visibility_level == Project::PUBLIC
cache_control = 'public, '
else
cache_control = 'private, '
end
if @ref && @commit && @ref == @commit.id
# This is a link to a commit by its commit SHA. That means that the blob
# is immutable.
cache_control << 'max-age=600' # 10 minutes
else
# A branch or tag points at this blob. That means that the expected blob
# value may change over time.
cache_control << 'max-age=60' # 1 minute
end
headers['Cache-Control'] = cache_control
headers['ETag'] = @blob.id
end
def check_etag!
stale?(etag: @blob.id)
end
end

View file

@ -56,6 +56,7 @@ class Project < ActiveRecord::Base
extend Gitlab::ConfigHelper
UNKNOWN_IMPORT_URL = 'http://unknown.git'
AVATAR_BRANCH = 'master'
default_value_for :archived, false
default_value_for :visibility_level, gitlab_config_features.visibility_level
@ -544,9 +545,9 @@ class Project < ActiveRecord::Base
end
def avatar_in_git
@avatar_file ||= 'logo.png' if repository.blob_at_branch('master', 'logo.png')
@avatar_file ||= 'logo.jpg' if repository.blob_at_branch('master', 'logo.jpg')
@avatar_file ||= 'logo.gif' if repository.blob_at_branch('master', 'logo.gif')
@avatar_file ||= 'logo.png' if repository.blob_at_branch(AVATAR_BRANCH, 'logo.png')
@avatar_file ||= 'logo.jpg' if repository.blob_at_branch(AVATAR_BRANCH, 'logo.jpg')
@avatar_file ||= 'logo.gif' if repository.blob_at_branch(AVATAR_BRANCH, 'logo.gif')
@avatar_file
end

View file

@ -6,4 +6,4 @@
- blob = sanitize_svg(blob)
%img{src: "data:#{blob.mime_type};base64,#{Base64.encode64(blob.data)}"}
- else
%img{src: namespace_project_raw_path(@project.namespace, @project, @id)}
%img{src: namespace_project_raw_path(@project.namespace, @project, tree_join(@commit.id, blob.path))}