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

maven.rb « packages « helpers « api « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c50f4c00a1e2f3e7d7e2b03be10e65f551daeb0 (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
81
82
83
84
# frozen_string_literal: true

module API
  module Helpers
    module Packages
      module Maven
        extend Grape::API::Helpers

        params :path_and_file_name do
          requires :path,
            type: String,
            file_path: true,
            desc: 'Package path',
            documentation: { example: 'foo/bar/mypkg/1.0-SNAPSHOT' }
          requires :file_name,
            type: String,
            file_path: true,
            desc: 'Package file name',
            documentation: { example: 'mypkg-1.0-SNAPSHOT.jar' }
        end

        def extract_format(file_name)
          name, _, format = file_name.rpartition('.')

          if %w[md5 sha1].include?(format)
            unprocessable_entity! if Gitlab::FIPS.enabled? && format == 'md5'

            [name, format]
          else
            [file_name, format]
          end
        end

        def fetch_package(file_name:, project: nil, group: nil)
          order_by_package_file = file_name.include?(::Packages::Maven::Metadata.filename) &&
            params[:path].exclude?(::Packages::Maven::FindOrCreatePackageService::SNAPSHOT_TERM)

          ::Packages::Maven::PackageFinder.new(
            current_user,
            project || group,
            path: params[:path],
            order_by_package_file: order_by_package_file
          ).execute&.last
        end

        def project
          nil
        end

        def group
          nil
        end

        def present_carrierwave_file_with_head_support!(package_file, supports_direct_download: true)
          package_file.package.touch_last_downloaded_at
          file = package_file.file

          if head_request_on_aws_file?(file, supports_direct_download) && !file.file_storage?
            return redirect(signed_head_url(file))
          end

          present_carrierwave_file!(file, supports_direct_download: supports_direct_download)
        end

        def signed_head_url(file)
          fog_storage = ::Fog::Storage.new(file.fog_credentials)
          fog_dir = fog_storage.directories.new(key: file.fog_directory)
          fog_file = fog_dir.files.new(key: file.path)
          expire_at = ::Fog::Time.now + file.fog_authenticated_url_expiration

          fog_file.collection.head_url(fog_file.key, expire_at)
        end

        def head_request_on_aws_file?(file, supports_direct_download)
          Gitlab.config.packages.object_store.enabled &&
            supports_direct_download &&
            file.class.direct_download_enabled? &&
            request.head? &&
            file.fog_credentials[:provider] == 'AWS'
        end
      end
    end
  end
end