summaryrefslogtreecommitdiff
path: root/lib/gitlab/import_export/lfs_restorer.rb
blob: 345c7880e30c6134522661ed6db1b6408512d49a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

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.all_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