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

zip_stream.rb « adapters « artifacts « build « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 690a47097c60f7f081029c4c846201e286a2da33 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Build
      module Artifacts
        module Adapters
          class ZipStream
            MAX_DECOMPRESSED_SIZE = 100.megabytes
            MAX_FILES_PROCESSED = 50

            attr_reader :stream

            InvalidStreamError = Class.new(StandardError)

            def initialize(stream)
              raise InvalidStreamError, "Stream is required" unless stream

              @stream = stream
              @files_processed = 0
            end

            def each_blob
              Zip::InputStream.open(stream) do |zio|
                while entry = zio.get_next_entry
                  break if at_files_processed_limit?
                  next unless should_process?(entry)

                  @files_processed += 1

                  yield entry.get_input_stream.read
                end
              end
            end

            private

            def should_process?(entry)
              file?(entry) && !too_large?(entry)
            end

            def file?(entry)
              # Check the file name as a workaround for incorrect
              # file type detection when using InputStream
              # https://github.com/rubyzip/rubyzip/issues/533
              entry.file? && !entry.name.end_with?('/')
            end

            def too_large?(entry)
              entry.size > MAX_DECOMPRESSED_SIZE
            end

            def at_files_processed_limit?
              @files_processed >= MAX_FILES_PROCESSED
            end
          end
        end
      end
    end
  end
end