-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
process_package_file_service.rb
63 lines (48 loc) · 1.78 KB
/
process_package_file_service.rb
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
# frozen_string_literal: true
module Packages
module TerraformModule
class ProcessPackageFileService
include Gitlab::Utils::StrongMemoize
ExtractionError = Class.new(StandardError)
def initialize(package_file)
@package_file = package_file
end
def execute
raise ExtractionError, 'invalid package file' unless valid_package_file?
result = nil
with_archive_file do |archive_file|
result = ::Packages::TerraformModule::Metadata::ExtractFilesService.new(archive_file).execute
end
if result&.success?
::Packages::TerraformModule::Metadata::CreateService.new(package_file.package, result.payload).execute
end
ServiceResponse.success
end
private
attr_reader :package_file
def valid_package_file?
package_file && package_file.package&.terraform_module? && !package_file.file.empty_size?
end
def with_archive_file(&block)
package_file.file.use_open_file(unlink_early: false) do |open_file|
success = process_as_gzip(open_file, &block)
process_as_zip(open_file, &block) unless success
end
end
def process_as_gzip(open_file, &block)
Zlib::GzipReader.open(open_file.file_path) do |gzip_file|
Gem::Package::TarReader.new(gzip_file, &block)
end
true
rescue Zlib::GzipFile::Error => e
return false if e.message == 'not in gzip format'
raise ExtractionError, e.message
end
def process_as_zip(open_file, &block)
Zip::File.open(open_file.file_path, &block) # rubocop:disable Performance/Rubyzip -- Zip::InputStream has some limitations
rescue Zip::Error => e
raise ExtractionError, e.message
end
end
end
end