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

extract_metadata_file_service.rb « nuget « packages « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 61e4892fee7be27d107845a1d7879f23e2ea7cdc (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
# frozen_string_literal: true

module Packages
  module Nuget
    class ExtractMetadataFileService
      include Gitlab::Utils::StrongMemoize

      ExtractionError = Class.new(StandardError)

      MAX_FILE_SIZE = 4.megabytes.freeze

      def initialize(package_file_id)
        @package_file_id = package_file_id
      end

      def execute
        raise ExtractionError, 'invalid package file' unless valid_package_file?

        ServiceResponse.success(payload: nuspec_file_content)
      end

      private

      attr_reader :package_file_id

      def package_file
        ::Packages::PackageFile.find_by_id(package_file_id)
      end
      strong_memoize_attr :package_file

      def valid_package_file?
        package_file &&
          package_file.package&.nuget? &&
          package_file.file.size > 0 # rubocop:disable Style/ZeroLengthPredicate
      end

      def nuspec_file_content
        with_zip_file do |zip_file|
          entry = zip_file.glob('*.nuspec').first

          raise ExtractionError, 'nuspec file not found' unless entry
          raise ExtractionError, 'nuspec file too big' if MAX_FILE_SIZE < entry.size

          Tempfile.open("nuget_extraction_package_file_#{package_file_id}") do |file|
            entry.extract(file.path) { true } # allow #extract to overwrite the file
            file.unlink
            file.read
          end
        rescue Zip::EntrySizeError => e
          raise ExtractionError, "nuspec file has the wrong entry size: #{e.message}"
        end
      end

      def with_zip_file
        package_file.file.use_open_file do |open_file|
          zip_file = Zip::File.new(open_file, false, true) # rubocop:disable Performance/Rubyzip
          yield(zip_file)
        end
      end
    end
  end
end