debian-mirror-gitlab/lib/gitlab/import_export/uploads_manager.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.6 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
module Gitlab
module ImportExport
class UploadsManager
include Gitlab::ImportExport::CommandLineUtil
UPLOADS_BATCH_SIZE = 100
2018-11-20 20:47:30 +05:30
def initialize(project:, shared:, relative_export_path: 'uploads')
2018-11-08 19:23:39 +05:30
@project = project
@shared = shared
@relative_export_path = relative_export_path
end
def save
copy_project_uploads
true
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2018-11-08 19:23:39 +05:30
@shared.error(e)
false
end
def restore
Dir["#{uploads_export_path}/**/*"].each do |upload|
next if File.directory?(upload)
add_upload(upload)
end
true
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2018-11-08 19:23:39 +05:30
@shared.error(e)
false
end
private
def add_upload(upload)
uploader_context = FileUploader.extract_dynamic_path(upload).named_captures.symbolize_keys
2021-01-03 14:25:43 +05:30
UploadService.new(@project, File.open(upload, 'r'), FileUploader, **uploader_context).execute.to_h
2018-11-08 19:23:39 +05:30
end
def copy_project_uploads
each_uploader do |uploader|
next unless uploader.file
if uploader.upload.local?
next unless uploader.upload.exist?
copy_files(uploader.absolute_path, File.join(uploads_export_path, uploader.upload.path))
else
download_and_copy(uploader)
end
end
end
def uploads_export_path
@uploads_export_path ||= File.join(@shared.export_path, @relative_export_path)
end
def each_uploader
avatar_path = @project.avatar&.upload&.path
if @relative_export_path == 'avatar'
yield(@project.avatar)
else
project_uploads_except_avatar(avatar_path).find_each(batch_size: UPLOADS_BATCH_SIZE) do |upload|
2019-12-21 20:55:43 +05:30
yield(upload.retrieve_uploader)
2018-11-08 19:23:39 +05:30
end
end
end
def project_uploads_except_avatar(avatar_path)
return @project.uploads unless avatar_path
2021-04-29 21:17:54 +05:30
@project.uploads.where.not(path: avatar_path)
2018-11-08 19:23:39 +05:30
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))
2018-11-18 11:00:15 +05:30
download_or_copy_upload(upload, upload_path)
2021-01-29 00:20:46 +05:30
rescue Errno::ENAMETOOLONG => e
# Do not fail entire project export 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, project_id: @project.id)
2018-11-08 19:23:39 +05:30
end
end
end
end