diff options
Diffstat (limited to 'lib/bulk_imports/groups/pipelines')
-rw-r--r-- | lib/bulk_imports/groups/pipelines/group_avatar_pipeline.rb | 49 | ||||
-rw-r--r-- | lib/bulk_imports/groups/pipelines/members_pipeline.rb | 3 |
2 files changed, 52 insertions, 0 deletions
diff --git a/lib/bulk_imports/groups/pipelines/group_avatar_pipeline.rb b/lib/bulk_imports/groups/pipelines/group_avatar_pipeline.rb new file mode 100644 index 00000000000..6de8bbbc910 --- /dev/null +++ b/lib/bulk_imports/groups/pipelines/group_avatar_pipeline.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module BulkImports + module Groups + module Pipelines + class GroupAvatarPipeline + include Pipeline + + ALLOWED_AVATAR_DOWNLOAD_TYPES = (AvatarUploader::MIME_WHITELIST + %w(application/octet-stream)).freeze + + GroupAvatarLoadingError = Class.new(StandardError) + + def extract(context) + context.extra[:tmpdir] = Dir.mktmpdir + + filepath = BulkImports::FileDownloadService.new( + configuration: context.configuration, + relative_url: "/groups/#{context.entity.encoded_source_full_path}/avatar", + dir: context.extra[:tmpdir], + file_size_limit: Avatarable::MAXIMUM_FILE_SIZE, + allowed_content_types: ALLOWED_AVATAR_DOWNLOAD_TYPES + ).execute + + BulkImports::Pipeline::ExtractedData.new(data: { filepath: filepath }) + end + + def load(context, data) + return if data.blank? + + File.open(data[:filepath]) do |avatar| + service = ::Groups::UpdateService.new( + portable, + current_user, + avatar: avatar + ) + + unless service.execute + raise GroupAvatarLoadingError, portable.errors.full_messages.first + end + end + end + + def after_run(_) + FileUtils.remove_entry(context.extra[:tmpdir]) if context.extra[:tmpdir].present? + end + end + end + end +end diff --git a/lib/bulk_imports/groups/pipelines/members_pipeline.rb b/lib/bulk_imports/groups/pipelines/members_pipeline.rb index 5e4293d2c06..265abd5e3a7 100644 --- a/lib/bulk_imports/groups/pipelines/members_pipeline.rb +++ b/lib/bulk_imports/groups/pipelines/members_pipeline.rb @@ -15,6 +15,9 @@ module BulkImports def load(context, data) return unless data + # Current user is already a member + return if data['user_id'].to_i == context.current_user.id + context.group.members.create!(data) end end |