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

[#57501] file link creation via API fails on legacy nextcloud configs

Eric Schubertd371921a39c5
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index 27eee1c2659..d03c8d0f3fb 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -122,8 +122,8 @@ module API::V3::FileLinks
# remove all trailing slashes except the last one
canonical_url = "#{fragment['href'].gsub(/\/+$/, '')}/"
- represented.storage = ::Storages::Storage.find_by(host: canonical_url)
- represented.storage ||= ::Storages::Storage::InexistentStorage.new(host: canonical_url)
+ represented.storage = find_storage_by_url(canonical_url) ||
+ ::Storages::Storage::InexistentStorage.new(host: canonical_url)
}
associated_resource :container,
@@ -153,6 +153,15 @@ module API::V3::FileLinks
}
end
+ def find_storage_by_url(canonical_url)
+ found = ::Storages::Storage.find_by(host: canonical_url)
+ return found if found.present?
+
+ # Search for storages that are still using the legacy URL format
+ legacy_url_data = canonical_url.chomp("/")
+ ::Storages::Storage.find_by(host: legacy_url_data)
+ end
+
def parse_origin_data(origin_data)
{
origin_id: origin_data["id"].to_s,
diff --git a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb
index 00bedb0b16d..152d0bd43bd 100644
--- a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb
+++ b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_parsing_spec.rb
@@ -40,9 +40,9 @@ RSpec.describe API::V3::FileLinks::FileLinkRepresenter, "parsing" do
let(:current_user) { build_stubbed(:user) }
before do
- allow(Storages::Storage).to receive(:find_by)
- .with(host: storage.host)
- .and_return storage
+ allow(Storages::Storage).to receive(:find_by) do |args|
+ args[:host] == storage.host ? storage : nil
+ end
end
describe "parsing" do
@@ -88,6 +88,14 @@ RSpec.describe API::V3::FileLinks::FileLinkRepresenter, "parsing" do
end
end
+ context "if storage is configured with legacy url format (without trailing slash)" do
+ let(:storage) { build_stubbed(:nextcloud_storage, host: "https://host.without-trailing.slash") }
+
+ it "is parsed correctly" do
+ expect(parsed).to have_attributes(storage_id: storage.id)
+ end
+ end
+
context "if storage is given as resource" do
let(:parsed_hash) do
{