Class: BulkImports::UploadsExportService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::ImportExport::CommandLineUtil
Defined in:
app/services/bulk_imports/uploads_export_service.rb

Constant Summary collapse

BATCH_SIZE =
100
AVATAR_PATH =
'avatar'

Constants included from Gitlab::ImportExport::CommandLineUtil

Gitlab::ImportExport::CommandLineUtil::CLEAN_DIR_IGNORE_FILE_NAMES, Gitlab::ImportExport::CommandLineUtil::CommandLineUtilError, Gitlab::ImportExport::CommandLineUtil::DEFAULT_DIR_MODE, Gitlab::ImportExport::CommandLineUtil::FileOversizedError, Gitlab::ImportExport::CommandLineUtil::HardLinkError, Gitlab::ImportExport::CommandLineUtil::UNTAR_MASK

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::ImportExport::CommandLineUtil

#gunzip, #gzip, #gzip_with_options, #mkdir_p, #tar_cf, #tar_czf, #untar_xf, #untar_zxf

Constructor Details

#initialize(portable, export_path) ⇒ UploadsExportService

Returns a new instance of UploadsExportService.



12
13
14
15
16
# File 'app/services/bulk_imports/uploads_export_service.rb', line 12

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

Instance Attribute Details

#exported_objects_countObject (readonly)

Returns the value of attribute exported_objects_count.



10
11
12
# File 'app/services/bulk_imports/uploads_export_service.rb', line 10

def exported_objects_count
  @exported_objects_count
end

Instance Method Details

#execute(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/bulk_imports/uploads_export_service.rb', line 18

def execute(options = {})
  relation = portable.uploads

  if options[:batch_ids]
    relation = relation.where(relation.model.primary_key => options[:batch_ids]) # rubocop:disable CodeReuse/ActiveRecord
  end

  relation.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))
    @exported_objects_count += 1
  rescue StandardError => e
    # Do not fail entire project export if something goes wrong during file download
    # (e.g. 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