debian-mirror-gitlab/lib/gitlab/import_export/file_importer.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
2.7 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-06-22 15:30:34 +05:30
module Gitlab
module ImportExport
class FileImporter
include Gitlab::ImportExport::CommandLineUtil
2020-01-01 13:55:28 +05:30
ImporterError = Class.new(StandardError)
2016-08-24 12:49:21 +05:30
MAX_RETRIES = 8
2021-01-03 14:25:43 +05:30
def self.import(*args, **kwargs)
new(*args, **kwargs).import
2016-06-22 15:30:34 +05:30
end
2020-01-01 13:55:28 +05:30
def initialize(importable:, archive_file:, shared:)
@importable = importable
2016-06-22 15:30:34 +05:30
@archive_file = archive_file
@shared = shared
end
def import
2016-11-03 12:29:30 +05:30
mkdir_p(@shared.export_path)
2018-11-18 11:00:15 +05:30
mkdir_p(@shared.archive_path)
2016-08-24 12:49:21 +05:30
2023-09-09 18:01:29 +05:30
clean_extraction_dir!(@shared.export_path)
2018-11-18 11:00:15 +05:30
copy_archive
2018-03-17 18:26:18 +05:30
2016-08-24 12:49:21 +05:30
wait_for_archived_file do
2022-07-16 23:28:13 +05:30
validate_decompressed_archive_size if Feature.enabled?(:validate_import_decompressed_archive_size)
2016-08-24 12:49:21 +05:30
decompress_archive
end
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2016-06-22 15:30:34 +05:30
@shared.error(e)
false
2018-03-17 18:26:18 +05:30
ensure
2018-11-18 11:00:15 +05:30
remove_import_file
2023-09-09 18:01:29 +05:30
clean_extraction_dir!(@shared.export_path)
2016-06-22 15:30:34 +05:30
end
private
2016-08-24 12:49:21 +05:30
# Exponentially sleep until I/O finishes copying the file
def wait_for_archived_file
MAX_RETRIES.times do |retry_number|
break if File.exist?(@archive_file)
sleep(2**retry_number)
end
yield
end
2016-06-22 15:30:34 +05:30
def decompress_archive
2016-08-24 12:49:21 +05:30
result = untar_zxf(archive: @archive_file, dir: @shared.export_path)
2021-06-08 01:23:25 +05:30
raise ImporterError, "Unable to decompress #{@archive_file} into #{@shared.export_path}" unless result
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
result
2016-11-03 12:29:30 +05:30
end
2018-11-18 11:00:15 +05:30
def copy_archive
return if @archive_file
2020-01-01 13:55:28 +05:30
@archive_file = File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(exportable: @importable))
2018-11-18 11:00:15 +05:30
2021-06-08 01:23:25 +05:30
remote_download_or_download_or_copy_upload
end
def remote_download_or_download_or_copy_upload
import_export_upload = @importable.import_export_upload
if import_export_upload.remote_import_url.present?
2022-05-07 20:08:51 +05:30
download(
import_export_upload.remote_import_url,
@archive_file,
size_limit: ::Import::GitlabProjects::RemoteFileValidator::FILE_SIZE_LIMIT
)
2021-06-08 01:23:25 +05:30
else
2022-05-07 20:08:51 +05:30
download_or_copy_upload(
import_export_upload.import_file,
@archive_file,
size_limit: ::Import::GitlabProjects::RemoteFileValidator::FILE_SIZE_LIMIT
)
2021-06-08 01:23:25 +05:30
end
2018-11-18 11:00:15 +05:30
end
def remove_import_file
FileUtils.rm_rf(@archive_file)
end
2020-08-18 19:51:02 +05:30
def validate_decompressed_archive_size
2021-06-08 01:23:25 +05:30
raise ImporterError, _('Decompressed archive size validation failed.') unless size_validator.valid?
2020-08-18 19:51:02 +05:30
end
def size_validator
@size_validator ||= DecompressedArchiveSizeValidator.new(archive_path: @archive_file)
end
2016-06-22 15:30:34 +05:30
end
end
end