Commit fc90d9e5 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Tell clients/proxies to cache raw blob requests

parent 00cb4a97
...@@ -7,6 +7,8 @@ class Projects::AvatarsController < Projects::ApplicationController ...@@ -7,6 +7,8 @@ class Projects::AvatarsController < Projects::ApplicationController
@blob = @repository.blob_at_branch('master', @project.avatar_in_git) @blob = @repository.blob_at_branch('master', @project.avatar_in_git)
if @blob if @blob
headers['X-Content-Type-Options'] = 'nosniff' headers['X-Content-Type-Options'] = 'nosniff'
set_cache_headers
check_etag!
headers.store(*Gitlab::Workhorse.send_git_blob(@repository, @blob)) headers.store(*Gitlab::Workhorse.send_git_blob(@repository, @blob))
headers['Content-Disposition'] = 'inline' headers['Content-Disposition'] = 'inline'
headers['Content-Type'] = safe_content_type(@blob) headers['Content-Type'] = safe_content_type(@blob)
......
...@@ -12,6 +12,8 @@ class Projects::RawController < Projects::ApplicationController ...@@ -12,6 +12,8 @@ class Projects::RawController < Projects::ApplicationController
if @blob if @blob
headers['X-Content-Type-Options'] = 'nosniff' headers['X-Content-Type-Options'] = 'nosniff'
check_etag!
set_cache_headers
if @blob.lfs_pointer? if @blob.lfs_pointer?
send_lfs_object send_lfs_object
......
...@@ -152,4 +152,29 @@ module BlobHelper ...@@ -152,4 +152,29 @@ module BlobHelper
'application/octet-stream' 'application/octet-stream'
end end
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 end
...@@ -56,6 +56,7 @@ class Project < ActiveRecord::Base ...@@ -56,6 +56,7 @@ class Project < ActiveRecord::Base
extend Gitlab::ConfigHelper extend Gitlab::ConfigHelper
UNKNOWN_IMPORT_URL = 'http://unknown.git' UNKNOWN_IMPORT_URL = 'http://unknown.git'
AVATAR_BRANCH = 'master'
default_value_for :archived, false default_value_for :archived, false
default_value_for :visibility_level, gitlab_config_features.visibility_level default_value_for :visibility_level, gitlab_config_features.visibility_level
...@@ -544,9 +545,9 @@ class Project < ActiveRecord::Base ...@@ -544,9 +545,9 @@ class Project < ActiveRecord::Base
end end
def avatar_in_git def avatar_in_git
@avatar_file ||= 'logo.png' if repository.blob_at_branch('master', 'logo.png') @avatar_file ||= 'logo.png' if repository.blob_at_branch(AVATAR_BRANCH, 'logo.png')
@avatar_file ||= 'logo.jpg' if repository.blob_at_branch('master', 'logo.jpg') @avatar_file ||= 'logo.jpg' if repository.blob_at_branch(AVATAR_BRANCH, 'logo.jpg')
@avatar_file ||= 'logo.gif' if repository.blob_at_branch('master', 'logo.gif') @avatar_file ||= 'logo.gif' if repository.blob_at_branch(AVATAR_BRANCH, 'logo.gif')
@avatar_file @avatar_file
end end
......
...@@ -6,4 +6,4 @@ ...@@ -6,4 +6,4 @@
- blob = sanitize_svg(blob) - blob = sanitize_svg(blob)
%img{src: "data:#{blob.mime_type};base64,#{Base64.encode64(blob.data)}"} %img{src: "data:#{blob.mime_type};base64,#{Base64.encode64(blob.data)}"}
- else - 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))}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment