summaryrefslogtreecommitdiff
path: root/lib/gitlab/import_export/uploads_manager.rb
blob: c968deb6b198de67ea81828ef28dc7a1138aa087 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module Gitlab
  module ImportExport
    class UploadsManager
      include Gitlab::ImportExport::CommandLineUtil

      def initialize(project:, shared:, relative_export_path: 'uploads', from: nil)
        @project = project
        @shared = shared
        @relative_export_path = relative_export_path
        @from = from || default_uploads_path
      end

      def save
        copy_files(@from, uploads_export_path) if File.directory?(@from)

        if File.file?(@from) && @relative_export_path == 'avatar'
          copy_files(@from, File.join(uploads_export_path, @project.avatar.filename))
        end

        copy_from_object_storage

        true
      rescue => e
        @shared.error(e)
        false
      end

      def restore
        Dir["#{uploads_export_path}/**/*"].each do |upload|
          next if File.directory?(upload)

          UploadService.new(@project, File.open(upload, 'r'), FileUploader).execute
        end

        true
      rescue => e
        @shared.error(e)
        false
      end

      private

      def copy_from_object_storage
        return unless Gitlab::ImportExport.object_storage?
        return if uploads.empty?

        uploads.each do |upload_model|
          next unless upload_model.file
          next if upload_model.upload.local? # Already copied

          download_and_copy(upload_model)
        end
      end

      def default_uploads_path
        FileUploader.absolute_base_dir(@project)
      end

      def uploads_export_path
        @uploads_export_path ||= File.join(@shared.export_path, @relative_export_path)
      end

      def uploads
        @uploads ||= begin
          if @relative_export_path == 'avatar'
            [@project.avatar].compact
          else
            (@project.uploads - [@project.avatar&.upload]).map(&:build_uploader)
          end
        end
      end

      def download_and_copy(upload)
        secret = upload.try(:secret) || ''
        upload_path = File.join(uploads_export_path, secret, upload.filename)

        mkdir_p(File.join(uploads_export_path, secret))

        File.open(upload_path, 'w') do |file|
          IO.copy_stream(URI.parse(upload.file.url).open, file)
        end
      end
    end
  end
end