Commit fc90d9e5 authored by Jacob Vosmaer's avatar Jacob Vosmaer
Browse files

Tell clients/proxies to cache raw blob requests

parent 00cb4a97
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ def show
    @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)
+2 −0
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@ def show

    if @blob
      headers['X-Content-Type-Options'] = 'nosniff'
      check_etag!
      set_cache_headers

      if @blob.lfs_pointer?
        send_lfs_object
+25 −0
Original line number Diff line number Diff line
@@ -152,4 +152,29 @@ def safe_content_type(blob)
      '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
+4 −3
Original line number Diff line number Diff line
@@ -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 @@ def avatar_type
  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

+1 −1
Original line number Diff line number Diff line
@@ -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))}