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

Added FileIdQuery and FileIdsQuery and refactored usage

Andreas Pfohl07ddd37b4430
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_id_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_id_query.rb
new file mode 100644
index 00000000000..3111f65141d
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_id_query.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages::Peripherals::StorageInteraction::Nextcloud
+ class FileIdQuery
+ def initialize(storage)
+ @query = ::Storages::Peripherals::StorageInteraction::Nextcloud::Internal::PropfindQuery.new(storage)
+ end
+
+ def call(path:)
+ query_params = {
+ depth: '0',
+ path:,
+ props: %w[oc:fileid]
+ }
+
+ @query.call(**query_params).map { |result| result.dig(path, 'fileid') }
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_ids_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_ids_query.rb
new file mode 100644
index 00000000000..ce969dbc05d
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_ids_query.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages::Peripherals::StorageInteraction::Nextcloud
+ class FileIdsQuery
+ def initialize(storage)
+ @query = ::Storages::Peripherals::StorageInteraction::Nextcloud::Internal::PropfindQuery.new(storage)
+ end
+
+ def call(path:)
+ query_params = {
+ depth: '1',
+ path:,
+ props: %w[oc:fileid]
+ }
+
+ @query.call(**query_params)
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/propfind_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
similarity index 74%
rename from modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/propfind_query.rb
rename to modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
index 5cc95e27b65..896de899d55 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/propfind_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
@@ -26,7 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module Storages::Peripherals::StorageInteraction::Nextcloud
+module Storages::Peripherals::StorageInteraction::Nextcloud::Internal
class PropfindQuery
# Only for information purposes currently.
# Probably a bit later we could validate `#call` parameters.
@@ -82,10 +82,17 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end.to_xml
- response = Util.http(@uri).propfind(
- Util.join_uri_path(@uri, 'remote.php/dav/files', CGI.escapeURIComponent(@username), Util.escape_path(path)),
+ response = ::Storages::Peripherals::StorageInteraction::Nextcloud::Util.http(@uri).propfind(
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .join_uri_path(
+ @uri,
+ 'remote.php/dav/files',
+ CGI.escapeURIComponent(@username),
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util.escape_path(path)
+ ),
body,
- Util.basic_auth_header(@username, @password).merge('Depth' => depth)
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .basic_auth_header(@username, @password).merge('Depth' => depth)
)
case response
@@ -95,7 +102,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
doc.xpath('/d:multistatus/d:response').each do |resource_section|
resource = CGI
.unescape(resource_section.xpath("d:href").text.strip)
- .gsub!(Util.join_uri_path(@uri.path, "/remote.php/dav/files/#{@username}/"), "")
+ .gsub!(
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .join_uri_path(@uri.path, "/remote.php/dav/files/#{@username}/"), ""
+ )
result[resource] = {}
# In future it could be useful to respond not only with found, but not found props as well
@@ -106,15 +116,20 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
ServiceResult.success(result:)
when Net::HTTPMethodNotAllowed
- Util.error(:not_allowed)
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .error(:not_allowed)
when Net::HTTPUnauthorized
- Util.error(:not_authorized)
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .error(:not_authorized)
when Net::HTTPNotFound
- Util.error(:not_found)
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .error(:not_found)
else
- Util.error(:error)
+ ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ .error(:error)
end
end
+
# rubocop:enable Metrics/AbcSize
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_requests.rb
index 558e40d4dee..737daf98604 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -41,9 +41,10 @@ module Storages::Peripherals
download_link_query
file_query
files_query
+ file_id_query
+ file_ids_query
upload_link_query
group_users_query
- propfind_query
].freeze
def initialize(storage:)
diff --git a/modules/storages/app/services/projects/copy/storage_project_folders_dependent_service.rb b/modules/storages/app/services/projects/copy/storage_project_folders_dependent_service.rb
index ab64a4d0fbf..db009e3b080 100644
--- a/modules/storages/app/services/projects/copy/storage_project_folders_dependent_service.rb
+++ b/modules/storages/app/services/projects/copy/storage_project_folders_dependent_service.rb
@@ -59,33 +59,24 @@ module Projects::Copy
source_folder_name = project_folder_path(source_project_storage)
destination_folder_name = project_folder_path(destination_project_storage)
- Storages::Peripherals::StorageRequests
- .new(storage: source_project_storage.storage)
- .copy_template_folder_command
- .call(source_path: source_folder_name, destination_path: destination_folder_name)
- .on_failure { |r| add_error!(source_folder_name, r.to_active_model_errors) }
+ result = Storages::Peripherals::StorageRequests
+ .new(storage: source_project_storage.storage)
+ .copy_template_folder_command
+ .call(source_path: source_folder_name, destination_path: destination_folder_name)
+ result.on_failure { |r| add_error!(source_folder_name, r.to_active_model_errors) }
end
def update_project_folder_id(project_storage)
destination_folder_name = project_folder_path(project_storage)
- query_params = {
- depth: '0',
- path: destination_folder_name,
- props: %w[oc:fileid]
- }
-
- Storages::Peripherals::StorageRequests
- .new(storage: project_storage.storage)
- .propfind_query
- .call(**query_params)
- .match(
- on_success: ->(r) do
- file_id = r[destination_folder_name]["fileid"]
- project_storage.update!(project_folder_id: file_id)
- end,
- on_failure: ->(r) { add_error!(destination_folder_name, r.to_active_model_errors) }
- )
+ result = Storages::Peripherals::StorageRequests
+ .new(storage: project_storage.storage)
+ .file_id_query
+ .call(path: destination_folder_name)
+ result.match(
+ on_success: ->(file_id) { project_storage.update!(project_folder_id: file_id) },
+ on_failure: ->(error) { add_error!(destination_folder_name, error.to_active_model_errors) }
+ )
end
def project_folder_path(project_storage)
diff --git a/modules/storages/app/services/storages/group_folder_properties_sync_service.rb b/modules/storages/app/services/storages/group_folder_properties_sync_service.rb
index fa6d08a7929..668e78e4549 100644
--- a/modules/storages/app/services/storages/group_folder_properties_sync_service.rb
+++ b/modules/storages/app/services/storages/group_folder_properties_sync_service.rb
@@ -59,9 +59,9 @@ class Storages::GroupFolderPropertiesSyncService
set_group_folder_root_permissions
@storage.projects_storages
- .automatic
- .includes(project: %i[users enabled_modules])
- .each do |project_storage|
+ .automatic
+ .includes(project: %i[users enabled_modules])
+ .each do |project_storage|
project = project_storage.project
project_folder_path = project_folder_path(project)
@project_folder_ids_used_in_openproject << ensure_project_folder(project_storage:, project_folder_path:)
@@ -73,6 +73,7 @@ class Storages::GroupFolderPropertiesSyncService
add_active_users_to_group
remove_inactive_users_from_group
end
+
# rubocop:enable Metrics/AbcSize
private
@@ -110,6 +111,7 @@ class Storages::GroupFolderPropertiesSyncService
# then the value inside the local variable will not be updated
project_storage.project_folder_id
end
+
# rubocop:enable Metrics/AbcSize
def file_ids
@@ -118,18 +120,11 @@ class Storages::GroupFolderPropertiesSyncService
def folders_properties
@folders_properties ||=
- begin
- query_params = {
- depth: '1',
- path: @group_folder,
- props: %w[oc:fileid]
- }
- @requests
- .propfind_query
- .call(**query_params)
- .on_failure(&failure_handler('propfind_query', query_params))
- .result
- end
+ @requests
+ .file_ids_query
+ .call(path: @group_folder)
+ .on_failure(&failure_handler('file_ids_query', { path: @group_folder }))
+ .result
end
def rename_folder(source:, target:)
@@ -141,10 +136,10 @@ class Storages::GroupFolderPropertiesSyncService
def create_folder(path:, project_storage:)
@requests.create_folder_command.call(folder_path: path)
- .match(
- on_success: ->(_) { ServiceResult.success(result: [project_storage, path]) },
- on_failure: failure_handler('create_folder_command', { folder_path: path })
- )
+ .match(
+ on_success: ->(_) { ServiceResult.success(result: [project_storage, path]) },
+ on_failure: failure_handler('create_folder_command', { folder_path: path })
+ )
end
def save_file_id
@@ -155,17 +150,12 @@ class Storages::GroupFolderPropertiesSyncService
def obtain_file_id
->((project_storage, path)) do
- query_params = {
- depth: '0',
- path:,
- props: %w[oc:fileid]
- }
@requests
- .propfind_query
- .call(**query_params)
+ .file_id_query
+ .call(path:)
.match(
- on_success: ->(result) { ServiceResult.success(result: [project_storage, result.dig(path, 'fileid')]) },
- on_failure: failure_handler('propfind_query', query_params)
+ on_success: ->(file_id) { ServiceResult.success(result: [project_storage, file_id]) },
+ on_failure: failure_handler('file_id_query', { path: })
)
end
end
@@ -201,10 +191,10 @@ class Storages::GroupFolderPropertiesSyncService
@group_users ||= begin
query_params = { group: @group }
@requests
- .group_users_query
- .call(**query_params)
- .on_failure(&failure_handler('group_users_query', query_params))
- .result
+ .group_users_query
+ .call(**query_params)
+ .on_failure(&failure_handler('group_users_query', query_params))
+ .result
end
end
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index 9d5e6951df5..e8fc9ffcfa6 100644
--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
@@ -857,7 +857,7 @@ RSpec.describe Storages::Peripherals::StorageRequests, webmock: true do
end
end
- describe '#propfind_query' do
+ describe '#file_ids_query' do
let(:nextcloud_subpath) { '' }
let(:url) { "https://example.com#{nextcloud_subpath}" }
let(:expected_request_body) do
@@ -955,11 +955,11 @@ RSpec.describe Storages::Peripherals::StorageRequests, webmock: true do
).to_return(status: 200, body: expected_response_body, headers: {})
end
- shared_examples 'a propfind_query response' do
+ shared_examples 'a file_ids_query response' do
it 'responds with a list of paths and attributes for each of them' do
result = subject
- .propfind_query
- .call(depth: '1', path: 'OpenProject', props: %w[oc:fileid])
+ .file_ids_query
+ .call(path: 'OpenProject')
.result
expect(result).to eq({ "OpenProject/" => { "fileid" => "349" },
"OpenProject/Project #2/" => { "fileid" => "381" },
@@ -971,12 +971,12 @@ RSpec.describe Storages::Peripherals::StorageRequests, webmock: true do
end
end
- it_behaves_like 'a propfind_query response'
+ it_behaves_like 'a file_ids_query response'
context 'when NC is deployed under subpath' do
let(:nexcloud_subpath) { '/subpath' }
- it_behaves_like 'a propfind_query response'
+ it_behaves_like 'a file_ids_query response'
end
end