OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openproject/app/services/copy/concerns/copy_attachments.rb

26 lines
899 B

module Copy
module Concerns
module CopyAttachments
##
# Tries to copy the given attachment between containers
def copy_attachments(container_type, from_id:, to_id:)
Attachment
.where(container_type: container_type, container_id: from_id)
.find_each do |old_attachment|
copied = old_attachment.dup
old_attachment.file.copy_to(copied)
copied.author = user
copied.container_type = old_attachment.container_type
copied.container_id = to_id
unless copied.save
Rails.logger.error { "Attachments ##{old_attachment.id} could not be copied: #{copied.errors.full_messages} " }
end
rescue StandardError => e
Rails.logger.error { "Failed to copy attachments from ##{from_container_id} to ##{to_container_id}: #{e}" }
end
end
end
end
end