Welcome to mirror list, hosted at ThFree Co, Russian Federation.

uploads_export_service.rb « bulk_imports « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f5ee7b8624f8b6f263761b367dbae307bc33b1f (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
# frozen_string_literal: true

module BulkImports
  class UploadsExportService
    include Gitlab::ImportExport::CommandLineUtil

    BATCH_SIZE = 100
    AVATAR_PATH = 'avatar'

    def initialize(portable, export_path)
      @portable = portable
      @export_path = export_path
    end

    def execute
      portable.uploads.find_each(batch_size: BATCH_SIZE) do |upload| # rubocop: disable CodeReuse/ActiveRecord
        uploader = upload.retrieve_uploader

        next unless upload.exist?
        next unless uploader.file

        subdir_path = export_subdir_path(upload)
        mkdir_p(subdir_path)
        download_or_copy_upload(uploader, File.join(subdir_path, uploader.filename))
      rescue Errno::ENAMETOOLONG => e
        # Do not fail entire export process if downloaded file has filename that exceeds 255 characters.
        # Ignore raised exception, skip such upload, log the error and keep going with the export instead.
        Gitlab::ErrorTracking.log_exception(e, portable_id: portable.id, portable_class: portable.class.name, upload_id: upload.id)
      end
    end

    private

    attr_reader :portable, :export_path

    def export_subdir_path(upload)
      subdir = if upload.path == avatar_path
                 AVATAR_PATH
               else
                 upload.try(:secret).to_s
               end

      File.join(export_path, subdir)
    end

    def avatar_path
      @avatar_path ||= portable.avatar&.upload&.path
    end
  end
end