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

68 lines
1.5 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 Saver
include Gitlab::ImportExport::CommandLineUtil
def self.save(*args)
new(*args).save
end
2016-08-24 12:49:21 +05:30
def initialize(project:, shared:)
@project = project
2016-06-22 15:30:34 +05:30
@shared = shared
end
def save
if compress_and_save
remove_export_path
2018-11-08 19:23:39 +05:30
2019-09-30 21:07:59 +05:30
Rails.logger.info("Saved project export #{archive_file}") # rubocop:disable Gitlab/RailsLogger
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
save_upload
2016-06-22 15:30:34 +05:30
else
2018-11-08 19:23:39 +05:30
@shared.error(Gitlab::ImportExport::Error.new(error_message))
2016-06-22 15:30:34 +05:30
false
end
rescue => e
@shared.error(e)
false
2018-11-08 19:23:39 +05:30
ensure
2018-11-20 20:47:30 +05:30
remove_archive
remove_export_path
2016-06-22 15:30:34 +05:30
end
private
def compress_and_save
tar_czf(archive: archive_file, dir: @shared.export_path)
end
def remove_export_path
FileUtils.rm_rf(@shared.export_path)
end
2018-11-08 19:23:39 +05:30
def remove_archive
FileUtils.rm_rf(@shared.archive_path)
end
2016-06-22 15:30:34 +05:30
def archive_file
2018-03-17 18:26:18 +05:30
@archive_file ||= File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(project: @project))
2016-06-22 15:30:34 +05:30
end
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
def save_upload
2018-11-08 19:23:39 +05:30
upload = ImportExportUpload.find_or_initialize_by(project: @project)
File.open(archive_file) { |file| upload.export_file = file }
upload.save!
end
def error_message
2018-11-20 20:47:30 +05:30
"Unable to save #{archive_file} into #{@shared.export_path}."
2018-11-08 19:23:39 +05:30
end
2016-06-22 15:30:34 +05:30
end
end
end