Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Update links to images in wiki pages

Oliver Günther3ba3fae461a2
diff --git a/app/services/copy/concerns/copy_attachments.rb b/app/services/copy/concerns/copy_attachments.rb
index 869c0d135ef..ba22146b68e 100644
--- a/app/services/copy/concerns/copy_attachments.rb
+++ b/app/services/copy/concerns/copy_attachments.rb
@@ -3,20 +3,36 @@ module Copy
module CopyAttachments
##
# Tries to copy the given attachment between containers
- def copy_attachments(container_type, from_id:, to_id:)
+ def copy_attachments(container_type, from:, to:, references: [])
Attachment
- .where(container_type:, container_id: from_id)
+ .where(container_type:, container_id: from.id)
.find_each do |source|
- copy = Attachment
- .new(attachment_copy_attributes(source, to_id))
- source.file.copy_to(copy)
+ copy_attachment(from:, to:, references:, source:)
+ end
+
+ if to.changed? && !to.save
+ Rails.logger.error { "Failed to update copied attachment references in to #{to}: #{to.errors.full_messages}" }
+ end
+ end
+
+ def copy_attachment(source:, from:, to:, references:)
+ copy = Attachment
+ .new(attachment_copy_attributes(source, to.id))
+ source.file.copy_to(copy)
- unless copy.save
- Rails.logger.error { "Attachments ##{source.id} could not be copy: #{copy.errors.full_messages} " }
- end
- rescue StandardError => e
- Rails.logger.error { "Failed to copy attachments from ##{from_id} to ##{to_id}: #{e}" }
+ if copy.save
+ update_references(
+ attachment_source: source.id,
+ attachment_target: copy.id,
+ model_source: from,
+ model_target: to,
+ references:
+ )
+ else
+ Rails.logger.error { "Attachments ##{source.id} could not be copy: #{copy.errors.full_messages} " }
end
+ rescue StandardError => e
+ Rails.logger.error { "Failed to copy attachments from #{from} to #{to}: #{e}" }
end
def attachment_copy_attributes(source, to_id)
@@ -27,6 +43,18 @@ module Copy
.merge('author_id' => user.id,
'container_id' => to_id)
end
+
+ def update_references(attachment_source:, attachment_target:, model_source:, model_target:, references:)
+ references.each do |reference|
+ text = model_source.send(reference)
+ next if text.nil?
+
+ replaced = text.gsub("/api/v3/attachments/#{attachment_source}/content",
+ "/api/v3/attachments/#{attachment_target}/content")
+
+ model_target.send(:"#{reference}=", replaced)
+ end
+ end
end
end
end
diff --git a/app/services/wiki_pages/copy_service.rb b/app/services/wiki_pages/copy_service.rb
index 7e37b9f670b..118b62e8553 100644
--- a/app/services/wiki_pages/copy_service.rb
+++ b/app/services/wiki_pages/copy_service.rb
@@ -79,6 +79,11 @@ class WikiPages::CopyService
end
def copy_wiki_page_attachments(copy)
- copy_attachments('WikiPage', from_id: model.id, to_id: copy.id)
+ copy_attachments(
+ 'WikiPage',
+ from: model,
+ to: copy,
+ references: %i[text]
+ )
end
end
diff --git a/spec/services/wiki_pages/copy_service_integration_spec.rb b/spec/services/wiki_pages/copy_service_integration_spec.rb
index 027afe837ac..0bc66ca3fbb 100644
--- a/spec/services/wiki_pages/copy_service_integration_spec.rb
+++ b/spec/services/wiki_pages/copy_service_integration_spec.rb
@@ -115,6 +115,30 @@ RSpec.describe WikiPages::CopyService, 'integration', type: :model do
end
end
+ context 'when referencing the attachment in the wiki text' do
+ let(:text) do
+ <<~MARKDOWN
+ # Some text here
+
+ ![attachment#{attachment.id}](/api/v3/attachments/#{attachment.id}/content)
+ MARKDOWN
+ end
+
+ before do
+ wiki_page.update!(text:)
+ end
+
+ it 'updates the attachment reference' do
+ expect(wiki_page.text).to include "/api/v3/attachments/#{attachment.id}/content"
+
+ expect(copy.attachments.length).to eq 1
+ expect(copy.attachments.first.id).not_to eq attachment.id
+
+ expect(copy.reload.text).not_to include "/api/v3/attachments/#{attachment.id}/content"
+ expect(copy.text).to include "/api/v3/attachments/#{copy.attachments.first.id}/content"
+ end
+ end
+
context 'when specifying to not copy attachments' do
let(:attributes) { { copy_attachments: false } }