diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-03-30 19:45:58 +0200 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-04-05 10:21:51 +0200 |
commit | 79cb4d99c0e47bfd988788ff38871e668367dfbf (patch) | |
tree | ed690679d2b74f8e3e37f68b877cc80f13b99e12 /lib/gitlab/import_export/lfs_restorer.rb | |
parent | 84ee2ddbcd850d29ae852333c57e2e8381f5a535 (diff) | |
download | gitlab-ce-79cb4d99c0e47bfd988788ff38871e668367dfbf.tar.gz |
Import projects with LFS objects
If the LFS object already exist, we'll link it tot he existing one, if
not we'll create it.
Diffstat (limited to 'lib/gitlab/import_export/lfs_restorer.rb')
-rw-r--r-- | lib/gitlab/import_export/lfs_restorer.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/gitlab/import_export/lfs_restorer.rb b/lib/gitlab/import_export/lfs_restorer.rb new file mode 100644 index 00000000000..4f144d5c8e6 --- /dev/null +++ b/lib/gitlab/import_export/lfs_restorer.rb @@ -0,0 +1,43 @@ +module Gitlab + module ImportExport + class LfsRestorer + def initialize(project:, shared:) + @project = project + @shared = shared + end + + def restore + return true if lfs_file_paths.empty? + + lfs_file_paths.each do |file_path| + link_or_create_lfs_object!(file_path) + end + + true + rescue => e + @shared.error(e) + false + end + + private + + def link_or_create_lfs_object!(path) + size = File.size(path) + oid = LfsObject.calculate_oid(path) + + lfs_object = LfsObject.find_or_initialize_by(oid: oid, size: size) + lfs_object.file = File.open(path) unless lfs_object.file&.exists? + + @project.lfs_storage_project.lfs_objects << lfs_object + end + + def lfs_file_paths + @lfs_file_paths ||= Dir.glob("#{lfs_storage_path}/*") + end + + def lfs_storage_path + File.join(@shared.export_path, 'lfs-objects') + end + end + end +end |