2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-30 10:32:21 -04:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class LfsSaver
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
2022-04-06 11:08:23 -04:00
|
|
|
include DurationMeasuring
|
2018-03-30 10:32:21 -04:00
|
|
|
|
2019-07-24 07:23:51 -04:00
|
|
|
attr_accessor :lfs_json, :project, :shared
|
|
|
|
|
|
|
|
BATCH_SIZE = 100
|
|
|
|
|
2018-03-30 10:32:21 -04:00
|
|
|
def initialize(project:, shared:)
|
|
|
|
@project = project
|
|
|
|
@shared = shared
|
2019-07-24 07:23:51 -04:00
|
|
|
@lfs_json = {}
|
2018-03-30 10:32:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2022-04-06 11:08:23 -04:00
|
|
|
with_duration_measuring do
|
|
|
|
project.lfs_objects.find_in_batches(batch_size: BATCH_SIZE) do |batch|
|
|
|
|
batch.each do |lfs_object|
|
|
|
|
save_lfs_object(lfs_object)
|
|
|
|
end
|
2019-07-24 07:23:51 -04:00
|
|
|
|
2022-04-06 11:08:23 -04:00
|
|
|
append_lfs_json_for_batch(batch)
|
|
|
|
end
|
2018-03-30 10:32:21 -04:00
|
|
|
|
2022-04-06 11:08:23 -04:00
|
|
|
write_lfs_json
|
2019-07-24 07:23:51 -04:00
|
|
|
|
2022-04-06 11:08:23 -04:00
|
|
|
true
|
|
|
|
end
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2019-07-24 07:23:51 -04:00
|
|
|
shared.error(e)
|
2018-03-30 10:32:21 -04:00
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def save_lfs_object(lfs_object)
|
|
|
|
if lfs_object.local_store?
|
|
|
|
copy_file_for_lfs_object(lfs_object)
|
|
|
|
else
|
2018-04-03 12:54:42 -04:00
|
|
|
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)
|
2018-03-30 10:32:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_file_for_lfs_object(lfs_object)
|
|
|
|
copy_files(lfs_object.file.path, destination_path_for_object(lfs_object))
|
|
|
|
end
|
|
|
|
|
2019-07-24 07:23:51 -04:00
|
|
|
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-03-30 10:32:21 -04:00
|
|
|
def destination_path_for_object(lfs_object)
|
|
|
|
File.join(lfs_export_path, lfs_object.oid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def lfs_export_path
|
2019-07-24 07:23:51 -04:00
|
|
|
File.join(shared.export_path, ImportExport.lfs_objects_storage)
|
|
|
|
end
|
|
|
|
|
|
|
|
def lfs_json_path
|
|
|
|
File.join(shared.export_path, ImportExport.lfs_objects_filename)
|
2018-03-30 10:32:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|