2021-12-11 22:18:48 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
module Common
|
|
|
|
module Pipelines
|
|
|
|
class UploadsPipeline
|
|
|
|
include Pipeline
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
AVATAR_PATTERN = %r{.*\/#{BulkImports::UploadsExportService::AVATAR_PATH}\/(?<identifier>.*)}.freeze
|
|
|
|
|
|
|
|
AvatarLoadingError = Class.new(StandardError)
|
|
|
|
|
|
|
|
def extract(_context)
|
|
|
|
download_service.execute
|
|
|
|
decompression_service.execute
|
|
|
|
extraction_service.execute
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
upload_file_paths = Dir.glob(File.join(tmpdir, '**', '*'))
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
BulkImports::Pipeline::ExtractedData.new(data: upload_file_paths)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load(context, file_path)
|
2022-01-26 12:08:38 +05:30
|
|
|
avatar_path = AVATAR_PATTERN.match(file_path)
|
|
|
|
|
|
|
|
return save_avatar(file_path) if avatar_path
|
|
|
|
|
|
|
|
dynamic_path = file_uploader.extract_dynamic_path(file_path)
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
return unless dynamic_path
|
|
|
|
return if File.directory?(file_path)
|
2022-01-26 12:08:38 +05:30
|
|
|
return if File.lstat(file_path).symlink?
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
named_captures = dynamic_path.named_captures.symbolize_keys
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
UploadService.new(context.portable, File.open(file_path, 'r'), file_uploader, **named_captures).execute
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def after_run(_)
|
2022-03-02 08:16:31 +05:30
|
|
|
FileUtils.remove_entry(tmpdir) if Dir.exist?(tmpdir)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def download_service
|
2021-12-11 22:18:48 +05:30
|
|
|
BulkImports::FileDownloadService.new(
|
|
|
|
configuration: context.configuration,
|
2022-01-26 12:08:38 +05:30
|
|
|
relative_url: context.entity.relation_download_url_path(relation),
|
2022-03-02 08:16:31 +05:30
|
|
|
tmpdir: tmpdir,
|
2022-01-26 12:08:38 +05:30
|
|
|
filename: targz_filename
|
2021-12-11 22:18:48 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def decompression_service
|
2022-03-02 08:16:31 +05:30
|
|
|
BulkImports::FileDecompressionService.new(tmpdir: tmpdir, filename: targz_filename)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def extraction_service
|
2022-03-02 08:16:31 +05:30
|
|
|
BulkImports::ArchiveExtractionService.new(tmpdir: tmpdir, filename: tar_filename)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def relation
|
|
|
|
BulkImports::FileTransfer::BaseConfig::UPLOADS_RELATION
|
|
|
|
end
|
|
|
|
|
|
|
|
def tar_filename
|
|
|
|
"#{relation}.tar"
|
|
|
|
end
|
|
|
|
|
|
|
|
def targz_filename
|
|
|
|
"#{tar_filename}.gz"
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def tmpdir
|
|
|
|
@tmpdir ||= Dir.mktmpdir('bulk_imports')
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
def file_uploader
|
|
|
|
@file_uploader ||= if context.entity.group?
|
|
|
|
NamespaceFileUploader
|
|
|
|
else
|
|
|
|
FileUploader
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_avatar(file_path)
|
|
|
|
File.open(file_path) do |avatar|
|
|
|
|
service = context.entity.update_service.new(portable, current_user, avatar: avatar)
|
|
|
|
|
|
|
|
unless service.execute
|
|
|
|
raise AvatarLoadingError, portable.errors.full_messages.to_sentence
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|