Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AttachmentsCopier concern #1271

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions app/controllers/concerns/attachments_copier.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module AttachmentsCopier
def copy_attachments(record)
record.content.scan(Attachment::SCREENSHOT_REGEX).each do |screenshot_url|
_, _, _, _, project_id, node_id, filename, _ = screenshot_url
record.content.scan(Attachment::SCREENSHOT_REGEX).each do |screenshot_path|
full_screenshot_path, _, _, _, project_id, node_id, filename, _ = screenshot_path

attachment = Attachment.find_by(filename: filename, node_id: record.node_id_was)
attachment = Attachment.find_by(filename: CGI::unescape(filename), node_id: record.node_id_was)

if attachment
new_attachment = attachment.copy_to(record.node)
new_url = screenshot_url[0].gsub(
/nodes\/[0-9]+\/attachments/,
"nodes/#{new_attachment.node_id}/attachments"
new_filename = ERB::Util.url_encode(new_attachment.filename)
aapomm marked this conversation as resolved.
Show resolved Hide resolved
new_path = full_screenshot_path.gsub(
/nodes\/[0-9]+\/attachments\/.+/,
"nodes/#{new_attachment.node_id}/attachments/#{new_filename}"
)

record.content = record.content.gsub(screenshot_url[0], new_url)
record.content = record.content.gsub(full_screenshot_path, new_path)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion spec/features/evidence_moving_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ def create_node(label, parent = nil)
let(:current_evidence) { @evidence = create(:evidence, content: content, node: @node_5) }

describe 'moving an evidence to a different node' do
let(:attachment) { create(:attachment, node: @node_5) }
let(:attachment) { create(:attachment, filename: 'name with spaces.png', node: @node_5) }
let(:content) { "#[Description]#\n!/projects/#{current_project.id}/nodes/#{@node_5.id}/attachments/#{attachment.filename}!\n" }

before do
# Ensure this works with duplicated attachment
create(:attachment, filename: 'name with spaces.png', node: @node_1)

within('#modal_move_evidence') do
click_link @node_1.label
click_submit
Expand Down
5 changes: 4 additions & 1 deletion spec/features/note_moving_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ def create_node(label, parent = nil)
let(:current_note) { @note = create(:note, text: text, node: @node_5) }

describe 'moving a note to a different node' do
let(:attachment) { create(:attachment, node: @node_5) }
let(:attachment) { create(:attachment, filename: 'name with spaces.png', node: @node_5) }
let(:text) { "#[Description]#\n!/projects/#{current_project.id}/nodes/#{@node_5.id}/attachments/#{attachment.filename}!\n" }

before do
# Ensure this works with duplicated attachment
create(:attachment, filename: 'name with spaces.png', node: @node_1)

within('#modal_move_note') do
click_link @node_1.label
click_submit
Expand Down
Loading