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

global_file_size_check.rb « checks « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ff24467e9cc2fae1bfc66fc02ef6fe4979350cae (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
# frozen_string_literal: true

module Gitlab
  module Checks
    class GlobalFileSizeCheck < BaseBulkChecker
      LOG_MESSAGE = 'Checking for blobs over the file size limit'

      def validate!
        return unless Feature.enabled?(:global_file_size_check, project)

        Gitlab::AppJsonLogger.info(LOG_MESSAGE)
        logger.log_timed(LOG_MESSAGE) do
          oversized_blobs = Gitlab::Checks::FileSizeCheck::HookEnvironmentAwareAnyOversizedBlobs.new(
            project: project,
            changes: changes,
            file_size_limit_megabytes: file_size_limit
          ).find

          if oversized_blobs.present?

            blob_details = {}
            blob_id_size_msg = ""
            oversized_blobs.each do |blob|
              blob_details[blob.id] = { "size" => blob.size }

              # blob size is in byte, divide it by "/ 1024.0 / 1024.0" to get MiB
              blob_id_size_msg += "- #{blob.id} (#{(blob.size / 1024.0 / 1024.0).round(2)} MiB) \n"
            end

            oversize_err_msg = <<~OVERSIZE_ERR_MSG
              You are attempting to check in one or more blobs which exceed the #{file_size_limit}MiB limit:

              #{blob_id_size_msg}
              To resolve this error, you must either reduce the size of the above blobs, or utilize LFS.
              You may use "git ls-tree -r HEAD | grep $BLOB_ID" to see the file path.
              Please refer to #{Rails.application.routes.url_helpers.help_page_url('user/free_push_limit')} and
              #{Rails.application.routes.url_helpers.help_page_url('administration/settings/account_and_limit_settings')}
              for further information.
            OVERSIZE_ERR_MSG

            Gitlab::AppJsonLogger.info(
              message: 'Found blob over global limit',
              blob_sizes: oversized_blobs.map(&:size),
              blob_details: blob_details
            )

            raise ::Gitlab::GitAccess::ForbiddenError, oversize_err_msg if enforce_global_file_size_limit?
          end
        end

        true
      end

      private

      def file_size_limit
        project.actual_limits.file_size_limit_mb
      end

      def enforce_global_file_size_limit?
        Feature.enabled?(:enforce_global_file_size_limit, project)
      end
    end
  end
end