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

97 lines
2.5 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module Gitlab
module ImportExport
class LfsSaver
include Gitlab::ImportExport::CommandLineUtil
2019-10-12 21:52:04 +05:30
attr_accessor :lfs_json, :project, :shared
BATCH_SIZE = 100
2018-05-09 12:01:36 +05:30
def initialize(project:, shared:)
@project = project
@shared = shared
2019-10-12 21:52:04 +05:30
@lfs_json = {}
2018-05-09 12:01:36 +05:30
end
def save
2020-11-24 15:15:51 +05:30
project.lfs_objects.find_in_batches(batch_size: BATCH_SIZE) do |batch|
2019-10-12 21:52:04 +05:30
batch.each do |lfs_object|
save_lfs_object(lfs_object)
end
append_lfs_json_for_batch(batch) if write_lfs_json_enabled?
2018-05-09 12:01:36 +05:30
end
2019-10-12 21:52:04 +05:30
write_lfs_json if write_lfs_json_enabled?
2018-05-09 12:01:36 +05:30
true
rescue => e
2019-10-12 21:52:04 +05:30
shared.error(e)
2018-05-09 12:01:36 +05:30
false
end
private
2019-10-12 21:52:04 +05:30
def write_lfs_json_enabled?
::Feature.enabled?(:export_lfs_objects_projects, default_enabled: true)
end
2018-05-09 12:01:36 +05:30
def save_lfs_object(lfs_object)
if lfs_object.local_store?
copy_file_for_lfs_object(lfs_object)
else
download_file_for_lfs_object(lfs_object)
end
end
def download_file_for_lfs_object(lfs_object)
destination = destination_path_for_object(lfs_object)
mkdir_p(File.dirname(destination))
File.open(destination, 'w') do |file|
IO.copy_stream(URI.parse(lfs_object.file.url).open, file)
end
end
def copy_file_for_lfs_object(lfs_object)
copy_files(lfs_object.file.path, destination_path_for_object(lfs_object))
end
2019-10-12 21:52:04 +05:30
def append_lfs_json_for_batch(lfs_objects_batch)
lfs_objects_projects = LfsObjectsProject
.select('lfs_objects.oid, array_agg(distinct lfs_objects_projects.repository_type) as repository_types')
.joins(:lfs_object)
.where(project: project, lfs_object: lfs_objects_batch)
.group('lfs_objects.oid')
lfs_objects_projects.each do |group|
oid = group.oid
lfs_json[oid] ||= []
lfs_json[oid] += group.repository_types
end
end
def write_lfs_json
mkdir_p(shared.export_path)
File.write(lfs_json_path, lfs_json.to_json)
end
2018-05-09 12:01:36 +05:30
def destination_path_for_object(lfs_object)
File.join(lfs_export_path, lfs_object.oid)
end
def lfs_export_path
2019-10-12 21:52:04 +05:30
File.join(shared.export_path, ImportExport.lfs_objects_storage)
end
def lfs_json_path
File.join(shared.export_path, ImportExport.lfs_objects_filename)
2018-05-09 12:01:36 +05:30
end
end
end
end