Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sends_blob.rb « concerns « controllers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3cf260c9f1bff8253c1152c36c931cc0ebe31b7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

module SendsBlob
  extend ActiveSupport::Concern

  included do
    include BlobHelper
    include SendFileUpload
  end

  def send_blob(repository, blob, inline: true, allow_caching: false)
    if blob
      headers['X-Content-Type-Options'] = 'nosniff'

      return if cached_blob?(blob, allow_caching: allow_caching)

      if blob.stored_externally?
        send_lfs_object(blob, repository.project)
      else
        send_git_blob(repository, blob, inline: inline)
      end
    else
      render_404
    end
  end

  private

  def cached_blob?(blob, allow_caching: false)
    stale =
      if Feature.enabled?(:improve_blobs_cache_headers)
        stale?(strong_etag: blob.id)
      else
        stale?(etag: blob.id)
      end

    max_age =
      if @ref && @commit && @ref == @commit.id # rubocop:disable Gitlab/ModuleWithInstanceVariables
        # This is a link to a commit by its commit SHA. That means that the blob
        # is immutable. The only reason to invalidate the cache is if the commit
        # was deleted or if the user lost access to the repository.
        Blob::CACHE_TIME_IMMUTABLE
      else
        # A branch or tag points at this blob. That means that the expected blob
        # value may change over time.
        Blob::CACHE_TIME
      end

    # Because we are opinionated we set the cache headers ourselves.
    if Feature.enabled?(:improve_blobs_cache_headers)
      expires_in(max_age,
        public: allow_caching, must_revalidate: true, stale_if_error: 5.minutes,
        stale_while_revalidate: 1.minute, 's-maxage': 1.minute)
    else
      response.cache_control[:public] = allow_caching
      response.cache_control[:max_age] = max_age
    end

    !stale
  end

  def send_lfs_object(blob, project)
    lfs_object = find_lfs_object(blob)

    if lfs_object && lfs_object.project_allowed_access?(project)
      send_upload(lfs_object.file, attachment: blob.name)
    else
      render_404
    end
  end

  def find_lfs_object(blob)
    lfs_object = LfsObject.find_by_oid(blob.lfs_oid)
    if lfs_object && lfs_object.file.exists?
      lfs_object
    else
      nil
    end
  end
end