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

when POSTing file links, duplicated file link payload results in only one file link record creation

Christophe Bliardf07f79389b12
diff --git a/docs/api/apiv3/paths/work_package_file_links.yml b/docs/api/apiv3/paths/work_package_file_links.yml
index 8d2fda9dfc8..19a12b2e922 100644
--- a/docs/api/apiv3/paths/work_package_file_links.yml
+++ b/docs/api/apiv3/paths/work_package_file_links.yml
@@ -10,14 +10,19 @@ post:
Creates file links on a work package.
The request body must contain the generated storage token, and not the storage id. In addition, the request must
- send the origin file id, name, and mime type. The request should also send the origin meta data: `createdAt`, `createdByName`, `lastModifiedAt`, `lastModifiedByName` can the stored meta data.
+ send the origin file id, name, and mime type.
+
+ The request should also send the origin meta data: _createdAt_, _createdByName_, _lastModifiedAt_,
+ _lastModifiedByName_. As it is possible to link folders from a storage to a work package, the _mimeType_ of this
+ entity can be empty.
Up to 20 file links can be submitted at once. If there is already a link on the given work package with the same
file id from the same storage, the previous data is not overwritten.
- As it is possible to link folders from a storage to a work package, the _mimeType_ of this entity can be empty.
-
If some data is invalid, no file links will be created.
+
+ If a file link with matching origin id, work package, and storage already exists, then it will not create an
+ additional file link or update the metadata. Instead the information from the existing file line will be returned.
parameters:
- name: id
description: Work package id
diff --git a/modules/storages/app/services/storages/file_links/create_service.rb b/modules/storages/app/services/storages/file_links/create_service.rb
index 60d2a457795..9da8ed533bc 100644
--- a/modules/storages/app/services/storages/file_links/create_service.rb
+++ b/modules/storages/app/services/storages/file_links/create_service.rb
@@ -27,4 +27,24 @@
#++
class Storages::FileLinks::CreateService < ::BaseServices::Create
+ def persist(service_result)
+ if existing = find_existing(service_result.result)
+ service_result.result = existing
+ service_result
+ else
+ # create
+ super(service_result)
+ end
+ end
+
+ private
+
+ def find_existing(file_link)
+ Storages::FileLink.find_by(
+ origin_id: file_link.origin_id,
+ container_id: file_link.container_id,
+ container_type: file_link.container_type,
+ storage_id: file_link.storage_id
+ )
+ end
end
diff --git a/modules/storages/spec/factories/file_link_element_factory.rb b/modules/storages/spec/factories/file_link_element_factory.rb
index 88638e080bb..eca84a91e25 100644
--- a/modules/storages/spec/factories/file_link_element_factory.rb
+++ b/modules/storages/spec/factories/file_link_element_factory.rb
@@ -29,7 +29,7 @@
# Expects parameters: storage, container_id
FactoryBot.define do
factory :file_link_element, class: Hash do
- sequence(:origin_id) { |n| "10000#{n}" } # ID within external storage (i.e. Nextcloud)
+ sequence(:origin_id) { |n| "20000#{n}" } # ID within external storage (i.e. Nextcloud)
sequence(:origin_name) { |n| "file_name_#{n}.txt" } # File name within external storage (i.e. Nextcloud)
origin_mime_type { "text/plain" }
origin_created_at { Time.zone.now }
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
index e49a9e9cc19..119b6dbca13 100644
--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
@@ -28,6 +28,7 @@
require 'spec_helper'
+# rubocop:disable RSpec/MultipleMemoizedHelpers
describe 'API v3 file links resource', type: :request do
include API::V3::Utilities::PathHelper
@@ -159,6 +160,72 @@ describe 'API v3 file links resource', type: :request do
end
end
+ context 'when some file link elements with matching origin_id, container, and storage already exist in database' do
+ let(:existing_file_link) do
+ create(:file_link, origin_name: 'original name',
+ creator: current_user,
+ container: work_package,
+ storage: storage)
+ end
+ let(:already_existing_file_link_payload) do
+ build(:file_link_element, origin_name: 'new name',
+ origin_id: existing_file_link.origin_id,
+ storage_url: existing_file_link.storage.host)
+ end
+ let(:some_file_link_payload) do
+ build(:file_link_element, storage_url: existing_file_link.storage.host)
+ end
+ let(:embedded_elements) do
+ [
+ already_existing_file_link_payload,
+ some_file_link_payload
+ ]
+ end
+
+ it_behaves_like 'API V3 collection response', 2, 2, 'FileLink' do
+ let(:elements) { Storages::FileLink.all.order(id: :asc) }
+ let(:expected_status_code) { 201 }
+ end
+
+ it 'does not create any new FileLink records for the already existing one' do
+ expect(Storages::FileLink.count).to eq 2
+ end
+
+ it 'does not update the existing FileLink metadata from the POSTed one' do
+ expect(existing_file_link.reload.origin_name).to eq 'original name'
+ end
+ end
+
+ context 'when multiple file links elements are submitted with same origin_id, container, and storage' do
+ let(:some_file_link_payload) { build(:file_link_element, storage_url: storage.host) }
+ let(:embedded_elements) do
+ [
+ some_file_link_payload.deep_merge(originData: { name: 'first name' }),
+ some_file_link_payload.deep_merge(originData: { name: 'second name' }),
+ some_file_link_payload.deep_merge(originData: { name: 'third name' })
+ ]
+ end
+
+ it_behaves_like 'API V3 collection response', 3, 3, 'FileLink' do
+ let(:elements) { Storages::FileLink.all.order(id: :asc) }
+ let(:expected_status_code) { 201 }
+ end
+
+ it 'creates only one FileLink for all duplicates' do
+ expect(Storages::FileLink.count).to eq 1
+ end
+
+ it 'uses metadata from the first item' do
+ expect(Storages::FileLink.first.origin_name).to eq 'first name'
+ end
+
+ it 'replies with as many embedded elements as in the request, all identical', :aggregate_failures do
+ replied_elements = JSON.parse(last_response.body).dig('_embedded', 'elements')
+ expect(replied_elements.count).to eq(embedded_elements.count)
+ expect(replied_elements[1..]).to all(eq(replied_elements.first))
+ end
+ end
+
context 'when storage host is invalid' do
context 'when unknown host' do
let(:storage_url1) { 'https://invalid.host.org/' }
@@ -217,7 +284,6 @@ describe 'API v3 file links resource', type: :request do
actual: 'Integer')
end
- # rubocop:disable RSpec/MultipleMemoizedHelpers
context "when more than #{API::V3::FileLinks::ParseCreateParamsService::MAX_ELEMENTS} embedded elements" do
let(:max) { API::V3::FileLinks::ParseCreateParamsService::MAX_ELEMENTS }
let(:too_many) { max + 1 }
@@ -227,7 +293,6 @@ describe 'API v3 file links resource', type: :request do
let(:message) { "Too many elements created at once. Expected #{max} at most, got #{too_many}." }
end
end
- # rubocop:enable RSpec/MultipleMemoizedHelpers
end
describe 'GET /api/v3/file_links/:file_link_id' do
@@ -311,3 +376,4 @@ describe 'API v3 file links resource', type: :request do
end
end
end
+# rubocop:enable RSpec/MultipleMemoizedHelpers