2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-10 05:40:39 -04:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class UploadsManager
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
|
2018-07-12 05:08:19 -04:00
|
|
|
UPLOADS_BATCH_SIZE = 100
|
|
|
|
|
2018-08-29 09:41:56 -04:00
|
|
|
def initialize(project:, shared:, relative_export_path: 'uploads')
|
2018-07-10 05:40:39 -04:00
|
|
|
@project = project
|
|
|
|
@shared = shared
|
|
|
|
@relative_export_path = relative_export_path
|
|
|
|
end
|
|
|
|
|
2018-07-11 04:24:59 -04:00
|
|
|
def save
|
2018-08-14 14:20:21 -04:00
|
|
|
copy_project_uploads
|
2018-07-10 10:33:40 -04:00
|
|
|
|
|
|
|
true
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2018-07-10 10:33:40 -04:00
|
|
|
@shared.error(e)
|
|
|
|
false
|
2018-07-10 05:40:39 -04:00
|
|
|
end
|
|
|
|
|
2018-07-11 04:24:59 -04:00
|
|
|
def restore
|
|
|
|
Dir["#{uploads_export_path}/**/*"].each do |upload|
|
|
|
|
next if File.directory?(upload)
|
|
|
|
|
2018-07-11 09:58:42 -04:00
|
|
|
add_upload(upload)
|
2018-07-11 08:52:48 -04:00
|
|
|
end
|
2018-07-11 09:58:42 -04:00
|
|
|
|
|
|
|
true
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2018-07-11 04:24:59 -04:00
|
|
|
@shared.error(e)
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-07-10 05:40:39 -04:00
|
|
|
private
|
|
|
|
|
2018-07-11 09:58:42 -04:00
|
|
|
def add_upload(upload)
|
2018-07-13 10:13:13 -04:00
|
|
|
uploader_context = FileUploader.extract_dynamic_path(upload).named_captures.symbolize_keys
|
2018-07-11 09:58:42 -04:00
|
|
|
|
2020-10-15 14:08:43 -04:00
|
|
|
UploadService.new(@project, File.open(upload, 'r'), FileUploader, **uploader_context).execute.to_h
|
2018-07-11 09:58:42 -04:00
|
|
|
end
|
|
|
|
|
2018-08-14 14:20:21 -04:00
|
|
|
def copy_project_uploads
|
2018-07-16 05:45:11 -04:00
|
|
|
each_uploader do |uploader|
|
2018-07-13 09:41:24 -04:00
|
|
|
next unless uploader.file
|
2018-07-10 05:40:39 -04:00
|
|
|
|
2018-08-14 14:20:21 -04:00
|
|
|
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
|
2018-07-10 05:40:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def uploads_export_path
|
|
|
|
@uploads_export_path ||= File.join(@shared.export_path, @relative_export_path)
|
|
|
|
end
|
|
|
|
|
2018-07-16 05:45:11 -04:00
|
|
|
def each_uploader
|
2018-07-12 05:08:19 -04:00
|
|
|
avatar_path = @project.avatar&.upload&.path
|
|
|
|
|
|
|
|
if @relative_export_path == 'avatar'
|
|
|
|
yield(@project.avatar)
|
|
|
|
else
|
2018-07-16 05:45:11 -04:00
|
|
|
project_uploads_except_avatar(avatar_path).find_each(batch_size: UPLOADS_BATCH_SIZE) do |upload|
|
2019-10-02 20:05:59 -04:00
|
|
|
yield(upload.retrieve_uploader)
|
2018-07-10 05:40:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-16 05:45:11 -04:00
|
|
|
def project_uploads_except_avatar(avatar_path)
|
2018-07-12 05:08:19 -04:00
|
|
|
return @project.uploads unless avatar_path
|
|
|
|
|
2021-04-01 17:09:22 -04:00
|
|
|
@project.uploads.where.not(path: avatar_path)
|
2018-07-12 05:08:19 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 05:40:39 -04:00
|
|
|
def download_and_copy(upload)
|
2018-07-10 10:33:40 -04:00
|
|
|
secret = upload.try(:secret) || ''
|
|
|
|
upload_path = File.join(uploads_export_path, secret, upload.filename)
|
2018-07-10 09:29:31 -04:00
|
|
|
|
2018-07-10 10:33:40 -04:00
|
|
|
mkdir_p(File.join(uploads_export_path, secret))
|
2018-07-10 05:40:39 -04:00
|
|
|
|
2018-07-26 06:55:21 -04:00
|
|
|
download_or_copy_upload(upload, upload_path)
|
2020-11-04 07:09:14 -05:00
|
|
|
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-07-10 05:40:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|